lacoctelera/routes/author/
delete.rsuse crate::{
authentication::{check_access, AuthData},
domain::DataDomainError,
routes::author::utils::delete_author_from_db,
};
use actix_web::{
delete,
web::{Data, Path, Query},
HttpResponse,
};
use sqlx::MySqlPool;
use std::error::Error;
use tracing::{info, instrument};
use uuid::Uuid;
#[utoipa::path(
delete,
context_path = "/author/",
tag = "Author",
security(
("api_key" = [])
),
responses(
(status = 200, description = "The author was deleted from 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(path, token, pool), fields(author_id = %path.0))]
#[delete("{id}")]
pub async fn delete_author(
path: Path<(String,)>,
token: Query<AuthData>,
pool: Data<MySqlPool>,
) -> Result<HttpResponse, Box<dyn Error>> {
check_access(&pool, &token.api_key).await?;
info!("Access granted");
let author_id = match Uuid::parse_str(&path.0) {
Ok(id) => id,
Err(_) => return Err(Box::new(DataDomainError::InvalidId)),
};
delete_author_from_db(&pool, &author_id).await?;
info!("Author {} deleted from the DB.", author_id.to_string());
Ok(HttpResponse::Ok().finish())
}