Lines
63.33 %
Functions
6.67 %
Branches
100 %
use std::sync::Arc;
use askama::Template;
use axum::{Extension, Json, extract::State, http::StatusCode, response::IntoResponse};
use serde::Deserialize;
use crate::{AppState, jwt_auth::JWTAuthMiddleware, pages::HtmlTemplate};
#[derive(Template)]
#[template(path = "pages/commodity/create.html")]
struct CommodityCreatePage;
pub async fn commodity_create_page() -> impl IntoResponse {
let template = CommodityCreatePage {};
HtmlTemplate(template)
}
#[template(path = "components/commodity/create.html")]
struct CommodityFormTemplate;
pub async fn commodity_form() -> impl IntoResponse {
let template = CommodityFormTemplate {};
#[derive(Deserialize)]
pub struct CommodityForm {
symbol: String,
name: String,
pub async fn commodity_submit(
State(_data): State<Arc<AppState>>,
Extension(jwt_auth): Extension<JWTAuthMiddleware>,
Json(form): Json<CommodityForm>,
) -> Result<impl IntoResponse, (StatusCode, Json<serde_json::Value>)> {
let user = &jwt_auth.user;
// Execute command
match server::command::commodity::CreateCommodity::new()
.symbol(form.symbol)
.name(form.name)
.user_id(user.id)
.run()
.await
{
Ok(result) => match result {
Some(id) => Ok(format!("{}: {}", t!("New commodity id"), id)),
None => Ok("New commodity created".to_string()),
},
Err(e) => {
let error_response = serde_json::json!({
"status": "fail",
"message": t!("Failed to create commodity"),
});
log::error!("Failed to create commodity: {e:?}");
Err((StatusCode::INTERNAL_SERVER_ERROR, Json(error_response)))