feat: GET self owner and refactor self owner to remove circular dependency between user and owner
All checks were successful
/ test (push) Successful in 15m11s
All checks were successful
/ test (push) Successful in 15m11s
This commit is contained in:
parent
4225fcb36f
commit
08fe5949be
6 changed files with 41 additions and 16 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -10,7 +10,6 @@ pub struct Model {
|
|||
#[sea_orm(unique)]
|
||||
pub username: String,
|
||||
pub hashed_password: String,
|
||||
pub owner_id: Option<u32>,
|
||||
}
|
||||
|
||||
impl Model {
|
||||
|
|
|
|||
|
|
@ -163,6 +163,7 @@ pub async fn run_server(db: Arc<DatabaseConnection>, 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))
|
||||
|
|
|
|||
|
|
@ -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<Arc<AppState>>,
|
||||
claims: Claims,
|
||||
) -> (StatusCode, Json<Option<owner::Model>>) {
|
||||
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
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -59,11 +59,10 @@ pub async fn manage_users(db: Arc<DatabaseConnection>) {
|
|||
.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<DatabaseConnection>) {
|
|||
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 => {
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue