2023-01-06 04:36:42 +03:00
|
|
|
module main
|
|
|
|
|
|
|
|
import crypto.bcrypt
|
|
|
|
import databases
|
|
|
|
|
|
|
|
fn (mut app App) service_add_user(username string, password string) ! {
|
|
|
|
mut db := databases.create_db_connection()!
|
|
|
|
|
|
|
|
defer {
|
|
|
|
db.close() or { panic(err) }
|
|
|
|
}
|
|
|
|
|
|
|
|
hashed_password := bcrypt.generate_from_password(password.bytes(), bcrypt.min_cost) or {
|
|
|
|
eprintln(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
user_model := User{
|
|
|
|
username: username
|
|
|
|
password: hashed_password
|
|
|
|
active: true
|
|
|
|
}
|
|
|
|
|
|
|
|
mut insert_error := ''
|
|
|
|
sql db {
|
|
|
|
insert user_model into User
|
|
|
|
} or { insert_error = err.msg() }
|
|
|
|
if insert_error != '' {
|
|
|
|
return error(insert_error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-02 16:49:50 +03:00
|
|
|
fn (mut app App) service_get_all_user() ![]User {
|
2023-01-06 04:36:42 +03:00
|
|
|
mut db := databases.create_db_connection() or {
|
|
|
|
println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer {
|
|
|
|
db.close() or { panic(err) }
|
|
|
|
}
|
|
|
|
|
|
|
|
results := sql db {
|
|
|
|
select from User
|
2023-04-04 08:23:06 +03:00
|
|
|
}!
|
2023-01-06 04:36:42 +03:00
|
|
|
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2023-03-02 16:49:50 +03:00
|
|
|
fn (mut app App) service_get_user(id int) !User {
|
2023-01-06 04:36:42 +03:00
|
|
|
mut db := databases.create_db_connection() or {
|
|
|
|
println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer {
|
|
|
|
db.close() or { panic(err) }
|
|
|
|
}
|
|
|
|
|
|
|
|
results := sql db {
|
|
|
|
select from User where id == id
|
2023-04-04 08:23:06 +03:00
|
|
|
}!
|
2023-01-06 04:36:42 +03:00
|
|
|
|
2023-03-22 10:48:01 +03:00
|
|
|
return results.first()
|
2023-01-06 04:36:42 +03:00
|
|
|
}
|