Lines
61.82 %
Functions
11.67 %
Branches
100 %
use askama::Template;
use axum::{
Extension, Json,
extract::{Path, State},
http::StatusCode,
response::IntoResponse,
};
use serde::Deserialize;
use std::sync::Arc;
use uuid::Uuid;
use crate::{AppState, jwt_auth::JWTAuthMiddleware, pages::HtmlTemplate};
#[derive(Template)]
#[template(path = "pages/tag/edit.html")]
struct TagEditPage {
id: Uuid,
tag_name: String,
tag_value: String,
description: Option<String>,
}
pub async fn tag_edit_page(
State(_data): State<Arc<AppState>>,
Extension(jwt_auth): Extension<JWTAuthMiddleware>,
Path(id): Path<Uuid>,
) -> Result<impl IntoResponse, (StatusCode, String)> {
let user = &jwt_auth.user;
let server_user = server::user::User { id: user.id };
let tag = server_user
.get_tag(id)
.await
.map_err(|e| (StatusCode::NOT_FOUND, format!("Tag not found: {e:?}")))?;
let template = TagEditPage {
id: tag.id,
tag_name: tag.tag_name,
tag_value: tag.tag_value,
description: tag.description,
Ok(HtmlTemplate(template))
#[derive(Deserialize)]
pub struct TagEditForm {
pub async fn edit_tag(
Json(form): Json<TagEditForm>,
) -> Result<impl IntoResponse, (StatusCode, Json<serde_json::Value>)> {
let description = form.description.filter(|desc| !desc.is_empty());
server_user
.update_tag(form.id, form.tag_name, form.tag_value, description)
.map_err(|e| {
let error_response = serde_json::json!({
"status": "fail",
"message": t!("Failed to update tag"),
});
log::error!("Failed to update tag: {e:?}");
(StatusCode::INTERNAL_SERVER_ERROR, Json(error_response))
})?;
Ok(format!("{}: {}", t!("Tag updated"), form.id))
pub async fn delete_tag(
server_user.delete_tag(id).await.map_err(|e| {
"message": t!("Failed to delete tag"),
log::error!("Failed to delete tag: {e:?}");
Ok(t!("Tag deleted"))