This commit is contained in:
Mahmoud-Emad
2025-09-15 15:49:23 +03:00
parent 6d67dbe2d7
commit 23f7e05931
5 changed files with 138 additions and 129 deletions

View File

@@ -32,7 +32,7 @@ o.tags = mydb.calendar_event.db.tags_get(['work', 'meeting', 'team'])!
// o.comments = mydb.calendar_event.db.comments_get([CommentArg{comment: 'This is a comment'}])!
mydb.calendar_event.set(mut o)!
// mut o2 := mydb.calendar_event.get(o.Base.id)!
mut o2 := mydb.calendar_event.get(o.id)!
// println('Calendar Event ID: ${o.Base.id}')
// println('Calendar Event object: ${o.Base.id}')
println('Calendar Event ID: ${o.id}')
println('Calendar Event object: ${o2}')

View File

@@ -4,17 +4,17 @@ import freeflowuniverse.herolib.core.redisclient
import freeflowuniverse.herolib.hero.heromodels
mut mydb := heromodels.new()!
// mydb.comments.db.redis.flushdb()!
mydb.comments.db.redis.flushdb()!
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)!
mydb.comments.set(mut o)!
mut o2 := mydb.comments.get(o.id)!
println(oid)
println(o2)
println('Comment ID: ${o.id}')
println('Comment object: ${o2}')
mut objects := mydb.comments.list()!
println(objects)
// mut objects := mydb.comments.list()!
// println(objects)

View File

@@ -2,6 +2,7 @@ module db
import freeflowuniverse.herolib.data.ourtime
import freeflowuniverse.herolib.data.encoder
import time
pub fn (mut self DB) set[T](mut obj T) ! {
// Get the next ID
@@ -28,7 +29,15 @@ pub fn (mut self DB) set[T](mut obj T) ! {
for comment in obj.comments {
e.add_u32(comment)
}
obj.dump(mut e)!
// $for method in T.methods {
// $if method.name == 'dump' {
// println(method)
// method.name(mut e)!
// }
// }
self.redis.hset(self.db_name[T](), obj.id.str(), e.data.bytestr())!
}

View File

@@ -1,130 +1,130 @@
module heromodels
// import freeflowuniverse.herolib.data.encoder
// import freeflowuniverse.herolib.data.ourtime
// import freeflowuniverse.herolib.hero.db
import freeflowuniverse.herolib.data.encoder
import freeflowuniverse.herolib.data.ourtime
import freeflowuniverse.herolib.hero.db
// @[heap]
// pub struct Comment {
// db.Base
// pub mut:
// // id u32
// comment string
// parent u32 // id of parent comment if any, 0 means none
// author u32 // links to user
// }
@[heap]
pub struct Comment {
db.Base
pub mut:
// id u32
comment string
parent u32 // id of parent comment if any, 0 means none
author u32 // links to user
}
// //////////TO BE GENERATED BY AI////////////////////////////////
// ///BASIC CRUD FUNCTIONS
//////////TO BE GENERATED BY AI////////////////////////////////
///BASIC CRUD FUNCTIONS
// pub struct DBComments {
// pub mut:
// db &db.DB @[skip; str: skip]
// }
pub struct DBComments {
pub mut:
db &db.DB @[skip; str: skip]
}
// pub fn (self Comment) type_name() string {
// return 'comments'
// }
pub fn (self Comment) type_name() string {
return 'comments'
}
// // return example rpc call and result for each methodname
// pub fn (self Comment) description(methodname string) string {
// match methodname {
// 'set' {
// return 'Create or update a comment. Returns the ID of the comment.'
// }
// 'get' {
// return 'Retrieve a comment by ID. Returns the comment object.'
// }
// 'delete' {
// return 'Delete a comment by ID. Returns true if successful.'
// }
// 'exist' {
// return 'Check if a comment exists by ID. Returns true or false.'
// }
// 'list' {
// return 'List all comments. Returns an array of comment objects.'
// }
// else {
// return 'This is generic method for the root object, TODO fill in, ...'
// }
// }
// }
// return example rpc call and result for each methodname
pub fn (self Comment) description(methodname string) string {
match methodname {
'set' {
return 'Create or update a comment. Returns the ID of the comment.'
}
'get' {
return 'Retrieve a comment by ID. Returns the comment object.'
}
'delete' {
return 'Delete a comment by ID. Returns true if successful.'
}
'exist' {
return 'Check if a comment exists by ID. Returns true or false.'
}
'list' {
return 'List all comments. Returns an array of comment objects.'
}
else {
return 'This is generic method for the root object, TODO fill in, ...'
}
}
}
// // return example rpc call and result for each methodname
// pub fn (self Comment) example(methodname string) (string, string) {
// match methodname {
// 'set' {
// return '{"comment": {"comment": "This is a test comment.", "parent": 0, "author": 1}}', '1'
// }
// 'get' {
// return '{"id": 1}', '{"comment": "This is a test comment.", "parent": 0, "author": 1}'
// }
// 'delete' {
// return '{"id": 1}', 'true'
// }
// 'exist' {
// return '{"id": 1}', 'true'
// }
// 'list' {
// return '{}', '[{"comment": "This is a test comment.", "parent": 0, "author": 1}]'
// }
// else {
// return '{}', '{}'
// }
// }
// }
// return example rpc call and result for each methodname
pub fn (self Comment) example(methodname string) (string, string) {
match methodname {
'set' {
return '{"comment": {"comment": "This is a test comment.", "parent": 0, "author": 1}}', '1'
}
'get' {
return '{"id": 1}', '{"comment": "This is a test comment.", "parent": 0, "author": 1}'
}
'delete' {
return '{"id": 1}', 'true'
}
'exist' {
return '{"id": 1}', 'true'
}
'list' {
return '{}', '[{"comment": "This is a test comment.", "parent": 0, "author": 1}]'
}
else {
return '{}', '{}'
}
}
}
// pub fn (self Comment) dump(mut e encoder.Encoder) ! {
// // e.add_string(self.comment)
// // e.add_u32(self.parent)
// // e.add_u32(self.author)
// }
pub fn (self Comment) dump(mut e encoder.Encoder) ! {
e.add_string(self.comment)
e.add_u32(self.parent)
e.add_u32(self.author)
}
// fn (mut self DBComments) load(mut o Comment, mut e encoder.Decoder) ! {
// // o.comment = e.get_string()!
// // o.parent = e.get_u32()!
// // o.author = e.get_u32()!
// }
fn (mut self DBComments) load(mut o Comment, mut e encoder.Decoder) ! {
o.comment = e.get_string()!
o.parent = e.get_u32()!
o.author = e.get_u32()!
}
// @[params]
// pub struct CommentArg {
// pub mut:
// comment string @[required]
// parent u32
// author u32
// }
@[params]
pub struct CommentArg {
pub mut:
comment string @[required]
parent u32
author u32
}
// // get new comment, not from the DB
// 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
// }
// get new comment, not from the DB
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 (mut self DBComments) set(mut o Comment) ! {
// // Use openrpcserver set function which now returns the ID
// self.db.set[Comment](mut o)!
// }
pub fn (mut self DBComments) set(mut o Comment) ! {
// Use openrpcserver set function which now returns the ID
self.db.set[Comment](mut o)!
}
// pub fn (mut self DBComments) delete(id u32) ! {
// self.db.delete[Comment](id)!
// }
pub fn (mut self DBComments) delete(id u32) ! {
self.db.delete[Comment](id)!
}
// pub fn (mut self DBComments) exist(id u32) !bool {
// return self.db.exists[Comment](id)!
// }
pub fn (mut self DBComments) exist(id u32) !bool {
return self.db.exists[Comment](id)!
}
// pub fn (mut self DBComments) get(id u32) !Comment {
// mut o, data := self.db.get_data[Comment](id)!
// mut e_decoder := encoder.decoder_new(data)
// self.load(mut o, mut e_decoder)!
// return o
// }
pub fn (mut self DBComments) get(id u32) !Comment {
mut o, data := self.db.get_data[Comment](id)!
mut e_decoder := encoder.decoder_new(data)
self.load(mut o, mut e_decoder)!
return o
}
// pub fn (mut self DBComments) list() ![]Comment {
// return self.db.list[Comment]()!.map(self.get(it)!)
// }
pub fn (mut self DBComments) list() ![]Comment {
return self.db.list[Comment]()!.map(self.get(it)!)
}

View File

@@ -4,8 +4,8 @@ import freeflowuniverse.herolib.hero.db
pub struct ModelsFactory {
pub mut:
// comments DBComments
calendar DBCalendar
comments DBComments
// calendar DBCalendar
// calendar_event DBCalendarEvent
// group DBGroup
// user DBUser
@@ -18,12 +18,12 @@ pub mut:
pub fn new() !ModelsFactory {
mut mydb := db.new()!
return ModelsFactory{
// comments: DBComments{
// db: &mydb
// }
calendar: DBCalendar{
comments: DBComments{
db: &mydb
}
// calendar: DBCalendar{
// db: &mydb
// }
// calendar_event: DBCalendarEvent{
// db: &mydb
// }