diff --git a/src/entities/user.rs b/src/entities/user.rs index 25652fc..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 current_bal_id: Option, } impl Model { diff --git a/src/main.rs b/src/main.rs index 4229796..dd7b8c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -195,8 +195,6 @@ async fn run_server(db: Arc, port: u16) { .routes(routes!(routes::bal::create_bal)) .routes(routes!(routes::bal::update_bal)) .routes(routes!(routes::bal::get_bals)) - .routes(routes!(routes::bal::get_current_bal)) - .routes(routes!(routes::bal::set_current_bal)) // Authentication .route_layer(middleware::from_fn_with_state(shared_state.clone(), routes::auth::auth_middleware)) .routes(routes!(routes::auth::auth)) diff --git a/src/routes/bal.rs b/src/routes/bal.rs index c59402c..ceb9b69 100644 --- a/src/routes/bal.rs +++ b/src/routes/bal.rs @@ -2,11 +2,11 @@ use std::sync::Arc; use axum::{extract::{Path, State}, Json}; use reqwest::{StatusCode}; -use sea_orm::{ActiveModelTrait, ActiveValue::{NotSet, Set}, ColumnTrait, EntityTrait, IntoActiveModel, QueryFilter, TryIntoModel}; +use sea_orm::{ActiveModelTrait, ActiveValue::{NotSet, Set}, ColumnTrait, EntityTrait, QueryFilter, TryIntoModel}; use serde::{Deserialize, Serialize}; use utoipa::IntoParams; -use crate::{entities::{bal, prelude::*, user}, routes::auth::Claims, utils::auth::user_is_bal_owner, AppState}; +use crate::{entities::{bal, prelude::*}, routes::auth::Claims, AppState}; #[derive(IntoParams)] @@ -159,68 +159,3 @@ pub async fn get_bals( (StatusCode::NOT_FOUND, Json(vec![])) } } - -#[axum::debug_handler] -#[utoipa::path( - get, - path = "/bal/current", - security(("jwt" = [])), - responses( - (status = OK, body = bal::Model, description = "Your current active BAL"), - (status = NOT_FOUND, description = "You don't have a currently active BAL"), - ), - summary = "Get your current active BAL", - description = "Get your current active BAL", - tag = "bal-api", -)] -pub async fn get_current_bal( - State(state): State>, - claims: Claims, -) -> (StatusCode, Json>) { - if let Ok(Some(user)) = User::find_by_id(claims.user_id).one(state.db_conn.as_ref()).await { - if let Some(bal_id) = user.current_bal_id { - (StatusCode::OK, Json(Some(Bal::find_by_id(bal_id).one(state.db_conn.as_ref()).await.unwrap().unwrap()))) - } else { - (StatusCode::NOT_FOUND, Json(None)) - } - } else { - (StatusCode::INTERNAL_SERVER_ERROR, Json(None)) - } -} - -#[derive(Deserialize, utoipa::ToSchema)] -pub struct BalIdParams{ - id: u32, -} - -#[axum::debug_handler] -#[utoipa::path( - post, - path = "/bal/current", - request_body = BalIdParams, - security(("jwt" = [])), - responses( - (status = OK, description = "Successfully set current active BAL"), - (status = UNAUTHORIZED, description = "Tried to set a BAL you don't own as your active BAL"), - ), - summary = "Set your current active BAL", - description = "Set your current active BAL", - tag = "bal-api", -)] -pub async fn set_current_bal( - State(state): State>, - claims: Claims, - Json(payload): Json, -) -> StatusCode { - if !user_is_bal_owner(claims.user_id, payload.id, state.db_conn.as_ref()).await { - return StatusCode::UNAUTHORIZED; - } - if let Ok(Some(user)) = User::find_by_id(claims.user_id).one(state.db_conn.as_ref()).await { - let mut user_active_model: user::ActiveModel = user.into_active_model(); - user_active_model.current_bal_id = Set(Some(payload.id)); - let _ = User::update(user_active_model).exec(state.db_conn.as_ref()).await; - StatusCode::OK - } else { - StatusCode::INTERNAL_SERVER_ERROR - } -} diff --git a/src/routes/book_instance.rs b/src/routes/book_instance.rs index 3fc3e9b..f7cec31 100644 --- a/src/routes/book_instance.rs +++ b/src/routes/book_instance.rs @@ -332,8 +332,7 @@ pub async fn get_bal_book_instances_by_ean( #[derive(Deserialize, Serialize, utoipa::ToSchema)] pub struct BookInstanceSearchParams { - title: Option, - author: Option, + title: String, } #[derive(Serialize, utoipa::ToSchema)] @@ -361,24 +360,17 @@ pub async fn search_bal_book_instances( State(state): State>, claims: Claims, Path(bal_id): Path, - Json(payload): Json, + Json(instance_payload): Json, ) -> (StatusCode, Json>) { if !user_is_bal_owner(claims.user_id, bal_id, state.db_conn.as_ref()).await { return (StatusCode::FORBIDDEN, Json(None)); } - - let mut search_query = BookInstance::find() + if let Ok(res) = BookInstance::find() .filter(book_instance::Column::BalId.eq(bal_id)) .filter(book_instance::Column::Available.eq(true)) - .join(JoinType::InnerJoin, book_instance::Relation::Book.def()); - if let Some(title) = payload.title { - search_query = search_query.filter(book::Column::Title.like(format!("%{}%", title))) - } - if let Some(author) = payload.author { - search_query = search_query.filter(book::Column::Author.like(format!("%{}%", author))) - } - - if let Ok(res) = search_query.all(state.db_conn.as_ref()).await + .join(JoinType::InnerJoin, book_instance::Relation::Book.def()) + .filter(book::Column::Title.like(format!("%{}%", instance_payload.title))) + .all(state.db_conn.as_ref()).await { let mut book_id_map = HashMap::new(); for instance in &res { diff --git a/src/utils/cli.rs b/src/utils/cli.rs index 2cb8b7e..55722a6 100644 --- a/src/utils/cli.rs +++ b/src/utils/cli.rs @@ -64,8 +64,7 @@ pub async fn manage_users(db: Arc) { let new_user = user::ActiveModel { id: NotSet, username: Set(username), - hashed_password: Set(hash_password(password)), - current_bal_id: Set(None) + hashed_password: Set(hash_password(password)) }; let res = new_user.insert(db.as_ref()).await.unwrap();