feat: list books of a bal with specific EAN
This commit is contained in:
parent
5ce3e7a62c
commit
5dbbe23103
2 changed files with 42 additions and 2 deletions
|
|
@ -171,6 +171,7 @@ async fn run_server(db: Arc<DatabaseConnection>) {
|
||||||
.routes(routes!(routes::book_instance::sell_book_instance))
|
.routes(routes!(routes::book_instance::sell_book_instance))
|
||||||
.routes(routes!(routes::book_instance::bulk_create_book_instance))
|
.routes(routes!(routes::book_instance::bulk_create_book_instance))
|
||||||
.routes(routes!(routes::book_instance::get_bal_owner_book_instances))
|
.routes(routes!(routes::book_instance::get_bal_owner_book_instances))
|
||||||
|
.routes(routes!(routes::book_instance::get_bal_book_instances_by_ean))
|
||||||
// Owner API
|
// Owner API
|
||||||
.routes(routes!(routes::owner::get_owner_by_id))
|
.routes(routes!(routes::owner::get_owner_by_id))
|
||||||
.routes(routes!(routes::owner::create_owner))
|
.routes(routes!(routes::owner::create_owner))
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,11 @@ use std::sync::Arc;
|
||||||
|
|
||||||
use axum::{extract::{Path, State}, Json};
|
use axum::{extract::{Path, State}, Json};
|
||||||
use reqwest::{StatusCode};
|
use reqwest::{StatusCode};
|
||||||
use sea_orm::{ActiveModelTrait, ActiveValue::{NotSet, Set}, ColumnTrait, EntityTrait, QueryFilter, TryIntoModel};
|
use sea_orm::{ActiveModelTrait, ActiveValue::{NotSet, Set}, ColumnTrait, EntityTrait, JoinType, QueryFilter, QuerySelect, RelationTrait, TryIntoModel};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use utoipa::IntoParams;
|
use utoipa::IntoParams;
|
||||||
|
|
||||||
use crate::{entities::{book_instance, prelude::*}, routes::auth::Claims, utils::auth::{user_is_bal_owner, user_is_book_instance_owner, user_is_owner_owner}, AppState};
|
use crate::{entities::{book, book_instance, prelude::*}, routes::auth::Claims, utils::auth::{user_is_bal_owner, user_is_book_instance_owner, user_is_owner_owner}, AppState};
|
||||||
|
|
||||||
|
|
||||||
#[derive(IntoParams)]
|
#[derive(IntoParams)]
|
||||||
|
|
@ -295,3 +295,42 @@ pub async fn get_bal_owner_book_instances(
|
||||||
(StatusCode::INTERNAL_SERVER_ERROR, Json(vec![]))
|
(StatusCode::INTERNAL_SERVER_ERROR, Json(vec![]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(IntoParams)]
|
||||||
|
#[into_params(names("bal_id", "ean"), parameter_in = Path)]
|
||||||
|
#[allow(dead_code)]
|
||||||
|
struct BalBookByEanParams(u32, String);
|
||||||
|
|
||||||
|
#[axum::debug_handler]
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/bal/{bal_id}/ean/{ean}/book_instances",
|
||||||
|
params(BalBookByEanParams),
|
||||||
|
security(("jwt" = [])),
|
||||||
|
responses(
|
||||||
|
(status = OK, body = Vec<book_instance::Model>, description = "Found book instances in the database"),
|
||||||
|
(status = FORBIDDEN, description = "You do not own the specified bal"),
|
||||||
|
),
|
||||||
|
summary = "Get books instances with the specified ean in a bal",
|
||||||
|
description = "Lists all book instances with the specified ean in a bal",
|
||||||
|
tag = "book-instance-api",
|
||||||
|
)]
|
||||||
|
pub async fn get_bal_book_instances_by_ean(
|
||||||
|
State(state): State<Arc<AppState>>,
|
||||||
|
claims: Claims,
|
||||||
|
Path((bal_id, ean)): Path<(u32, String)>,
|
||||||
|
) -> (StatusCode, Json<Vec<book_instance::Model>>) {
|
||||||
|
if !user_is_bal_owner(claims.user_id, bal_id, state.db_conn.as_ref()).await {
|
||||||
|
return (StatusCode::FORBIDDEN, Json(vec![]));
|
||||||
|
}
|
||||||
|
if let Ok(res) = BookInstance::find()
|
||||||
|
.filter(book_instance::Column::BalId.eq(bal_id))
|
||||||
|
.join(JoinType::InnerJoin, book_instance::Relation::Book.def())
|
||||||
|
.filter(book::Column::Ean.eq(ean))
|
||||||
|
.all(state.db_conn.as_ref()).await
|
||||||
|
{
|
||||||
|
(StatusCode::OK, Json(res))
|
||||||
|
} else {
|
||||||
|
(StatusCode::INTERNAL_SERVER_ERROR, Json(vec![]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Reference in a new issue