1
use askama::Template;
2
use axum::{
3
    Extension, Json,
4
    extract::{Path, State},
5
    http::StatusCode,
6
    response::IntoResponse,
7
};
8
use serde::Deserialize;
9
use std::sync::Arc;
10
use uuid::Uuid;
11

            
12
use crate::{AppState, jwt_auth::JWTAuthMiddleware, pages::HtmlTemplate};
13

            
14
#[derive(Template)]
15
#[template(path = "pages/tag/edit.html")]
16
struct TagEditPage {
17
    id: Uuid,
18
    tag_name: String,
19
    tag_value: String,
20
    description: Option<String>,
21
}
22

            
23
pub async fn tag_edit_page(
24
    State(_data): State<Arc<AppState>>,
25
    Extension(jwt_auth): Extension<JWTAuthMiddleware>,
26
    Path(id): Path<Uuid>,
27
) -> Result<impl IntoResponse, (StatusCode, String)> {
28
    let user = &jwt_auth.user;
29
    let server_user = server::user::User { id: user.id };
30

            
31
    let tag = server_user
32
        .get_tag(id)
33
        .await
34
        .map_err(|e| (StatusCode::NOT_FOUND, format!("Tag not found: {e:?}")))?;
35

            
36
    let template = TagEditPage {
37
        id: tag.id,
38
        tag_name: tag.tag_name,
39
        tag_value: tag.tag_value,
40
        description: tag.description,
41
    };
42

            
43
    Ok(HtmlTemplate(template))
44
}
45

            
46
#[derive(Deserialize)]
47
pub struct TagEditForm {
48
    id: Uuid,
49
    tag_name: String,
50
    tag_value: String,
51
    description: Option<String>,
52
}
53

            
54
4
pub async fn edit_tag(
55
4
    State(_data): State<Arc<AppState>>,
56
4
    Extension(jwt_auth): Extension<JWTAuthMiddleware>,
57
4
    Json(form): Json<TagEditForm>,
58
6
) -> Result<impl IntoResponse, (StatusCode, Json<serde_json::Value>)> {
59
4
    let user = &jwt_auth.user;
60
4
    let server_user = server::user::User { id: user.id };
61

            
62
4
    let description = form.description.filter(|desc| !desc.is_empty());
63

            
64
4
    server_user
65
4
        .update_tag(form.id, form.tag_name, form.tag_value, description)
66
4
        .await
67
4
        .map_err(|e| {
68
4
            let error_response = serde_json::json!({
69
4
                "status": "fail",
70
4
                "message": t!("Failed to update tag"),
71
            });
72
4
            log::error!("Failed to update tag: {e:?}");
73
4
            (StatusCode::INTERNAL_SERVER_ERROR, Json(error_response))
74
4
        })?;
75

            
76
    Ok(format!("{}: {}", t!("Tag updated"), form.id))
77
4
}
78

            
79
2
pub async fn delete_tag(
80
2
    State(_data): State<Arc<AppState>>,
81
2
    Extension(jwt_auth): Extension<JWTAuthMiddleware>,
82
2
    Path(id): Path<Uuid>,
83
3
) -> Result<impl IntoResponse, (StatusCode, Json<serde_json::Value>)> {
84
2
    let user = &jwt_auth.user;
85
2
    let server_user = server::user::User { id: user.id };
86

            
87
2
    server_user.delete_tag(id).await.map_err(|e| {
88
2
        let error_response = serde_json::json!({
89
2
            "status": "fail",
90
2
            "message": t!("Failed to delete tag"),
91
        });
92
2
        log::error!("Failed to delete tag: {e:?}");
93
2
        (StatusCode::INTERNAL_SERVER_ERROR, Json(error_response))
94
2
    })?;
95

            
96
    Ok(t!("Tag deleted"))
97
2
}