- fix: issue where login failed to return the user if their role was null

- add: register route for api-service
- update: spawn a thread for the rest api in the api service
This commit is contained in:
2024-12-17 04:06:56 -05:00
parent 52455d6ffc
commit c67cdd5b2a
8 changed files with 74 additions and 23 deletions

View File

@@ -20,7 +20,7 @@ impl UserService for MyDatabaseService {
username: user.username,
email: user.email,
hashed_password: user.hashed_password,
roles: user.roles,
roles: user.roles.unwrap_or_else(Vec::new),
}))
}
@@ -30,7 +30,7 @@ impl UserService for MyDatabaseService {
) -> Result<Response<CreateUserResponse>, Status> {
let req = request.into_inner();
let user_id = self.db.user_repo.create_user(&req.username, &req.email, &req.hashed_password, &[])
let user_id = self.db.user_repo.create_user(&req.username, &req.email, &req.hashed_password)
.await
.map_err(|_| Status::internal("Failed to create user"))?;
@@ -53,7 +53,7 @@ impl UserService for MyDatabaseService {
username: user.username,
email: user.email,
hashed_password: user.hashed_password,
roles: user.roles,
roles: user.roles.unwrap_or_else(Vec::new),
}))
}
@@ -72,7 +72,7 @@ impl UserService for MyDatabaseService {
username: user.username,
email: user.email,
hashed_password: user.hashed_password,
roles: user.roles,
roles: user.roles.unwrap_or_else(Vec::new),
}))
}
}

View File

@@ -3,6 +3,7 @@ use crate::redis_cache::{RedisCache, Cache}; // Import RedisCache and Cache Trai
use serde::{Serialize, Deserialize};
use std::sync::Arc;
use tokio::sync::Mutex;
use tracing::{debug};
#[derive(Debug, FromRow, Serialize, Deserialize)]
pub struct User {
@@ -10,7 +11,7 @@ pub struct User {
pub username: String,
pub email: String,
pub hashed_password: String,
pub roles: Vec<String>,
pub roles: Option<Vec<String>>,
pub created_at: chrono::NaiveDateTime,
pub updated_at: chrono::NaiveDateTime,
}
@@ -79,19 +80,21 @@ impl UserRepository {
Ok(user)
}
pub async fn create_user(&self, username: &str, email: &str, hashed_password: &str, roles: &[String]) -> Result<i32, sqlx::Error> {
let row = sqlx::query(
"INSERT INTO users (username, email, hashed_password, roles, created_at, updated_at) \
VALUES ($1, $2, $3, $4, NOW(), NOW()) RETURNING id",
pub async fn create_user(&self, username: &str, email: &str, hashed_password: &str) -> Result<i32, sqlx::Error> {
let result = sqlx::query!(
r#"
INSERT INTO users (username, email, hashed_password)
VALUES ($1, $2, $3)
RETURNING id
"#,
username,
email,
hashed_password
)
.bind(username)
.bind(email)
.bind(hashed_password)
.bind(roles)
.fetch_one(&self.pool)
.await?;
Ok(row.get("id"))
Ok(result.id)
}
pub async fn update_user_email(&self, user_id: i32, new_email: &str) -> Result<(), sqlx::Error> {