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-13 13:41:23 +02:00
|
|
|
|
use actix_identity::Identity;
|
2024-07-11 01:12:34 +02:00
|
|
|
|
use actix_web::{error, get, post, web, Responder};
|
|
|
|
|
use maud::html;
|
2024-07-11 17:25:01 +02:00
|
|
|
|
use sqlx::PgPool;
|
2024-07-07 13:48:31 +02:00
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
2024-07-13 15:04:27 +02:00
|
|
|
|
use super::templates::helpers::{Colour, PageAction, PageActionMethod};
|
2024-07-12 15:44:11 +02:00
|
|
|
|
use super::templates::{self, datalist, forms, TemplateConfig};
|
2024-07-07 13:48:31 +02:00
|
|
|
|
use crate::manage;
|
|
|
|
|
use crate::models::*;
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[get("/item-class/{id}")]
|
|
|
|
|
async fn show_item_class(
|
2024-07-11 17:25:01 +02:00
|
|
|
|
pool: web::Data<PgPool>,
|
2024-07-07 13:48:31 +02:00
|
|
|
|
path: web::Path<Uuid>,
|
2024-07-13 13:41:23 +02:00
|
|
|
|
user: Identity,
|
2024-07-07 13:48:31 +02:00
|
|
|
|
) -> actix_web::Result<impl Responder> {
|
|
|
|
|
let id = path.into_inner();
|
|
|
|
|
|
2024-07-11 17:25:01 +02:00
|
|
|
|
let item_class = manage::item_class::get(&pool, id)
|
2024-07-07 14:55:05 +02:00
|
|
|
|
.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 {
|
2024-07-11 17:25:01 +02:00
|
|
|
|
Some(id) => manage::item_class::get(&pool, id)
|
2024-07-07 14:55:05 +02:00
|
|
|
|
.await
|
|
|
|
|
.map(Some)
|
|
|
|
|
.map_err(error::ErrorInternalServerError)?,
|
|
|
|
|
None => None,
|
|
|
|
|
};
|
2024-07-07 13:48:31 +02:00
|
|
|
|
|
2024-07-11 17:25:01 +02:00
|
|
|
|
let children = manage::item_class::children(&pool, id)
|
2024-07-11 13:50:59 +02:00
|
|
|
|
.await
|
|
|
|
|
.map_err(error::ErrorInternalServerError)?;
|
|
|
|
|
|
2024-07-11 01:12:34 +02:00
|
|
|
|
let mut title = item_class.name.clone();
|
|
|
|
|
title.push_str(" – Item Details");
|
2024-07-07 13:48:31 +02:00
|
|
|
|
|
2024-07-11 23:20:22 +02:00
|
|
|
|
let mut page_actions = vec![
|
2024-07-13 15:04:27 +02:00
|
|
|
|
(PageAction {
|
|
|
|
|
method: PageActionMethod::Get,
|
|
|
|
|
target: format!("/items/add?class={}", item_class.id),
|
2024-07-11 23:20:22 +02:00
|
|
|
|
name: "Add Item".to_string(),
|
2024-07-13 15:04:27 +02:00
|
|
|
|
colour: Colour::Success,
|
2024-07-11 23:20:22 +02:00
|
|
|
|
}),
|
|
|
|
|
];
|
|
|
|
|
if item_class.parent.is_none() {
|
2024-07-13 15:04:27 +02:00
|
|
|
|
page_actions.push(PageAction {
|
|
|
|
|
method: PageActionMethod::Get,
|
|
|
|
|
target: format!("/item-classes/add?parent={}", item_class.id),
|
2024-07-11 23:20:22 +02:00
|
|
|
|
name: "Add Child".to_string(),
|
2024-07-13 15:04:27 +02:00
|
|
|
|
colour: Colour::Primary,
|
2024-07-11 23:20:22 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
2024-07-13 15:04:27 +02:00
|
|
|
|
page_actions.push(PageAction {
|
|
|
|
|
method: PageActionMethod::Get,
|
|
|
|
|
target: format!("/item-class/{}/edit", item_class.id),
|
2024-07-11 23:20:22 +02:00
|
|
|
|
name: "Edit".to_string(),
|
2024-07-13 15:04:27 +02:00
|
|
|
|
colour: Colour::Warning,
|
2024-07-11 23:20:22 +02:00
|
|
|
|
});
|
|
|
|
|
|
2024-07-11 01:12:34 +02:00
|
|
|
|
Ok(templates::base(
|
|
|
|
|
TemplateConfig {
|
|
|
|
|
path: &format!("/item-class/{}", item_class.id),
|
|
|
|
|
title: Some(&title),
|
|
|
|
|
page_title: Some(Box::new(item_class.name.clone())),
|
2024-07-11 23:20:22 +02:00
|
|
|
|
page_actions,
|
2024-07-13 13:41:23 +02:00
|
|
|
|
user: Some(user),
|
2024-07-11 01:12:34 +02:00
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
html! {
|
|
|
|
|
table .table {
|
|
|
|
|
tr {
|
|
|
|
|
th { "UUID" }
|
|
|
|
|
td { (item_class.id) }
|
|
|
|
|
}
|
|
|
|
|
tr {
|
|
|
|
|
th { "Name" }
|
|
|
|
|
td { (item_class.name) }
|
|
|
|
|
}
|
|
|
|
|
@if let Some(parent) = parent {
|
|
|
|
|
tr {
|
|
|
|
|
th { "Parent" }
|
|
|
|
|
td { a href={ "/item-class/" (parent.id) } { (parent.name) } }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-11 13:50:59 +02:00
|
|
|
|
|
|
|
|
|
@if !children.is_empty() {
|
|
|
|
|
h3 .mt-4 { "Children" }
|
|
|
|
|
|
|
|
|
|
ul {
|
|
|
|
|
@for child in children {
|
|
|
|
|
li {
|
|
|
|
|
a href={ "/item-class/" (child.id) } { (child.name) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-11 01:12:34 +02:00
|
|
|
|
},
|
|
|
|
|
))
|
2024-07-07 13:48:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[get("/item-classes")]
|
2024-07-13 13:41:23 +02:00
|
|
|
|
async fn list_item_classes(
|
|
|
|
|
pool: web::Data<PgPool>,
|
|
|
|
|
user: Identity,
|
|
|
|
|
) -> actix_web::Result<impl Responder> {
|
2024-07-11 21:50:03 +02:00
|
|
|
|
let item_classes_ids = sqlx::query_scalar!("SELECT id FROM item_classes ORDER BY created_at")
|
|
|
|
|
.fetch_all(pool.as_ref())
|
|
|
|
|
.await
|
|
|
|
|
.map_err(error::ErrorInternalServerError)?;
|
|
|
|
|
|
2024-07-11 17:25:01 +02:00
|
|
|
|
let item_classes = manage::item_class::get_all_as_map(&pool)
|
2024-07-07 14:55:05 +02:00
|
|
|
|
.await
|
2024-07-07 13:48:31 +02:00
|
|
|
|
.map_err(error::ErrorInternalServerError)?;
|
|
|
|
|
|
2024-07-11 01:12:34 +02:00
|
|
|
|
Ok(templates::base(
|
|
|
|
|
TemplateConfig {
|
|
|
|
|
path: "/item-classes",
|
|
|
|
|
title: Some("Item Class List"),
|
|
|
|
|
page_title: Some(Box::new("Item Class List")),
|
|
|
|
|
page_actions: vec![
|
2024-07-13 15:04:27 +02:00
|
|
|
|
(PageAction {
|
|
|
|
|
method: PageActionMethod::Get,
|
|
|
|
|
target: "/item-classes/add".to_string(),
|
2024-07-11 23:17:22 +02:00
|
|
|
|
name: "Add".to_string(),
|
2024-07-13 15:04:27 +02:00
|
|
|
|
colour: Colour::Success,
|
2024-07-11 01:12:34 +02:00
|
|
|
|
}),
|
|
|
|
|
],
|
2024-07-13 13:41:23 +02:00
|
|
|
|
user: Some(user),
|
2024-07-11 01:12:34 +02:00
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
html! {
|
|
|
|
|
table .table {
|
|
|
|
|
thead {
|
|
|
|
|
tr {
|
|
|
|
|
th { "Name" }
|
|
|
|
|
th { "Parents" }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
tbody {
|
2024-07-11 21:50:03 +02:00
|
|
|
|
@for item_class in item_classes_ids {
|
|
|
|
|
@let item_class = item_classes.get(&item_class).unwrap();
|
2024-07-11 01:12:34 +02:00
|
|
|
|
tr {
|
|
|
|
|
td { a href={ "/item-class/" (item_class.id) } { (item_class.name) } }
|
|
|
|
|
td {
|
|
|
|
|
@if let Some(parent) = item_class.parent {
|
|
|
|
|
@let parent = item_classes.get(&parent).unwrap();
|
|
|
|
|
a href={ "/item-class/" (parent.id) } { (parent.name) }
|
|
|
|
|
} @else {
|
|
|
|
|
"-"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
))
|
2024-07-07 13:48:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[get("/item-classes/add")]
|
2024-07-12 15:44:11 +02:00
|
|
|
|
async fn add_item_class(
|
|
|
|
|
pool: web::Data<PgPool>,
|
|
|
|
|
form: web::Query<NewItemClassForm>,
|
2024-07-13 13:41:23 +02:00
|
|
|
|
user: Identity,
|
2024-07-12 15:44:11 +02:00
|
|
|
|
) -> actix_web::Result<impl Responder> {
|
|
|
|
|
let datalist_item_classes = datalist::item_classes(&pool)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(error::ErrorInternalServerError)?;
|
|
|
|
|
|
2024-07-11 01:12:34 +02:00
|
|
|
|
Ok(templates::base(
|
|
|
|
|
TemplateConfig {
|
|
|
|
|
path: "/items-classes/add",
|
|
|
|
|
title: Some("Add Item Class"),
|
|
|
|
|
page_title: Some(Box::new("Add Item Class")),
|
2024-07-12 15:44:11 +02:00
|
|
|
|
datalists: vec![&datalist_item_classes],
|
2024-07-13 13:41:23 +02:00
|
|
|
|
user: Some(user),
|
2024-07-11 01:12:34 +02:00
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
html! {
|
|
|
|
|
form method="POST" {
|
|
|
|
|
(forms::InputGroup {
|
|
|
|
|
r#type: forms::InputType::Text,
|
|
|
|
|
name: "name",
|
|
|
|
|
title: "Name",
|
|
|
|
|
required: true,
|
2024-07-11 23:01:27 +02:00
|
|
|
|
value: form.name.clone(),
|
2024-07-11 01:12:34 +02:00
|
|
|
|
..Default::default()
|
|
|
|
|
})
|
2024-07-11 13:25:09 +02:00
|
|
|
|
(forms::InputGroup {
|
|
|
|
|
r#type: forms::InputType::Text,
|
|
|
|
|
name: "parent",
|
|
|
|
|
title: "Parent",
|
2024-07-11 23:01:27 +02:00
|
|
|
|
disabled: form.parent.is_none(),
|
|
|
|
|
value: form.parent.map(|id| id.to_string()),
|
2024-07-12 15:44:11 +02:00
|
|
|
|
datalist: Some(&datalist_item_classes),
|
2024-07-11 13:25:09 +02:00
|
|
|
|
..Default::default()
|
|
|
|
|
})
|
2024-07-11 01:12:34 +02:00
|
|
|
|
|
|
|
|
|
button .btn.btn-primary type="submit" { "Add" }
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
))
|
2024-07-07 13:48:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[post("/item-classes/add")]
|
|
|
|
|
async fn add_item_class_post(
|
|
|
|
|
data: web::Form<NewItemClass>,
|
2024-07-11 17:25:01 +02:00
|
|
|
|
pool: web::Data<PgPool>,
|
2024-07-13 13:41:23 +02:00
|
|
|
|
_user: Identity,
|
2024-07-07 13:48:31 +02:00
|
|
|
|
) -> actix_web::Result<impl Responder> {
|
2024-07-11 17:25:01 +02:00
|
|
|
|
let item = manage::item_class::add(&pool, data.into_inner())
|
2024-07-07 14:55:05 +02:00
|
|
|
|
.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())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[get("/item-class/{id}/edit")]
|
|
|
|
|
async fn edit_item_class(
|
2024-07-11 17:25:01 +02:00
|
|
|
|
pool: web::Data<PgPool>,
|
2024-07-07 13:48:31 +02:00
|
|
|
|
path: web::Path<Uuid>,
|
2024-07-13 13:41:23 +02:00
|
|
|
|
user: Identity,
|
2024-07-07 13:48:31 +02:00
|
|
|
|
) -> actix_web::Result<impl Responder> {
|
|
|
|
|
let id = path.into_inner();
|
|
|
|
|
|
2024-07-11 17:25:01 +02:00
|
|
|
|
let item_class = manage::item_class::get(&pool, id)
|
2024-07-07 14:55:05 +02:00
|
|
|
|
.await
|
2024-07-07 13:48:31 +02:00
|
|
|
|
.map_err(error::ErrorInternalServerError)?;
|
|
|
|
|
|
2024-07-12 15:44:11 +02:00
|
|
|
|
let datalist_item_classes = datalist::item_classes(&pool)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(error::ErrorInternalServerError)?;
|
|
|
|
|
|
2024-07-11 01:12:34 +02:00
|
|
|
|
let mut title = item_class.name.clone();
|
|
|
|
|
title.push_str(" – Item Details");
|
|
|
|
|
|
|
|
|
|
Ok(templates::base(
|
|
|
|
|
TemplateConfig {
|
|
|
|
|
path: &format!("/items-class/{}/add", id),
|
|
|
|
|
title: Some(&title),
|
|
|
|
|
page_title: Some(Box::new(item_class.name.clone())),
|
2024-07-12 15:44:11 +02:00
|
|
|
|
datalists: vec![&datalist_item_classes],
|
2024-07-13 13:41:23 +02:00
|
|
|
|
user: Some(user),
|
2024-07-11 01:12:34 +02:00
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
html! {
|
|
|
|
|
form method="POST" {
|
|
|
|
|
(forms::InputGroup {
|
|
|
|
|
r#type: forms::InputType::Text,
|
|
|
|
|
name: "id",
|
|
|
|
|
title: "UUID",
|
|
|
|
|
disabled: true,
|
|
|
|
|
required: true,
|
2024-07-11 23:00:55 +02:00
|
|
|
|
value: Some(item_class.id.to_string()),
|
2024-07-12 15:44:11 +02:00
|
|
|
|
..Default::default()
|
2024-07-11 01:12:34 +02:00
|
|
|
|
})
|
|
|
|
|
(forms::InputGroup {
|
|
|
|
|
r#type: forms::InputType::Text,
|
|
|
|
|
name: "name",
|
|
|
|
|
title: "Name",
|
|
|
|
|
required: true,
|
2024-07-11 23:00:55 +02:00
|
|
|
|
value: Some(item_class.name),
|
2024-07-11 01:12:34 +02:00
|
|
|
|
..Default::default()
|
|
|
|
|
})
|
2024-07-11 13:25:09 +02:00
|
|
|
|
(forms::InputGroup {
|
|
|
|
|
r#type: forms::InputType::Text,
|
|
|
|
|
name: "parent",
|
|
|
|
|
title: "Parent",
|
|
|
|
|
disabled: item_class.parent.is_none(),
|
2024-07-11 23:00:55 +02:00
|
|
|
|
value: item_class.parent.map(|id| id.to_string()),
|
2024-07-12 15:44:11 +02:00
|
|
|
|
datalist: Some(&datalist_item_classes),
|
2024-07-11 13:25:09 +02:00
|
|
|
|
..Default::default()
|
|
|
|
|
})
|
2024-07-11 01:12:34 +02:00
|
|
|
|
|
|
|
|
|
button .btn.btn-primary type="submit" { "Edit" }
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
))
|
2024-07-07 13:48:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[post("/item-class/{id}/edit")]
|
|
|
|
|
async fn edit_item_class_post(
|
2024-07-11 17:25:01 +02:00
|
|
|
|
pool: web::Data<PgPool>,
|
2024-07-07 13:48:31 +02:00
|
|
|
|
path: web::Path<Uuid>,
|
|
|
|
|
data: web::Form<NewItemClass>,
|
2024-07-13 13:41:23 +02:00
|
|
|
|
_user: Identity,
|
2024-07-07 13:48:31 +02:00
|
|
|
|
) -> actix_web::Result<impl Responder> {
|
|
|
|
|
let id = path.into_inner();
|
|
|
|
|
|
2024-07-11 17:25:01 +02:00
|
|
|
|
let item_class = manage::item_class::update(&pool, 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())
|
|
|
|
|
}
|