...
This commit is contained in:
@@ -19,7 +19,7 @@ pub mut:
|
|||||||
updated_at i64
|
updated_at i64
|
||||||
securitypolicy u32
|
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
|
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
|
return myid
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn [T] new(args BaseArgs) Base {
|
pub fn comments2id(comments []CommentArg) !u32 {
|
||||||
|
mut myid:=0
|
||||||
mut redis := redisclient.core_get()!
|
if comments.len>0{
|
||||||
|
mycomments:=comments.map(it.to_lower_ascii().trim_space()).sort().join(",")
|
||||||
redis.hget("db:comments")
|
mymd5:=crypto.hexhash(mycomments)
|
||||||
|
comments:=redis.hget("db:comments", mymd5)!
|
||||||
return T{
|
if comments == ""{
|
||||||
id: args.id or { 0 }
|
myid = u32(redis.incr("db:comments:id")!)
|
||||||
name: args.name
|
redis.hset("db:comments", mymd5, myid)!
|
||||||
description: args.description
|
redis.hset("db:comments", myid, mycomments)!
|
||||||
created_at: ourtime.now().unix()
|
}else{
|
||||||
updated_at: ourtime.now().unix()
|
myid = comments.int()
|
||||||
securitypolicy: args.securitypolicy or { 0 }
|
}
|
||||||
tags: args.tags
|
}
|
||||||
comments: args.comments.map(it.to_base())
|
return myid
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Convert CommentArg array to u32 array
|
||||||
|
mut comment_ids := []u32{}
|
||||||
|
for comment in args.comments {
|
||||||
|
comment_ids << comment_set(comment)!
|
||||||
}
|
}
|
||||||
}
|
|
||||||
@@ -12,7 +12,6 @@ pub struct CalendarEvent {
|
|||||||
Base
|
Base
|
||||||
pub mut:
|
pub mut:
|
||||||
title string
|
title string
|
||||||
description string
|
|
||||||
start_time i64 // Unix timestamp
|
start_time i64 // Unix timestamp
|
||||||
end_time i64 // Unix timestamp
|
end_time i64 // Unix timestamp
|
||||||
location string
|
location string
|
||||||
@@ -76,10 +75,9 @@ pub enum RecurrenceFreq {
|
|||||||
|
|
||||||
@[params]
|
@[params]
|
||||||
pub struct CalendarEventArgs {
|
pub struct CalendarEventArgs {
|
||||||
Base
|
BaseArgs
|
||||||
pub mut:
|
pub mut:
|
||||||
title string
|
title string
|
||||||
description string
|
|
||||||
start_time string // use ourtime module to go from string to epoch
|
start_time string // use ourtime module to go from string to epoch
|
||||||
end_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
|
location string
|
||||||
@@ -98,22 +96,17 @@ pub mut:
|
|||||||
|
|
||||||
pub fn calendar_event_new(args CalendarEventArgs) !CalendarEvent {
|
pub fn calendar_event_new(args CalendarEventArgs) !CalendarEvent {
|
||||||
// Convert tags to u32 ID
|
// 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{
|
return CalendarEvent{
|
||||||
// Base fields
|
// Base fields
|
||||||
id: args.Base.id or { 0 }
|
id: args.id or { 0 }
|
||||||
name: args.Base.name
|
name: args.name
|
||||||
description: args.Base.description
|
description: args.description
|
||||||
created_at: ourtime.now().unix()
|
created_at: ourtime.now().unix()
|
||||||
updated_at: ourtime.now().unix()
|
updated_at: ourtime.now().unix()
|
||||||
securitypolicy: args.Base.securitypolicy or { 0 }
|
securitypolicy: args.securitypolicy or { 0 }
|
||||||
tags: tags_id
|
tags: tags_id
|
||||||
comments: comment_ids
|
comments: comment_ids
|
||||||
|
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ pub mut:
|
|||||||
author u32 //links to user
|
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{
|
pub fn comment_new(args CommentArg) !Comment{
|
||||||
mut o:=Comment {
|
mut o:=Comment {
|
||||||
comment: args.comment
|
comment: args.comment
|
||||||
@@ -66,8 +66,18 @@ pub fn comment_new(args CommentArg) !Comment{
|
|||||||
return o
|
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 redis := redisclient.core_get()!
|
||||||
|
mut o:=comment_new(args)!
|
||||||
myid := redis.incr("db:comments:id")!
|
myid := redis.incr("db:comments:id")!
|
||||||
o.id = myid
|
o.id = myid
|
||||||
data := o.dump()!
|
data := o.dump()!
|
||||||
|
|||||||
71
lib/hero/heromodels/core_methods.v
Normal file
71
lib/hero/heromodels/core_methods.v
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user