From 08fe5949beb0513cfebf0c62cfa780ab98375600 Mon Sep 17 00:00:00 2001 From: Ninjdai Date: Thu, 14 Aug 2025 12:29:47 +0200 Subject: [PATCH] feat: GET self owner and refactor self owner to remove circular dependency between user and owner --- src/entities/owner.rs | 3 ++- src/entities/user.rs | 1 - src/lib.rs | 1 + src/routes/owner.rs | 30 +++++++++++++++++++++++++++++- src/utils/cli.rs | 12 +++++------- tests/common/mod.rs | 10 ++++------ 6 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/entities/owner.rs b/src/entities/owner.rs index a95fedb..ec8300f 100644 --- a/src/entities/owner.rs +++ b/src/entities/owner.rs @@ -10,7 +10,8 @@ pub struct Model { pub user_id: u32, pub first_name: String, pub last_name: String, - pub contact: String + pub contact: String, + pub user: bool } impl Entity { diff --git a/src/entities/user.rs b/src/entities/user.rs index 4efcba9..f0b2ad0 100644 --- a/src/entities/user.rs +++ b/src/entities/user.rs @@ -10,7 +10,6 @@ pub struct Model { #[sea_orm(unique)] pub username: String, pub hashed_password: String, - pub owner_id: Option, } impl Model { diff --git a/src/lib.rs b/src/lib.rs index 42da2bc..3a5636b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -163,6 +163,7 @@ pub async fn run_server(db: Arc, port: u16, serve_docs: bool .routes(routes!(routes::book_instance::search_bal_book_instances)) // Owner API .routes(routes!(routes::owner::get_owner_by_id)) + .routes(routes!(routes::owner::get_self_owner)) .routes(routes!(routes::owner::create_owner)) .routes(routes!(routes::owner::update_owner)) .routes(routes!(routes::owner::get_owners)) diff --git a/src/routes/owner.rs b/src/routes/owner.rs index 2957af1..91e09a9 100644 --- a/src/routes/owner.rs +++ b/src/routes/owner.rs @@ -6,7 +6,7 @@ use sea_orm::{ActiveModelTrait, ActiveValue::{NotSet, Set}, ColumnTrait, EntityT use serde::{Deserialize, Serialize}; use utoipa::IntoParams; -use crate::{entities::{owner, prelude::Owner}, routes::auth::Claims, utils::events::{Event, WebsocketMessage}, AppState}; +use crate::{entities::{owner, prelude::*}, routes::auth::Claims, utils::events::{Event, WebsocketMessage}, AppState}; #[derive(IntoParams)] @@ -45,6 +45,33 @@ pub async fn get_owner_by_id( } } +#[axum::debug_handler] +#[utoipa::path( + get, + path = "/owner/self", + security(("jwt" = [])), + responses( + (status = OK, body = owner::Model, description = "Found your corresponding owner"), + ), + summary = "Get your own owner", + description = "Get your own owner", + tag = "owner-api", +)] +pub async fn get_self_owner( + State(state): State>, + claims: Claims, +) -> (StatusCode, Json>) { + if let Ok(Some(res)) =Owner::find() + .filter(owner::Column::User.eq(true)) + .filter(owner::Column::UserId.eq(claims.user_id)) + .one(state.db_conn.as_ref()).await + { + (StatusCode::OK, Json(Some(res))) + } else { + (StatusCode::INTERNAL_SERVER_ERROR, Json(None)) + } +} + #[derive(Deserialize, Serialize, utoipa::ToSchema)] pub struct OwnerCreateParams { first_name: String, @@ -76,6 +103,7 @@ pub async fn create_owner( last_name: Set(instance_payload.last_name), contact: Set(instance_payload.contact), user_id: Set(claims.user_id), + user: Set(false), id: NotSet }; diff --git a/src/utils/cli.rs b/src/utils/cli.rs index 483067f..84e1019 100644 --- a/src/utils/cli.rs +++ b/src/utils/cli.rs @@ -59,11 +59,10 @@ pub async fn manage_users(db: Arc) { .with_validator(min_length!(10)) .with_display_mode(inquire::PasswordDisplayMode::Masked) .prompt().unwrap(); - let mut new_user = user::ActiveModel { + let new_user = user::ActiveModel { id: NotSet, username: Set(username), - hashed_password: Set(hash_password(password)), - owner_id: Set(None) + hashed_password: Set(hash_password(password)) }; let res = new_user.clone().insert(db.as_ref()).await.unwrap(); @@ -76,11 +75,10 @@ pub async fn manage_users(db: Arc) { user_id: Set(res.id), first_name: Set(first_name), last_name: Set(last_name), - contact: Set(contact) + contact: Set(contact), + user: Set(true) }; - let owner_res = new_owner.insert(db.as_ref()).await.unwrap(); - new_user.owner_id = Set(Some(owner_res.id)); - let _ = new_user.update(db.as_ref()); + let _ = new_owner.insert(db.as_ref()).await.unwrap(); } }, Action::Delete => { diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 02e24a7..184727e 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -63,11 +63,10 @@ where C: ConnectionTrait { panic!("Username {username} already in use"); } else { let password = password_t.to_string(); - let mut new_user = user::ActiveModel { + let new_user = user::ActiveModel { id: NotSet, username: Set(username.clone()), hashed_password: Set(hash_password(password)), - owner_id: Set(None) }; let res = new_user.clone().insert(db_conn).await.unwrap(); @@ -76,10 +75,9 @@ where C: ConnectionTrait { user_id: Set(res.id), first_name: Set(format!("{username} first name")), last_name: Set(format!("{username} last name")), - contact: Set(format!("{username}@mail.com")) + contact: Set(format!("{username}@mail.com")), + user: Set(true) }; - let owner_res = new_owner.insert(db_conn).await.unwrap(); - new_user.owner_id = Set(Some(owner_res.id)); - let _ = new_user.update(db_conn); + let _ = new_owner.insert(db_conn).await.unwrap(); } }