feat: add author optional parameter to the BookInstance search payload
This commit is contained in:
parent
9bbdb9decb
commit
61aac4bd80
2 changed files with 14 additions and 12 deletions
|
|
@ -15,12 +15,6 @@ pub struct Model {
|
|||
pub enum Relation {
|
||||
#[sea_orm(has_many = "super::book_instance::Entity")]
|
||||
BookInstance,
|
||||
#[sea_orm(
|
||||
belongs_to = "super::user::Entity",
|
||||
from = "Column::Id",
|
||||
to = "super::user::Column::CurrentBalId"
|
||||
)]
|
||||
User,
|
||||
}
|
||||
|
||||
impl Related<super::book_instance::Entity> for Entity {
|
||||
|
|
|
|||
|
|
@ -332,7 +332,8 @@ pub async fn get_bal_book_instances_by_ean(
|
|||
|
||||
#[derive(Deserialize, Serialize, utoipa::ToSchema)]
|
||||
pub struct BookInstanceSearchParams {
|
||||
title: String,
|
||||
title: Option<String>,
|
||||
author: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, utoipa::ToSchema)]
|
||||
|
|
@ -360,17 +361,24 @@ pub async fn search_bal_book_instances(
|
|||
State(state): State<Arc<AppState>>,
|
||||
claims: Claims,
|
||||
Path(bal_id): Path<u32>,
|
||||
Json(instance_payload): Json<BookInstanceSearchParams>,
|
||||
Json(payload): Json<BookInstanceSearchParams>,
|
||||
) -> (StatusCode, Json<Option<BookInstanceSearchResults>>) {
|
||||
if !user_is_bal_owner(claims.user_id, bal_id, state.db_conn.as_ref()).await {
|
||||
return (StatusCode::FORBIDDEN, Json(None));
|
||||
}
|
||||
if let Ok(res) = BookInstance::find()
|
||||
|
||||
let mut search_query = 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())
|
||||
.filter(book::Column::Title.like(format!("%{}%", instance_payload.title)))
|
||||
.all(state.db_conn.as_ref()).await
|
||||
.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
|
||||
{
|
||||
let mut book_id_map = HashMap::new();
|
||||
for instance in &res {
|
||||
|
|
|
|||
Reference in a new issue