li7y/src/frontend/item_class.rs
2024-07-11 01:16:37 +02:00

141 lines
3.7 KiB
Rust

// SPDX-FileCopyrightText: 2024 Simon Bruder <simon@sbruder.de>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use actix_web::{error, get, post, web, HttpRequest, Responder};
use askama_actix::Template;
use uuid::Uuid;
use crate::manage;
use crate::models::*;
use crate::DbPool;
pub fn config(cfg: &mut web::ServiceConfig) {
cfg.service(show_item_class)
.service(list_item_classes)
.service(add_item_class)
.service(add_item_class_post)
.service(edit_item_class)
.service(edit_item_class_post);
}
#[derive(Template)]
#[template(path = "item_class_details.html")]
struct ItemClassDetails {
req: HttpRequest,
item_class: ItemClass,
parent: Option<ItemClass>,
}
#[get("/item-class/{id}")]
async fn show_item_class(
req: HttpRequest,
pool: web::Data<DbPool>,
path: web::Path<Uuid>,
) -> actix_web::Result<impl Responder> {
let id = path.into_inner();
// FIXME hack
let pool_ = pool.clone();
let item_class = web::block(move || manage::item_class::get(&mut pool.get().unwrap(), id))
.await?
.map_err(error::ErrorInternalServerError)?;
let parent = web::block(move || {
item_class.parent.map_or(Ok(None), |id| {
manage::item_class::get(&mut pool_.get().unwrap(), id).map(Some)
})
})
.await?
.map_err(error::ErrorInternalServerError)?;
Ok(ItemClassDetails {
req,
item_class,
parent,
})
}
#[derive(Template)]
#[template(path = "item_class_list.html")]
struct ItemClassList {
req: HttpRequest,
item_classes: Vec<ItemClass>,
}
#[get("/item-classes")]
async fn list_item_classes(
req: HttpRequest,
pool: web::Data<DbPool>,
) -> actix_web::Result<impl Responder> {
let item_classes = web::block(move || manage::item_class::get_all(&mut pool.get().unwrap()))
.await?
.map_err(error::ErrorInternalServerError)?;
Ok(ItemClassList { req, item_classes })
}
#[derive(Template)]
#[template(path = "item_class_add.html")]
struct ItemClassAddForm {
req: HttpRequest,
data: Option<NewItemClass>,
}
#[get("/item-classes/add")]
async fn add_item_class(req: HttpRequest) -> actix_web::Result<impl Responder> {
Ok(ItemClassAddForm { req, data: None })
}
#[post("/item-classes/add")]
async fn add_item_class_post(
data: web::Form<NewItemClass>,
pool: web::Data<DbPool>,
) -> actix_web::Result<impl Responder> {
let item =
web::block(move || manage::item_class::add(&mut pool.get().unwrap(), data.into_inner()))
.await?
.map_err(error::ErrorInternalServerError)?;
Ok(web::Redirect::to("/item-class/".to_owned() + &item.id.to_string()).see_other())
}
#[derive(Template)]
#[template(path = "item_class_edit.html")]
struct ItemClassEditForm {
req: HttpRequest,
item_class: ItemClass,
}
#[get("/item-class/{id}/edit")]
async fn edit_item_class(
req: HttpRequest,
pool: web::Data<DbPool>,
path: web::Path<Uuid>,
) -> actix_web::Result<impl Responder> {
let id = path.into_inner();
let item_class = web::block(move || manage::item_class::get(&mut pool.get().unwrap(), id))
.await?
.map_err(error::ErrorInternalServerError)?;
Ok(ItemClassEditForm { req, item_class })
}
#[post("/item-class/{id}/edit")]
async fn edit_item_class_post(
pool: web::Data<DbPool>,
path: web::Path<Uuid>,
data: web::Form<NewItemClass>,
) -> actix_web::Result<impl Responder> {
let id = path.into_inner();
let item_class = web::block(move || {
manage::item_class::update(&mut pool.get().unwrap(), id, data.into_inner())
})
.await?
.map_err(error::ErrorInternalServerError)?;
Ok(web::Redirect::to("/item-class/".to_owned() + &item_class.id.to_string()).see_other())
}