This commit is contained in:
2025-08-31 13:11:43 +02:00
parent 2c7eaa4f5d
commit f9717f8f5d
4 changed files with 112 additions and 32 deletions

View File

@@ -76,20 +76,26 @@ pub fn tags2id(tags []string) !u32 {
return myid
}
pub fn [T] new(args BaseArgs) Base {
mut redis := redisclient.core_get()!
redis.hget("db:comments")
return T{
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: args.tags
comments: args.comments.map(it.to_base())
pub fn comments2id(comments []CommentArg) !u32 {
mut myid:=0
if comments.len>0{
mycomments:=comments.map(it.to_lower_ascii().trim_space()).sort().join(",")
mymd5:=crypto.hexhash(mycomments)
comments:=redis.hget("db:comments", mymd5)!
if comments == ""{
myid = u32(redis.incr("db:comments:id")!)
redis.hset("db:comments", mymd5, myid)!
redis.hset("db:comments", myid, mycomments)!
}else{
myid = comments.int()
}
}
return myid
}
// Convert CommentArg array to u32 array
mut comment_ids := []u32{}
for comment in args.comments {
comment_ids << comment_set(comment)!
}

View File

@@ -12,7 +12,6 @@ pub struct CalendarEvent {
Base
pub mut:
title string
description string
start_time i64 // Unix timestamp
end_time i64 // Unix timestamp
location string
@@ -76,10 +75,9 @@ pub enum RecurrenceFreq {
@[params]
pub struct CalendarEventArgs {
Base
BaseArgs
pub mut:
title string
description string
start_time string // use ourtime module to go from string to epoch
end_time string // use ourtime module to go from string to epoch
location string
@@ -98,22 +96,17 @@ pub mut:
pub fn calendar_event_new(args CalendarEventArgs) !CalendarEvent {
// Convert tags to u32 ID
tags_id := tags2id(args.Base.tags)!
tags_id := tags2id(args.tags)!
// Convert CommentArg array to u32 array
mut comment_ids := []u32{}
for comment in args.comments {
comment_ids << comment_set(comment)!
}
return CalendarEvent{
// Base fields
id: args.Base.id or { 0 }
name: args.Base.name
description: args.Base.description
id: args.id or { 0 }
name: args.name
description: args.description
created_at: ourtime.now().unix()
updated_at: ourtime.now().unix()
securitypolicy: args.Base.securitypolicy or { 0 }
securitypolicy: args.securitypolicy or { 0 }
tags: tags_id
comments: comment_ids

View File

@@ -55,7 +55,7 @@ pub mut:
author u32 //links to user
}
//get new comment, not our of db
//get new comment, not from the DB
pub fn comment_new(args CommentArg) !Comment{
mut o:=Comment {
comment: args.comment
@@ -66,8 +66,18 @@ pub fn comment_new(args CommentArg) !Comment{
return o
}
pub fn comment_set(mut o Comment) !u32{
pub fn comment_multiset(args []CommentArg) ![]u32{
mut ids := []u32{}
for comment in args {
ids << comment_set(comment)!
}
return ids
}
pub fn comment_set(args CommentArg) !u32{
mut redis := redisclient.core_get()!
mut o:=comment_new(args)!
myid := redis.incr("db:comments:id")!
o.id = myid
data := o.dump()!

View File

@@ -0,0 +1,71 @@
module heromodels
import crypto.md5
import json
import freeflowuniverse.herolib.core.redisclient
import freeflowuniverse.herolib.data.encoder
pub fn [T] set(obj T) !Base {
//todo: get the dump() from the obj , save the
mut redis := redisclient.core_get()!
data := obj.dump()
redis.hset("db:${name}",id,data)!
}
pub fn [T] get(id u32) !T {
//todo: get the dump() from the obj , save the
mut redis := redisclient.core_get()!
data := redis.hget("db:${name}",id)!
obj:=$name_load(data) or {
return error("could not load ${name} from data")
}
return obj
}
pub fn [T] exists(id u32) !T {
//todo: get the dump() from the obj , save the
mut redis := redisclient.core_get()!
return redis.hexists("db:${name}",id)!
return obj
}
pub fn [T] delete(id u32) !T {
//todo: get the dump() from the obj , save the
mut redis := redisclient.core_get()!
return redis.hdel("db:${name}",id)!
return obj
}
//make it easy to get a base object
pub fn [T] new_from_base(args BaseArgs) !T {
mut redis := redisclient.core_get()!
commentids:=comment_multiset(args.comments)!
tags:=tags2id(args.tags)!
return T{
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)
}
}