// SPDX-FileCopyrightText: 2024 Simon Bruder // // SPDX-License-Identifier: AGPL-3.0-or-later mod item; mod item_class; mod templates; use actix_web::{error, get, web, Responder}; use maud::html; use serde::Deserialize; use sqlx::PgPool; use uuid::Uuid; use crate::manage; use crate::models::EntityType; pub fn config(cfg: &mut web::ServiceConfig) { cfg.service(index) .service(jump) .configure(item::config) .configure(item_class::config); } #[get("/")] async fn index() -> impl Responder { templates::base(templates::TemplateConfig::default(), html! {}) } #[derive(Deserialize)] struct JumpData { id: String, } #[get("/jump")] async fn jump( pool: web::Data, data: web::Query, ) -> Result { let mut id = data.id.clone(); let entity_type = if let Ok(id) = Uuid::parse_str(&id) { manage::query_entity_type(&pool, id) .await .map_err(error::ErrorInternalServerError)? } else if let Ok(short_id) = id.parse::() { if let Ok(item) = manage::item::get_by_short_id(&pool, short_id) .await .map_err(error::ErrorInternalServerError) { id = item.id.to_string(); Some(EntityType::Item) } else { None } } else { None }; if let Some(prefix) = entity_type.map(|entity_type| match entity_type { EntityType::Item => "item", EntityType::ItemClass => "item-class", }) { Ok(web::Redirect::to(format!("/{prefix}/{id}")).see_other()) } else { Ok(web::Redirect::to(format!("/items/add?name={id}")).see_other()) } }