feat: create_book endpoint

This commit is contained in:
Ninjdai 2025-08-03 12:03:31 +02:00
parent 6ddb24e563
commit 684eba8c70
2 changed files with 49 additions and 1 deletions

View file

@ -160,6 +160,7 @@ async fn run_server(db: Arc<DatabaseConnection>) {
// Book API
.routes(routes!(routes::book::get_book_by_ean))
.routes(routes!(routes::book::get_book_by_id))
.routes(routes!(routes::book::create_book))
// Book Instance API
.routes(routes!(routes::book_instance::get_book_instance_by_id))
.routes(routes!(routes::book_instance::create_book_instance))

View file

@ -2,7 +2,8 @@ use std::sync::Arc;
use axum::{extract::{Path, State}, Json};
use reqwest::{StatusCode};
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};
use sea_orm::{ActiveModelTrait, ActiveValue::{NotSet, Set}, ColumnTrait, EntityTrait, QueryFilter, TryIntoModel};
use serde::{Deserialize, Serialize};
use utoipa::IntoParams;
use crate::{entities::{book, prelude::{Book}}, utils::open_library};
@ -85,3 +86,49 @@ pub async fn get_book_by_ean(
}
}
}
#[derive(Deserialize, Serialize, utoipa::ToSchema)]
pub struct BookCreateParams {
ean: String,
title: String,
author: String
}
#[axum::debug_handler]
#[utoipa::path(
post,
path = "/book",
request_body = BookCreateParams,
security(("jwt" = [])),
responses(
(status = OK, body = book::Model, description = "Successfully saved book data"),
(status = CONFLICT, body = book::Model, description = "A book with the same EAN already exists. Replies with the data of the already saved book."),
),
summary = "Manually add book data to the database",
description = "Should be used when needing to insert data for an EAN that doesn't already exist in the database and couldn't be fetched automatically, ie the /book/ean/{ean} endpoint returned a 404 NOT FOUND error",
tag = "book-api",
)]
pub async fn create_book(
State(state): State<Arc<AppState>>,
Json(instance_payload): Json<BookCreateParams>,
) -> (StatusCode, Json<Option<book::Model>>) {
if let Some(book) = Book::find().filter(book::Column::Ean.eq(&instance_payload.ean)).one(state.db_conn.as_ref()).await.unwrap() {
return (StatusCode::CONFLICT, Json(Some(book)));
}
let book = book::ActiveModel {
ean: Set(instance_payload.ean),
title: Set(instance_payload.title),
author: Set(instance_payload.author),
id: NotSet
};
let b = book.save(state.db_conn.as_ref()).await;
match b {
Err(e) => {
log::error!(target: "api", "Error while inserting new book: {:#?}", e);
(StatusCode::BAD_REQUEST, Json(None))
},
Ok(res) => (StatusCode::OK, Json(Some(res.try_into_model().expect("All fields should be set once the book is saved"))))
}
}