quokka_admin/service/
user_service.rs1use quokka::{helper::database::BaseRepository, state::FromState};
2
3use crate::entity::user::{User, UserRepository};
4
5#[derive(Clone, FromState)]
6pub struct UserService {
7 user_repository: UserRepository,
8}
9
10pub struct UserRegistration {
11 pub username: String,
12 pub password: String,
13 pub email: String,
14}
15
16impl UserService {
17 pub async fn register_user(
18 &self,
19 UserRegistration {
20 username,
21 mut password,
22 email,
23 }: UserRegistration,
24 ) -> quokka::Result<User> {
25 use argon2::PasswordHasher;
26 let salt = argon2::password_hash::SaltString::generate(
27 &mut argon2::password_hash::rand_core::OsRng,
28 );
29 password = argon2::Argon2::default()
30 .hash_password(password.as_bytes(), &salt)
31 .map_err(quokka::Error::wrap_error("Unable to hash password", 500))?
32 .to_string();
33
34 self.user_repository
35 .create_entity(User {
36 id: -1,
37 username,
38 password,
39 email,
40 created_at: time::OffsetDateTime::now_utc(),
41 updated_at: time::OffsetDateTime::now_utc(),
42 })
43 .await
44 }
45}