2022-07-24 13:02:57 +03:00
|
|
|
module main
|
|
|
|
|
|
|
|
import crypto.bcrypt
|
|
|
|
import databases
|
|
|
|
|
2023-03-02 16:49:50 +03:00
|
|
|
fn (mut app App) service_add_user(username string, password string) !User {
|
2022-07-24 13:02:57 +03:00
|
|
|
mut db := databases.create_db_connection() or {
|
|
|
|
eprintln(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer {
|
2022-08-12 17:24:57 +03:00
|
|
|
db.close() or { panic(err) }
|
2022-07-24 13:02:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
sql db {
|
|
|
|
insert user_model into User
|
2023-01-15 04:39:51 +03:00
|
|
|
} or {
|
|
|
|
eprintln(err)
|
|
|
|
return err
|
2022-07-24 13:02:57 +03:00
|
|
|
}
|
|
|
|
|
2023-03-22 10:48:01 +03:00
|
|
|
users := sql db {
|
2022-07-24 13:02:57 +03:00
|
|
|
select from User where username == username limit 1
|
2023-04-04 08:23:06 +03:00
|
|
|
}!
|
2022-07-24 13:02:57 +03:00
|
|
|
|
2023-03-22 10:48:01 +03:00
|
|
|
return users.first()
|
2022-07-24 13:02:57 +03:00
|
|
|
}
|
|
|
|
|
2023-03-02 16:49:50 +03:00
|
|
|
fn (mut app App) service_get_user_by_id(user_id int) !User {
|
2022-07-24 13:02:57 +03:00
|
|
|
mut db := databases.create_db_connection() or {
|
|
|
|
println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer {
|
2022-08-12 17:24:57 +03:00
|
|
|
db.close() or { panic(err) }
|
2022-07-24 13:02:57 +03:00
|
|
|
}
|
|
|
|
|
2023-03-22 10:48:01 +03:00
|
|
|
users := sql db {
|
2022-07-24 13:02:57 +03:00
|
|
|
select from User where id == user_id
|
2023-04-04 08:23:06 +03:00
|
|
|
}!
|
2022-07-24 13:02:57 +03:00
|
|
|
|
2023-03-22 10:48:01 +03:00
|
|
|
return users.first()
|
2022-07-24 13:02:57 +03:00
|
|
|
}
|
|
|
|
|
2023-03-02 16:49:50 +03:00
|
|
|
fn (mut app App) service_get_all_user() ![]User {
|
2022-07-24 13:02:57 +03:00
|
|
|
mut db := databases.create_db_connection() or {
|
|
|
|
println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer {
|
2022-08-12 17:24:57 +03:00
|
|
|
db.close() or { panic(err) }
|
2022-07-24 13:02:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
results := sql db {
|
|
|
|
select from User
|
2023-04-04 08:23:06 +03:00
|
|
|
}!
|
2022-07-24 13:02:57 +03:00
|
|
|
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2023-03-02 16:49:50 +03:00
|
|
|
fn (mut app App) service_get_by_username(username string) !User {
|
2022-07-24 13:02:57 +03:00
|
|
|
mut db := databases.create_db_connection() or {
|
|
|
|
println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer {
|
2022-08-12 17:24:57 +03:00
|
|
|
db.close() or { panic(err) }
|
2022-07-24 13:02:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
results := sql db {
|
|
|
|
select from User where username == username
|
2023-04-04 08:23:06 +03:00
|
|
|
}!
|
2022-07-24 13:02:57 +03:00
|
|
|
|
|
|
|
if results.len == 0 {
|
2022-08-12 17:24:57 +03:00
|
|
|
return error('User not found')
|
2022-07-24 13:02:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return results[0]
|
|
|
|
}
|