...
This commit is contained in:
18
examples/hero/heromodels/heromodels_comments.vsh
Executable file
18
examples/hero/heromodels/heromodels_comments.vsh
Executable file
@@ -0,0 +1,18 @@
|
|||||||
|
#!/usr/bin/env -S v -n -w -cg -gc none -cc tcc -d use_openssl -enable-globals run
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.hero.heromodels
|
||||||
|
|
||||||
|
mut mydb := heromodels.new()!
|
||||||
|
|
||||||
|
mut o := mydb.comments.new(comment: 'Hello, world!')!
|
||||||
|
|
||||||
|
o.tags = mydb.comments.db.tags_get(['tag1', 'tag2'])!
|
||||||
|
|
||||||
|
oid := mydb.comments.set(o)!
|
||||||
|
mut o2 := mydb.comments.get(oid)!
|
||||||
|
|
||||||
|
println(oid)
|
||||||
|
println(o2)
|
||||||
|
|
||||||
|
mut objects := mydb.comments.list()!
|
||||||
|
println(objects)
|
||||||
@@ -9,15 +9,15 @@ fn main() {
|
|||||||
mut handler := openrpc.new_heromodels_handler()!
|
mut handler := openrpc.new_heromodels_handler()!
|
||||||
|
|
||||||
my_calendar := heromodels.calendar_new(
|
my_calendar := heromodels.calendar_new(
|
||||||
name: "My Calendar"
|
name: 'My Calendar'
|
||||||
description: "My Calendar"
|
description: 'My Calendar'
|
||||||
securitypolicy: 1
|
securitypolicy: 1
|
||||||
tags: ["tag1", "tag2"]
|
tags: ['tag1', 'tag2']
|
||||||
group_id: 1,
|
group_id: 1
|
||||||
events: []u32{},
|
events: []u32{}
|
||||||
color: "#000000",
|
color: '#000000'
|
||||||
timezone: "UTC",
|
timezone: 'UTC'
|
||||||
is_public: true,
|
is_public: true
|
||||||
)!
|
)!
|
||||||
|
|
||||||
response := handler.handle(jsonrpc.new_request('calendar_set', json.encode(my_calendar)))!
|
response := handler.handle(jsonrpc.new_request('calendar_set', json.encode(my_calendar)))!
|
||||||
|
|||||||
54
lib/hero/db/core_methods.v
Normal file
54
lib/hero/db/core_methods.v
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
module db
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.data.ourtime
|
||||||
|
|
||||||
|
pub fn (mut self DB) set[T](obj_ T) !u32 {
|
||||||
|
// Get the next ID
|
||||||
|
mut obj := obj_
|
||||||
|
if obj.id == 0 {
|
||||||
|
obj.id = self.new_id()!
|
||||||
|
}
|
||||||
|
mut t := ourtime.now().unix()
|
||||||
|
if obj.created_at == 0 {
|
||||||
|
obj.created_at = t
|
||||||
|
}
|
||||||
|
obj.updated_at = t
|
||||||
|
|
||||||
|
data := obj.dump()!
|
||||||
|
self.redis.hset(self.db_name[T](), obj.id.str(), data.bytestr())!
|
||||||
|
return obj.id
|
||||||
|
}
|
||||||
|
|
||||||
|
// return the data, cannot return the object as we do not know the type
|
||||||
|
pub fn (mut self DB) get_data[T](id u32) ![]u8 {
|
||||||
|
data := self.redis.hget(self.db_name[T](), id.str())!
|
||||||
|
return data.bytes()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (mut self DB) exists[T](id u32) !bool {
|
||||||
|
return self.redis.hexists(self.db_name[T](), id.str())!
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (mut self DB) delete[T](id u32) ! {
|
||||||
|
self.redis.hdel(self.db_name[T](), id.str())!
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (mut self DB) list[T]() ![]u32 {
|
||||||
|
ids := self.redis.hkeys(self.db_name[T]())!
|
||||||
|
return ids.map(it.u32())
|
||||||
|
}
|
||||||
|
|
||||||
|
// make it easy to get a base object
|
||||||
|
pub fn (mut self DB) new_from_base[T](args BaseArgs) !Base {
|
||||||
|
return T{
|
||||||
|
Base: new_base(args)!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (mut self DB) db_name[T]() string {
|
||||||
|
return 'db:${T.name}'
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (mut self DB) new_id() !u32 {
|
||||||
|
return u32(self.redis.incr('db:id')!)
|
||||||
|
}
|
||||||
38
lib/hero/db/core_models.v
Normal file
38
lib/hero/db/core_models.v
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
module db
|
||||||
|
|
||||||
|
import crypto.md5
|
||||||
|
import freeflowuniverse.herolib.core.redisclient
|
||||||
|
import freeflowuniverse.herolib.data.ourtime
|
||||||
|
|
||||||
|
// Group represents a collection of users with roles and permissions
|
||||||
|
@[heap]
|
||||||
|
pub struct Base {
|
||||||
|
pub mut:
|
||||||
|
id u32
|
||||||
|
name string
|
||||||
|
description string
|
||||||
|
created_at i64
|
||||||
|
updated_at i64
|
||||||
|
securitypolicy u32
|
||||||
|
tags u32 // when we set/get we always do as []string but this can then be sorted and md5ed this gies the unique id of tags
|
||||||
|
comments []u32
|
||||||
|
}
|
||||||
|
|
||||||
|
@[heap]
|
||||||
|
pub struct SecurityPolicy {
|
||||||
|
pub mut:
|
||||||
|
id u32
|
||||||
|
read []u32 // links to users & groups
|
||||||
|
write []u32 // links to users & groups
|
||||||
|
delete []u32 // links to users & groups
|
||||||
|
public bool
|
||||||
|
md5 string // this sorts read, write and delete u32 + hash, then do md5 hash, this allows to go from a random read/write/delete/public config to a hash
|
||||||
|
}
|
||||||
|
|
||||||
|
@[heap]
|
||||||
|
pub struct Tags {
|
||||||
|
pub mut:
|
||||||
|
id u32
|
||||||
|
names []string // unique per id
|
||||||
|
md5 string // of sorted names, to make easy to find unique id, each name lowercased and made ascii
|
||||||
|
}
|
||||||
18
lib/hero/db/factory.v
Normal file
18
lib/hero/db/factory.v
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
module db
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.core.redisclient
|
||||||
|
|
||||||
|
// Current time
|
||||||
|
// import freeflowuniverse.herolib.data.encoder
|
||||||
|
|
||||||
|
pub struct DB {
|
||||||
|
pub mut:
|
||||||
|
redis &redisclient.Redis @[skip; str: skip]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new() !DB {
|
||||||
|
mut redisconnection := redisclient.core_get()!
|
||||||
|
return DB{
|
||||||
|
redis: redisconnection
|
||||||
|
}
|
||||||
|
}
|
||||||
62
lib/hero/db/helpers_comments.v
Normal file
62
lib/hero/db/helpers_comments.v
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
module db
|
||||||
|
|
||||||
|
import crypto.md5
|
||||||
|
|
||||||
|
// @[params]
|
||||||
|
// pub struct BaseArgs {
|
||||||
|
// pub mut:
|
||||||
|
// id ?u32
|
||||||
|
// name string
|
||||||
|
// description string
|
||||||
|
// securitypolicy ?u32
|
||||||
|
// tags []string
|
||||||
|
// comments []u32
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // make it easy to get a base object
|
||||||
|
// pub fn (mut self DB) new_base(args BaseArgs) !Base {
|
||||||
|
// mut redis := redisclient.core_get()!
|
||||||
|
|
||||||
|
// tags := tags2id(args.tags)!
|
||||||
|
|
||||||
|
// return Base{
|
||||||
|
// id: args.id or { 0 }
|
||||||
|
// name: args.name
|
||||||
|
// description: args.description
|
||||||
|
// created_at: ourtime.now().unix()
|
||||||
|
// updated_at: ourtime.now().unix()
|
||||||
|
// securitypolicy: args.securitypolicy or { 0 }
|
||||||
|
// tags: tags
|
||||||
|
// comments: args.comments
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
@[params]
|
||||||
|
pub struct CommentArg {
|
||||||
|
pub mut:
|
||||||
|
comment string
|
||||||
|
parent u32
|
||||||
|
author u32
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (mut self DB) comments_get(args []CommentArg) ![]u32 {
|
||||||
|
return args.map(self.comment_get(it.comment)!)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (mut self DB) comment_get(comment string) !u32 {
|
||||||
|
comment_fixed := comment.to_lower_ascii().trim_space()
|
||||||
|
return if comment_fixed.len > 0 {
|
||||||
|
hash := md5.hexhash(comment_fixed)
|
||||||
|
comment_found := self.redis.hget('db:comments', hash)!
|
||||||
|
if comment_found == '' {
|
||||||
|
id := self.new_id()!
|
||||||
|
self.redis.hset('db:comments', hash, id.str())!
|
||||||
|
self.redis.hset('db:comments', id.str(), comment_fixed)!
|
||||||
|
id
|
||||||
|
} else {
|
||||||
|
comment_found.u32()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
22
lib/hero/db/helpers_tags.v
Normal file
22
lib/hero/db/helpers_tags.v
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
module db
|
||||||
|
|
||||||
|
import crypto.md5
|
||||||
|
|
||||||
|
pub fn (mut self DB) tags_get(tags []string) !u32 {
|
||||||
|
return if tags.len > 0 {
|
||||||
|
mut tags_fixed := tags.map(it.to_lower_ascii().trim_space()).filter(it != '')
|
||||||
|
tags_fixed.sort_ignore_case()
|
||||||
|
hash := md5.hexhash(tags_fixed.join(','))
|
||||||
|
tags_found := self.redis.hget('db:tags', hash)!
|
||||||
|
return if tags_found == '' {
|
||||||
|
id := self.new_id()!
|
||||||
|
self.redis.hset('db:tags', hash, id.str())!
|
||||||
|
self.redis.hset('db:tags', id.str(), tags_fixed.join(','))!
|
||||||
|
id
|
||||||
|
} else {
|
||||||
|
tags_found.u32()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
27
lib/hero/heromodels copy/ai_openrpcprompt.md
Normal file
27
lib/hero/heromodels copy/ai_openrpcprompt.md
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
lets make an openrpc server
|
||||||
|
over unixsocker
|
||||||
|
|
||||||
|
on /tmp/heromodels
|
||||||
|
|
||||||
|
put code in lib/hero/heromodels/openrpc
|
||||||
|
|
||||||
|
do example for comment.v
|
||||||
|
|
||||||
|
make struct called RPCServer
|
||||||
|
|
||||||
|
put as methods
|
||||||
|
|
||||||
|
- comment_get(args CommentGetArgs)[]Comment! //chose the params well is @[params] struct CommentGetArgs always with id… in this case maybe author. …
|
||||||
|
- walk over the hset with data, find the one we are looking for based on the args
|
||||||
|
- comment_set(obj Comment)!
|
||||||
|
- comment_delete(id…)
|
||||||
|
- comment_list() ![]u32
|
||||||
|
- discover()!string //returns a full openrpc spec
|
||||||
|
|
||||||
|
make one .v file per type of object now comment_…
|
||||||
|
|
||||||
|
we will then do for the other objects too
|
||||||
|
|
||||||
|
also generate the openrpc spec based on the methods we have and the objects we return
|
||||||
|
|
||||||
|
|
||||||
117
lib/hero/heromodels copy/comment.v
Normal file
117
lib/hero/heromodels copy/comment.v
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
module heromodels
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.data.encoder
|
||||||
|
import crypto.md5
|
||||||
|
import freeflowuniverse.herolib.core.redisclient
|
||||||
|
import freeflowuniverse.herolib.data.ourtime
|
||||||
|
|
||||||
|
|
||||||
|
@[heap]
|
||||||
|
pub struct Comment {
|
||||||
|
Base
|
||||||
|
pub mut:
|
||||||
|
// id u32
|
||||||
|
comment string
|
||||||
|
parent u32 //id of parent comment if any, 0 means none
|
||||||
|
updated_at i64
|
||||||
|
author u32 //links to user
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (self Comment) type_name() string {
|
||||||
|
return 'comments'
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (self Comment) load(data []u8) !Comment {
|
||||||
|
return comment_load(data)!
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (self Comment) dump() ![]u8{
|
||||||
|
// Create a new encoder
|
||||||
|
mut e := encoder.new()
|
||||||
|
e.add_u8(1)
|
||||||
|
e.add_u32(self.id)
|
||||||
|
e.add_string(self.comment)
|
||||||
|
e.add_u32(self.parent)
|
||||||
|
e.add_i64(self.updated_at)
|
||||||
|
e.add_u32(self.author)
|
||||||
|
return e.data
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn comment_load(data []u8) !Comment{
|
||||||
|
// Create a new decoder
|
||||||
|
mut e := encoder.decoder_new(data)
|
||||||
|
version := e.get_u8()!
|
||||||
|
if version != 1 {
|
||||||
|
panic("wrong version in comment load")
|
||||||
|
}
|
||||||
|
mut comment := Comment{}
|
||||||
|
comment.id = e.get_u32()!
|
||||||
|
comment.comment = e.get_string()!
|
||||||
|
comment.parent = e.get_u32()!
|
||||||
|
comment.updated_at = e.get_i64()!
|
||||||
|
comment.author = e.get_u32()!
|
||||||
|
return comment
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub struct CommentArg {
|
||||||
|
pub mut:
|
||||||
|
comment string
|
||||||
|
parent u32
|
||||||
|
author u32
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn comment_multiset(args []CommentArg) ![]u32 {
|
||||||
|
return comments2ids(args)!
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn comments2ids(args []CommentArg) ![]u32 {
|
||||||
|
return args.map(comment2id(it.comment)!)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn comment2id(comment string) !u32 {
|
||||||
|
comment_fixed := comment.to_lower_ascii().trim_space()
|
||||||
|
mut redis := redisclient.core_get()!
|
||||||
|
return if comment_fixed.len > 0{
|
||||||
|
hash := md5.hexhash(comment_fixed)
|
||||||
|
comment_found := redis.hget("db:comments", hash)!
|
||||||
|
if comment_found == ""{
|
||||||
|
id := u32(redis.incr("db:comments:id")!)
|
||||||
|
redis.hset("db:comments", hash, id.str())!
|
||||||
|
redis.hset("db:comments", id.str(), comment_fixed)!
|
||||||
|
id
|
||||||
|
}else{
|
||||||
|
comment_found.u32()
|
||||||
|
}
|
||||||
|
} else { 0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//get new comment, not from the DB
|
||||||
|
pub fn comment_new(args CommentArg) !Comment{
|
||||||
|
mut o := Comment {
|
||||||
|
comment: args.comment
|
||||||
|
parent: args.parent
|
||||||
|
updated_at: ourtime.now().unix()
|
||||||
|
author: args.author
|
||||||
|
}
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn comment_set(args CommentArg) !u32{
|
||||||
|
mut o := comment_new(args)!
|
||||||
|
// Use openrpcserver set function which now returns the ID
|
||||||
|
return set[Comment](mut o)!
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn comment_delete(id u32) ! {
|
||||||
|
delete[Comment](id)!
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn comment_exist(id u32) !bool{
|
||||||
|
return exists[Comment](id)!
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn comment_get(id u32) !Comment{
|
||||||
|
return get[Comment](id)!
|
||||||
|
}
|
||||||
@@ -2,116 +2,94 @@ module heromodels
|
|||||||
|
|
||||||
import freeflowuniverse.herolib.data.encoder
|
import freeflowuniverse.herolib.data.encoder
|
||||||
import crypto.md5
|
import crypto.md5
|
||||||
import freeflowuniverse.herolib.core.redisclient
|
|
||||||
import freeflowuniverse.herolib.data.ourtime
|
import freeflowuniverse.herolib.data.ourtime
|
||||||
|
import freeflowuniverse.herolib.core.redisclient
|
||||||
|
import freeflowuniverse.herolib.hero.db
|
||||||
|
|
||||||
@[heap]
|
@[heap]
|
||||||
pub struct Comment {
|
pub struct Comment {
|
||||||
Base
|
db.Base
|
||||||
pub mut:
|
pub mut:
|
||||||
// id u32
|
// id u32
|
||||||
comment string
|
comment string
|
||||||
parent u32 //id of parent comment if any, 0 means none
|
parent u32 // id of parent comment if any, 0 means none
|
||||||
updated_at i64
|
author u32 // links to user
|
||||||
author u32 //links to user
|
}
|
||||||
|
|
||||||
|
//////////TO BE GENERATED BY AI////////////////////////////////
|
||||||
|
///BASIC CRUD FUNCTIONS
|
||||||
|
|
||||||
|
pub struct DBComments {
|
||||||
|
pub mut:
|
||||||
|
db &db.DB @[skip; str: skip]
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (self Comment) type_name() string {
|
pub fn (self Comment) type_name() string {
|
||||||
return 'comments'
|
return 'comments'
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (self Comment) load(data []u8) !Comment {
|
pub fn (self Comment) dump() ![]u8 {
|
||||||
return comment_load(data)!
|
// Create a new encoder
|
||||||
|
mut e := encoder.new()
|
||||||
|
e.add_u8(1)
|
||||||
|
e.add_u32(self.id)
|
||||||
|
e.add_string(self.comment)
|
||||||
|
e.add_u32(self.parent)
|
||||||
|
e.add_u32(self.author)
|
||||||
|
return e.data
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (self Comment) dump() ![]u8{
|
pub fn (mut self DBComments) load(data []u8) !Comment {
|
||||||
// Create a new encoder
|
// Create a new decoder
|
||||||
mut e := encoder.new()
|
mut e := encoder.decoder_new(data)
|
||||||
e.add_u8(1)
|
version := e.get_u8()!
|
||||||
e.add_u32(self.id)
|
if version != 1 {
|
||||||
e.add_string(self.comment)
|
panic('wrong version in comment load')
|
||||||
e.add_u32(self.parent)
|
}
|
||||||
e.add_i64(self.updated_at)
|
mut comment := Comment{}
|
||||||
e.add_u32(self.author)
|
comment.id = e.get_u32()!
|
||||||
return e.data
|
comment.comment = e.get_string()!
|
||||||
|
comment.parent = e.get_u32()!
|
||||||
|
comment.author = e.get_u32()!
|
||||||
|
return comment
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn comment_load(data []u8) !Comment{
|
@[params]
|
||||||
// Create a new decoder
|
|
||||||
mut e := encoder.decoder_new(data)
|
|
||||||
version := e.get_u8()!
|
|
||||||
if version != 1 {
|
|
||||||
panic("wrong version in comment load")
|
|
||||||
}
|
|
||||||
mut comment := Comment{}
|
|
||||||
comment.id = e.get_u32()!
|
|
||||||
comment.comment = e.get_string()!
|
|
||||||
comment.parent = e.get_u32()!
|
|
||||||
comment.updated_at = e.get_i64()!
|
|
||||||
comment.author = e.get_u32()!
|
|
||||||
return comment
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub struct CommentArg {
|
pub struct CommentArg {
|
||||||
pub mut:
|
pub mut:
|
||||||
comment string
|
comment string @[required]
|
||||||
parent u32
|
parent u32
|
||||||
author u32
|
author u32
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn comment_multiset(args []CommentArg) ![]u32 {
|
// get new comment, not from the DB
|
||||||
return comments2ids(args)!
|
pub fn (mut self DBComments) new(args CommentArg) !Comment {
|
||||||
|
mut o := Comment{
|
||||||
|
comment: args.comment
|
||||||
|
parent: args.parent
|
||||||
|
updated_at: ourtime.now().unix()
|
||||||
|
author: args.author
|
||||||
|
}
|
||||||
|
return o
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn comments2ids(args []CommentArg) ![]u32 {
|
pub fn (mut self DBComments) set(o Comment) !u32 {
|
||||||
return args.map(comment2id(it.comment)!)
|
// Use openrpcserver set function which now returns the ID
|
||||||
|
return self.db.set[Comment](o)!
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn comment2id(comment string) !u32 {
|
pub fn (mut self DBComments) delete(id u32) ! {
|
||||||
comment_fixed := comment.to_lower_ascii().trim_space()
|
self.db.delete[Comment](id)!
|
||||||
mut redis := redisclient.core_get()!
|
|
||||||
return if comment_fixed.len > 0{
|
|
||||||
hash := md5.hexhash(comment_fixed)
|
|
||||||
comment_found := redis.hget("db:comments", hash)!
|
|
||||||
if comment_found == ""{
|
|
||||||
id := u32(redis.incr("db:comments:id")!)
|
|
||||||
redis.hset("db:comments", hash, id.str())!
|
|
||||||
redis.hset("db:comments", id.str(), comment_fixed)!
|
|
||||||
id
|
|
||||||
}else{
|
|
||||||
comment_found.u32()
|
|
||||||
}
|
|
||||||
} else { 0 }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn (mut self DBComments) exist(id u32) !bool {
|
||||||
//get new comment, not from the DB
|
return self.db.exists[Comment](id)!
|
||||||
pub fn comment_new(args CommentArg) !Comment{
|
|
||||||
mut o := Comment {
|
|
||||||
comment: args.comment
|
|
||||||
parent: args.parent
|
|
||||||
updated_at: ourtime.now().unix()
|
|
||||||
author: args.author
|
|
||||||
}
|
|
||||||
return o
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn comment_set(args CommentArg) !u32{
|
pub fn (mut self DBComments) get(id u32) !Comment {
|
||||||
mut o := comment_new(args)!
|
return self.load(self.db.get_data[Comment](id)!)!
|
||||||
// Use openrpcserver set function which now returns the ID
|
|
||||||
return set[Comment](mut o)!
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn comment_delete(id u32) ! {
|
pub fn (mut self DBComments) list() ![]Comment {
|
||||||
delete[Comment](id)!
|
return self.db.list[Comment]()!.map(self.load(self.db.get_data[Comment](it)!)!)
|
||||||
}
|
|
||||||
|
|
||||||
pub fn comment_exist(id u32) !bool{
|
|
||||||
return exists[Comment](id)!
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn comment_get(id u32) !Comment{
|
|
||||||
return get[Comment](id)!
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,51 +0,0 @@
|
|||||||
module heromodels
|
|
||||||
|
|
||||||
import freeflowuniverse.herolib.core.redisclient
|
|
||||||
import freeflowuniverse.herolib.data.encoder
|
|
||||||
|
|
||||||
pub fn set[T](mut obj_ T) !u32 {
|
|
||||||
mut redis := redisclient.core_get()!
|
|
||||||
id := u32(redis.llen(db_name[T]()) or {0})
|
|
||||||
obj_.id = id
|
|
||||||
data := encoder.encode(obj_) or {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
redis.hset(db_name[T](),id.str(),data.bytestr())!
|
|
||||||
return id
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get[T](id u32) !T {
|
|
||||||
mut redis := redisclient.core_get()!
|
|
||||||
data := redis.hget(db_name[T](),id.str())!
|
|
||||||
t := T{}
|
|
||||||
return encoder.decode[T](data.bytes())!
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn exists[T](id u32) !bool {
|
|
||||||
mut redis := redisclient.core_get()!
|
|
||||||
return redis.hexists(db_name[T](),id.str())!
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn delete[T](id u32) ! {
|
|
||||||
mut redis := redisclient.core_get()!
|
|
||||||
redis.hdel(db_name[T](), id.str())!
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn list[T]() ![]T {
|
|
||||||
mut redis := redisclient.core_get()!
|
|
||||||
ids := redis.hkeys(db_name[T]())!
|
|
||||||
mut result := []T{}
|
|
||||||
for id in ids {
|
|
||||||
result << get[T](id.u32())!
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
// make it easy to get a base object
|
|
||||||
pub fn new_from_base[T](args BaseArgs) !Base {
|
|
||||||
return T { Base: new_base(args)! }
|
|
||||||
}
|
|
||||||
|
|
||||||
fn db_name[T]() string {
|
|
||||||
return "db:${T.name}"
|
|
||||||
}
|
|
||||||
@@ -1,93 +0,0 @@
|
|||||||
module heromodels
|
|
||||||
|
|
||||||
import crypto.md5
|
|
||||||
|
|
||||||
import freeflowuniverse.herolib.core.redisclient
|
|
||||||
import freeflowuniverse.herolib.data.ourtime
|
|
||||||
|
|
||||||
// Group represents a collection of users with roles and permissions
|
|
||||||
@[heap]
|
|
||||||
pub struct Base {
|
|
||||||
pub mut:
|
|
||||||
id u32
|
|
||||||
name string
|
|
||||||
description string
|
|
||||||
created_at i64
|
|
||||||
updated_at i64
|
|
||||||
securitypolicy u32
|
|
||||||
tags u32 //when we set/get we always do as []string but this can then be sorted and md5ed this gies the unique id of tags
|
|
||||||
comments []u32
|
|
||||||
}
|
|
||||||
|
|
||||||
@[heap]
|
|
||||||
pub struct SecurityPolicy {
|
|
||||||
pub mut:
|
|
||||||
id u32
|
|
||||||
read []u32 //links to users & groups
|
|
||||||
write []u32 //links to users & groups
|
|
||||||
delete []u32 //links to users & groups
|
|
||||||
public bool
|
|
||||||
md5 string //this sorts read, write and delete u32 + hash, then do md5 hash, this allows to go from a random read/write/delete/public config to a hash
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@[heap]
|
|
||||||
pub struct Tags {
|
|
||||||
pub mut:
|
|
||||||
id u32
|
|
||||||
names []string //unique per id
|
|
||||||
md5 string //of sorted names, to make easy to find unique id, each name lowercased and made ascii
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/////////////////
|
|
||||||
|
|
||||||
@[params]
|
|
||||||
pub struct BaseArgs {
|
|
||||||
pub mut:
|
|
||||||
id ?u32
|
|
||||||
name string
|
|
||||||
description string
|
|
||||||
securitypolicy ?u32
|
|
||||||
tags []string
|
|
||||||
comments []CommentArg
|
|
||||||
}
|
|
||||||
|
|
||||||
//make it easy to get a base object
|
|
||||||
pub fn new_base(args BaseArgs) !Base {
|
|
||||||
mut redis := redisclient.core_get()!
|
|
||||||
|
|
||||||
commentids:=comment_multiset(args.comments)!
|
|
||||||
tags:=tags2id(args.tags)!
|
|
||||||
|
|
||||||
return Base {
|
|
||||||
id: args.id or { 0 }
|
|
||||||
name: args.name
|
|
||||||
description: args.description
|
|
||||||
created_at: ourtime.now().unix()
|
|
||||||
updated_at: ourtime.now().unix()
|
|
||||||
securitypolicy: args.securitypolicy or { 0 }
|
|
||||||
tags: tags
|
|
||||||
comments: commentids
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn tags2id(tags []string) !u32 {
|
|
||||||
mut redis := redisclient.core_get()!
|
|
||||||
return if tags.len>0{
|
|
||||||
mut tags_fixed := tags.map(it.to_lower_ascii().trim_space()).filter(it != "")
|
|
||||||
tags_fixed.sort_ignore_case()
|
|
||||||
hash :=md5.hexhash(tags_fixed.join(","))
|
|
||||||
tags_found := redis.hget("db:tags", hash)!
|
|
||||||
return if tags_found == ""{
|
|
||||||
id := u32(redis.incr("db:tags:id")!)
|
|
||||||
redis.hset("db:tags", hash, id.str())!
|
|
||||||
redis.hset("db:tags", id.str(), tags_fixed.join(","))!
|
|
||||||
id
|
|
||||||
}else{
|
|
||||||
tags_found.u32()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
17
lib/hero/heromodels/factory.v
Normal file
17
lib/hero/heromodels/factory.v
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
module heromodels
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.hero.db
|
||||||
|
|
||||||
|
pub struct ModelsFactory {
|
||||||
|
pub mut:
|
||||||
|
comments DBComments
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new() !ModelsFactory {
|
||||||
|
mut mydb := db.new()!
|
||||||
|
return ModelsFactory{
|
||||||
|
comments: DBComments{
|
||||||
|
db: &mydb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user