2024-07-07 13:48:31 +02:00
|
|
|
// SPDX-FileCopyrightText: 2024 Simon Bruder <simon@sbruder.de>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
2024-07-08 20:54:57 +02:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2024-07-07 13:48:31 +02:00
|
|
|
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();
|
|
|
|
|
2024-07-07 14:55:05 +02:00
|
|
|
let item_class = manage::item_class::get(&mut pool.clone().get().await.unwrap(), id)
|
|
|
|
.await
|
2024-07-07 13:48:31 +02:00
|
|
|
.map_err(error::ErrorInternalServerError)?;
|
|
|
|
|
2024-07-07 14:55:05 +02:00
|
|
|
// TODO: Once async closures are stable, use map_or on item_class.parent instead
|
|
|
|
let parent = match item_class.parent {
|
|
|
|
Some(id) => manage::item_class::get(&mut pool.get().await.unwrap(), id)
|
|
|
|
.await
|
|
|
|
.map(Some)
|
|
|
|
.map_err(error::ErrorInternalServerError)?,
|
|
|
|
None => None,
|
|
|
|
};
|
2024-07-07 13:48:31 +02:00
|
|
|
|
|
|
|
Ok(ItemClassDetails {
|
|
|
|
req,
|
|
|
|
item_class,
|
|
|
|
parent,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Template)]
|
|
|
|
#[template(path = "item_class_list.html")]
|
|
|
|
struct ItemClassList {
|
|
|
|
req: HttpRequest,
|
2024-07-08 20:54:57 +02:00
|
|
|
item_classes: HashMap<Uuid, ItemClass>,
|
2024-07-07 13:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/item-classes")]
|
|
|
|
async fn list_item_classes(
|
|
|
|
req: HttpRequest,
|
|
|
|
pool: web::Data<DbPool>,
|
|
|
|
) -> actix_web::Result<impl Responder> {
|
2024-07-08 20:54:57 +02:00
|
|
|
let item_classes = manage::item_class::get_all_as_map(&mut pool.get().await.unwrap())
|
2024-07-07 14:55:05 +02:00
|
|
|
.await
|
2024-07-07 13:48:31 +02:00
|
|
|
.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> {
|
2024-07-07 14:55:05 +02:00
|
|
|
let item = manage::item_class::add(&mut pool.get().await.unwrap(), data.into_inner())
|
|
|
|
.await
|
|
|
|
.map_err(error::ErrorInternalServerError)?;
|
2024-07-07 13:48:31 +02:00
|
|
|
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();
|
|
|
|
|
2024-07-07 14:55:05 +02:00
|
|
|
let item_class = manage::item_class::get(&mut pool.get().await.unwrap(), id)
|
|
|
|
.await
|
2024-07-07 13:48:31 +02:00
|
|
|
.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();
|
|
|
|
|
2024-07-07 14:55:05 +02:00
|
|
|
let item_class =
|
|
|
|
manage::item_class::update(&mut pool.get().await.unwrap(), id, data.into_inner())
|
|
|
|
.await
|
|
|
|
.map_err(error::ErrorInternalServerError)?;
|
2024-07-07 13:48:31 +02:00
|
|
|
|
|
|
|
Ok(web::Redirect::to("/item-class/".to_owned() + &item_class.id.to_string()).see_other())
|
|
|
|
}
|