From f9717f8f5d059b0834f072d631a2617673cbdb5f Mon Sep 17 00:00:00 2001 From: despiegk Date: Sun, 31 Aug 2025 13:11:43 +0200 Subject: [PATCH] ... --- lib/hero/heromodels/base.v | 40 +++++++++------- lib/hero/heromodels/calendar_event.v | 19 +++----- lib/hero/heromodels/comment.v | 14 +++++- lib/hero/heromodels/core_methods.v | 71 ++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 32 deletions(-) create mode 100644 lib/hero/heromodels/core_methods.v diff --git a/lib/hero/heromodels/base.v b/lib/hero/heromodels/base.v index 6e778953..f2c42e78 100644 --- a/lib/hero/heromodels/base.v +++ b/lib/hero/heromodels/base.v @@ -19,7 +19,7 @@ pub mut: 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 + comments []u32 } @@ -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)! } -} \ No newline at end of file diff --git a/lib/hero/heromodels/calendar_event.v b/lib/hero/heromodels/calendar_event.v index d8c3c2ef..7dc22f14 100644 --- a/lib/hero/heromodels/calendar_event.v +++ b/lib/hero/heromodels/calendar_event.v @@ -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 diff --git a/lib/hero/heromodels/comment.v b/lib/hero/heromodels/comment.v index 360d082e..54938eb8 100644 --- a/lib/hero/heromodels/comment.v +++ b/lib/hero/heromodels/comment.v @@ -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()! diff --git a/lib/hero/heromodels/core_methods.v b/lib/hero/heromodels/core_methods.v new file mode 100644 index 00000000..d08dcf90 --- /dev/null +++ b/lib/hero/heromodels/core_methods.v @@ -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) + } +} \ No newline at end of file