Lines
58.11 %
Functions
8.97 %
Branches
100 %
use askama::Template;
use axum::{
Extension, Json,
extract::{Path, State},
http::StatusCode,
response::IntoResponse,
};
use serde::Deserialize;
use sqlx::types::Uuid;
use std::sync::Arc;
use crate::{AppState, jwt_auth::JWTAuthMiddleware, pages::HtmlTemplate};
#[derive(Template)]
#[template(path = "pages/tag/create.html")]
struct TagCreatePage;
pub async fn tag_create_page() -> impl IntoResponse {
HtmlTemplate(TagCreatePage {})
}
#[derive(Deserialize)]
pub struct TagForm {
tag_name: String,
tag_value: String,
description: Option<String>,
pub async fn create_tag(
State(_data): State<Arc<AppState>>,
Extension(jwt_auth): Extension<JWTAuthMiddleware>,
Json(form): Json<TagForm>,
) -> Result<impl IntoResponse, (StatusCode, Json<serde_json::Value>)> {
let user = &jwt_auth.user;
let server_user = server::user::User { id: user.id };
let description = form.description.filter(|desc| !desc.is_empty());
match server_user
.create_tag(form.tag_name, form.tag_value, description)
.await
{
Ok(id) => Ok(format!("{}: {}", t!("New tag id"), id)),
Err(e) => {
let error_response = serde_json::json!({
"status": "fail",
"message": t!("Failed to create tag"),
});
log::error!("Failed to create tag: {e:?}");
Err((StatusCode::INTERNAL_SERVER_ERROR, Json(error_response)))
pub async fn create_transaction_tag(
Path(transaction_id): Path<String>,
let tx_uuid = Uuid::parse_str(&transaction_id).map_err(|_| {
"message": "Invalid transaction ID format",
(StatusCode::BAD_REQUEST, Json(error_response))
})?;
.create_transaction_tag(tx_uuid, form.tag_name, form.tag_value, description)
Ok(id) => Ok(format!("{}: {}", t!("New transaction tag id"), id)),
"message": t!("Failed to create transaction tag"),
log::error!("Failed to create transaction tag: {e:?}");
pub async fn create_split_tag(
Path(split_id): Path<String>,
let split_uuid = Uuid::parse_str(&split_id).map_err(|_| {
"message": "Invalid split ID format",
.create_split_tag(split_uuid, form.tag_name, form.tag_value, description)
Ok(id) => Ok(format!("{}: {}", t!("New split tag id"), id)),
"message": t!("Failed to create split tag"),
log::error!("Failed to create split tag: {e:?}");