Files
herolib/lib/hero/heromodels/comment.v
2025-09-18 08:11:59 +02:00

187 lines
4.2 KiB
V

module heromodels
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
subject string
comment string
parent u32 // id of parent comment if any, 0 means none
author u32 // links to user
to []u32 // if comment/message has been sent to someone specifically
cc []u32 // like to but then for cc
}
//////////TO BE GENERATED BY AI////////////////////////////////
///BASIC CRUD FUNCTIONS
pub struct DBComments {
pub mut:
db &db.DB @[skip; str: skip]
}
@[params]
pub struct CommentListArg {
pub mut:
parent u32
author u32
limit int = 100 // Default limit is 100
}
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) 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.subject)
e.add_string(self.comment)
e.add_u32(self.parent)
e.add_u32(self.author)
e.add_list_u32(self.to)
e.add_list_u32(self.cc)
}
pub fn (mut self DBComments) load(mut o Comment, mut e encoder.Decoder) ! {
o.subject = e.get_string()!
o.comment = e.get_string()!
o.parent = e.get_u32()!
o.author = e.get_u32()!
o.to = e.get_list_u32()!
o.cc = e.get_list_u32()!
}
@[params]
pub struct CommentArg {
pub mut:
subject string
comment string @[required]
parent u32
author u32
to []u32
cc []u32
}
// get new comment, not from the DB
pub fn (mut self DBComments) new(args CommentArg) !Comment {
mut o := Comment{
subject: args.subject
comment: args.comment
parent: args.parent
author: args.author
to: args.to
cc: args.cc
updated_at: ourtime.now().unix()
}
return o
}
pub fn (mut self DBComments) set(o Comment) !Comment {
// Use db set function which returns the object with assigned ID
return self.db.set[Comment](o)!
}
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) 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(args CommentListArg) ![]Comment {
// Require at least one parameter to be provided
if args.parent == 0 && args.author == 0 {
return error('At least one filter parameter must be provided')
}
// Get all comments from the database
all_comments := self.db.list[Comment]()!.map(self.get(it)!)
// Apply filters
mut filtered_comments := []Comment{}
for comment in all_comments {
// Filter by parent if provided
if args.parent != 0 && comment.parent != args.parent {
continue
}
// Filter by author if provided
if args.author != 0 && comment.author != args.author {
continue
}
filtered_comments << comment
}
// Limit results to 100 or the specified limit
mut limit := args.limit
if limit > 100 {
limit = 100
}
if filtered_comments.len > limit {
return filtered_comments[..limit]
}
return filtered_comments
}