|
| 1 | +//! API handlers for the [`tag`](crate::web::api::v1::contexts::tag) API |
| 2 | +//! context. |
| 3 | +use std::sync::Arc; |
| 4 | + |
| 5 | +use axum::extract::{self, State}; |
| 6 | +use axum::response::Json; |
| 7 | + |
| 8 | +use super::forms::{AddTagForm, DeleteTagForm}; |
| 9 | +use super::responses::{added_tag, deleted_tag}; |
| 10 | +use crate::common::AppData; |
| 11 | +use crate::databases::database; |
| 12 | +use crate::errors::ServiceError; |
| 13 | +use crate::models::torrent_tag::TorrentTag; |
| 14 | +use crate::web::api::v1::extractors::bearer_token::Extract; |
| 15 | +use crate::web::api::v1::responses::{self, OkResponse}; |
| 16 | + |
| 17 | +/// It handles the request to get all the tags. |
| 18 | +/// |
| 19 | +/// It returns: |
| 20 | +/// |
| 21 | +/// - `200` response with a json containing the tag list [`Vec<TorrentTag>`](crate::models::torrent_tag::TorrentTag). |
| 22 | +/// - Other error status codes if there is a database error. |
| 23 | +/// |
| 24 | +/// Refer to the [API endpoint documentation](crate::web::api::v1::contexts::tag) |
| 25 | +/// for more information about this endpoint. |
| 26 | +/// |
| 27 | +/// # Errors |
| 28 | +/// |
| 29 | +/// It returns an error if there is a database error. |
| 30 | +#[allow(clippy::unused_async)] |
| 31 | +pub async fn get_all_handler( |
| 32 | + State(app_data): State<Arc<AppData>>, |
| 33 | +) -> Result<Json<responses::OkResponse<Vec<TorrentTag>>>, database::Error> { |
| 34 | + match app_data.tag_repository.get_all().await { |
| 35 | + Ok(tags) => Ok(Json(responses::OkResponse { data: tags })), |
| 36 | + Err(error) => Err(error), |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/// It adds a new tag. |
| 41 | +/// |
| 42 | +/// # Errors |
| 43 | +/// |
| 44 | +/// It returns an error if: |
| 45 | +/// |
| 46 | +/// - The user does not have permissions to create a new tag. |
| 47 | +/// - There is a database error. |
| 48 | +#[allow(clippy::unused_async)] |
| 49 | +pub async fn add_handler( |
| 50 | + State(app_data): State<Arc<AppData>>, |
| 51 | + Extract(maybe_bearer_token): Extract, |
| 52 | + extract::Json(add_tag_form): extract::Json<AddTagForm>, |
| 53 | +) -> Result<Json<OkResponse<String>>, ServiceError> { |
| 54 | + let user_id = app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await?; |
| 55 | + |
| 56 | + match app_data.tag_service.add_tag(&add_tag_form.name, &user_id).await { |
| 57 | + Ok(_) => Ok(added_tag(&add_tag_form.name)), |
| 58 | + Err(error) => Err(error), |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/// It deletes a tag. |
| 63 | +/// |
| 64 | +/// # Errors |
| 65 | +/// |
| 66 | +/// It returns an error if: |
| 67 | +/// |
| 68 | +/// - The user does not have permissions to delete tags. |
| 69 | +/// - There is a database error. |
| 70 | +#[allow(clippy::unused_async)] |
| 71 | +pub async fn delete_handler( |
| 72 | + State(app_data): State<Arc<AppData>>, |
| 73 | + Extract(maybe_bearer_token): Extract, |
| 74 | + extract::Json(delete_tag_form): extract::Json<DeleteTagForm>, |
| 75 | +) -> Result<Json<OkResponse<String>>, ServiceError> { |
| 76 | + let user_id = app_data.auth.get_user_id_from_bearer_token(&maybe_bearer_token).await?; |
| 77 | + |
| 78 | + match app_data.tag_service.delete_tag(&delete_tag_form.tag_id, &user_id).await { |
| 79 | + Ok(_) => Ok(deleted_tag(delete_tag_form.tag_id)), |
| 80 | + Err(error) => Err(error), |
| 81 | + } |
| 82 | +} |
0 commit comments