...
This commit is contained in:
46
examples/hero/heromodels/heromodels_user.vsh
Normal file
46
examples/hero/heromodels/heromodels_user.vsh
Normal file
@@ -0,0 +1,46 @@
|
||||
#!/usr/bin/env -S v -n -w -cg -gc none -cc tcc -d use_openssl -enable-globals run
|
||||
|
||||
import freeflowuniverse.herolib.hero.heromodels
|
||||
|
||||
mut mydb := heromodels.new()!
|
||||
|
||||
// Create a new user
|
||||
mut o := mydb.user.new(
|
||||
name: 'John Doe'
|
||||
description: 'Software Developer'
|
||||
email: 'john.doe@example.com'
|
||||
public_key: '-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA...\n-----END PUBLIC KEY-----'
|
||||
phone: '+1234567890'
|
||||
address: '123 Main St, City, Country'
|
||||
avatar_url: 'https://example.com/avatar.jpg'
|
||||
bio: 'Experienced software developer with a passion for V language'
|
||||
timezone: 'UTC'
|
||||
status: .active
|
||||
securitypolicy: 0
|
||||
tags: 0
|
||||
comments: []
|
||||
)!
|
||||
|
||||
// Save to database
|
||||
oid := mydb.user.set(o)!
|
||||
println('Created User ID: ${oid}')
|
||||
|
||||
// Check if the user exists
|
||||
mut exists := mydb.user.exist(oid)!
|
||||
println('User exists: ${exists}')
|
||||
|
||||
// Retrieve from database
|
||||
mut o2 := mydb.user.get(oid)!
|
||||
println('Retrieved User object: ${o2}')
|
||||
|
||||
// List all users
|
||||
mut objects := mydb.user.list()!
|
||||
println('All users: ${objects}')
|
||||
|
||||
// Delete the user
|
||||
mydb.user.delete(oid)!
|
||||
println('Deleted user with ID: ${oid}')
|
||||
|
||||
// Check if the user still exists
|
||||
exists = mydb.user.exist(oid)!
|
||||
println('User exists after deletion: ${exists}')
|
||||
@@ -8,6 +8,7 @@ pub mut:
|
||||
calendar DBCalendar
|
||||
calendar_event DBCalendarEvent
|
||||
group DBGroup
|
||||
user DBUser
|
||||
}
|
||||
|
||||
pub fn new() !ModelsFactory {
|
||||
@@ -25,5 +26,8 @@ pub fn new() !ModelsFactory {
|
||||
group: DBGroup{
|
||||
db: &mydb
|
||||
}
|
||||
user: DBUser{
|
||||
db: &mydb
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
module heromodels
|
||||
|
||||
import time
|
||||
import crypto.blake3
|
||||
import json
|
||||
import freeflowuniverse.herolib.data.encoder
|
||||
import freeflowuniverse.herolib.data.ourtime
|
||||
import freeflowuniverse.herolib.hero.db
|
||||
|
||||
// User represents a person in the system
|
||||
@[heap]
|
||||
pub struct User {
|
||||
db.Base
|
||||
pub mut:
|
||||
id string // blake192 hash
|
||||
name string
|
||||
email string
|
||||
public_key string // for encryption/signing
|
||||
phone string
|
||||
@@ -17,8 +16,6 @@ pub mut:
|
||||
avatar_url string
|
||||
bio string
|
||||
timezone string
|
||||
created_at i64
|
||||
updated_at i64
|
||||
status UserStatus
|
||||
}
|
||||
|
||||
@@ -28,3 +25,99 @@ pub enum UserStatus {
|
||||
suspended
|
||||
pending
|
||||
}
|
||||
|
||||
pub fn (self User) type_name() string {
|
||||
return 'user'
|
||||
}
|
||||
|
||||
pub fn (self User) dump(mut e &encoder.Encoder) ! {
|
||||
e.add_string(self.email)
|
||||
e.add_string(self.public_key)
|
||||
e.add_string(self.phone)
|
||||
e.add_string(self.address)
|
||||
e.add_string(self.avatar_url)
|
||||
e.add_string(self.bio)
|
||||
e.add_string(self.timezone)
|
||||
e.add_u8(u8(self.status))
|
||||
}
|
||||
|
||||
fn (mut self DBUser) load(mut o User, mut e &encoder.Decoder) ! {
|
||||
o.email = e.get_string()!
|
||||
o.public_key = e.get_string()!
|
||||
o.phone = e.get_string()!
|
||||
o.address = e.get_string()!
|
||||
o.avatar_url = e.get_string()!
|
||||
o.bio = e.get_string()!
|
||||
o.timezone = e.get_string()!
|
||||
o.status = unsafe { UserStatus(e.get_u8()!) }
|
||||
}
|
||||
|
||||
@[params]
|
||||
pub struct UserArg {
|
||||
pub mut:
|
||||
name string @[required]
|
||||
description string
|
||||
email string
|
||||
public_key string // for encryption/signing
|
||||
phone string
|
||||
address string
|
||||
avatar_url string
|
||||
bio string
|
||||
timezone string
|
||||
status UserStatus
|
||||
securitypolicy u32
|
||||
tags u32
|
||||
comments []u32
|
||||
}
|
||||
|
||||
pub struct DBUser {
|
||||
pub mut:
|
||||
db &db.DB @[skip; str: skip]
|
||||
}
|
||||
|
||||
// get new user, not from the DB
|
||||
pub fn (mut self DBUser) new(args UserArg) !User {
|
||||
mut o := User{
|
||||
email: args.email
|
||||
public_key: args.public_key
|
||||
phone: args.phone
|
||||
address: args.address
|
||||
avatar_url: args.avatar_url
|
||||
bio: args.bio
|
||||
timezone: args.timezone
|
||||
status: args.status
|
||||
}
|
||||
|
||||
// Set base fields
|
||||
o.name = args.name
|
||||
o.description = args.description
|
||||
o.securitypolicy = args.securitypolicy
|
||||
o.tags = args.tags
|
||||
o.comments = args.comments
|
||||
o.updated_at = ourtime.now().unix()
|
||||
|
||||
return o
|
||||
}
|
||||
|
||||
pub fn (mut self DBUser) set(o User) !u32 {
|
||||
return self.db.set[User](o)!
|
||||
}
|
||||
|
||||
pub fn (mut self DBUser) delete(id u32) ! {
|
||||
self.db.delete[User](id)!
|
||||
}
|
||||
|
||||
pub fn (mut self DBUser) exist(id u32) !bool {
|
||||
return self.db.exists[User](id)!
|
||||
}
|
||||
|
||||
pub fn (mut self DBUser) get(id u32) !User {
|
||||
mut o, data := self.db.get_data[User](id)!
|
||||
mut e_decoder := encoder.decoder_new(data)
|
||||
self.load(mut o, mut e_decoder)!
|
||||
return o
|
||||
}
|
||||
|
||||
pub fn (mut self DBUser) list() ![]User {
|
||||
return self.db.list[User]()!.map(self.get(it)!)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user