lacoctelera/routes/author/
patch.rsuse crate::{
authentication::{check_access, AuthData},
domain::Author,
routes::author::utils::{get_author_from_db, modify_author_from_db},
};
use actix_web::{
patch,
web::{Data, Json, Path, Query},
HttpResponse,
};
use sqlx::MySqlPool;
use std::error::Error;
use tracing::{debug, info, instrument};
#[utoipa::path(
patch,
context_path = "/author/",
tag = "Author",
security(
("api_key" = [])
),
request_body(
content = Author, description = "A partial definition of an Author entry.",
example = json!({"id": "0191e13b-5ab7-78f1-bc06-be503a6c111b", "name": "Juana"})
),
responses(
(status = 200, description = "The author entry was updated in the DB."),
(status = 401, description = "The client has no access to this resource."),
(status = 404, description = "An author identified by the given ID didn't exist in the DB."),
)
)]
#[instrument(skip(pool, token, path), fields(author_id = %path.0))]
#[patch("{id}")]
pub async fn patch_author(
path: Path<(String,)>,
req: Json<Author>,
pool: Data<MySqlPool>,
token: Query<AuthData>,
) -> Result<HttpResponse, Box<dyn Error>> {
check_access(&pool, &token.api_key).await?;
debug!("Access granted");
let author_id = &path.0;
let mut existing_author = get_author_from_db(&pool, author_id).await?;
existing_author.update_from(&req);
debug!("Author modified: {:#?}", existing_author);
modify_author_from_db(&pool, &existing_author).await?;
info!("Author entry {author_id} modified");
Ok(HttpResponse::Ok().finish())
}