li7y/src/models.rs

79 lines
2.1 KiB
Rust
Raw Normal View History

2024-06-24 22:46:04 +02:00
// SPDX-FileCopyrightText: 2024 Simon Bruder <simon@sbruder.de>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
2024-07-07 13:48:31 +02:00
use std::fmt;
2024-06-24 22:46:04 +02:00
use diesel::prelude::*;
2024-07-07 13:48:31 +02:00
use diesel_derive_enum::DbEnum;
2024-06-24 22:46:04 +02:00
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::schema::*;
#[derive(Clone, Debug, Queryable, Selectable, Insertable, Serialize)]
2024-06-24 22:46:04 +02:00
#[diesel(table_name = items)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct Item {
pub id: Uuid,
pub name: Option<String>,
2024-07-05 12:38:45 +02:00
pub parent: Option<Uuid>,
2024-07-07 13:48:31 +02:00
pub class: Uuid,
2024-06-24 22:46:04 +02:00
}
2024-07-03 18:47:29 +02:00
#[derive(Debug, Insertable, Deserialize, AsChangeset)]
2024-06-24 22:46:04 +02:00
#[diesel(table_name = items)]
#[diesel(check_for_backend(diesel::pg::Pg))]
#[diesel(treat_none_as_null = true)]
2024-06-24 22:46:04 +02:00
pub struct NewItem {
#[serde(default)]
pub name: Option<String>,
2024-07-05 12:38:45 +02:00
#[serde(default)]
pub parent: Option<Uuid>,
2024-07-07 13:48:31 +02:00
pub class: Uuid,
}
#[derive(Clone, Debug, DbEnum, PartialEq, Deserialize, Serialize)]
2024-07-07 13:48:31 +02:00
#[ExistingTypePath = "sql_types::ItemClassType"]
#[serde(rename_all = "snake_case")]
pub enum ItemClassType {
Generic,
Specific,
}
impl ItemClassType {
pub const VARIANTS: [ItemClassType; 2] = [Self::Generic, Self::Specific];
}
impl fmt::Display for ItemClassType {
// FIXME: currently, the HTML form relies on this matching the serde serializsation.
// This should not be the case.
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Generic => write!(f, "generic"),
Self::Specific => write!(f, "specific"),
}
}
}
#[derive(Clone, Debug, Queryable, Selectable, Insertable, Serialize)]
2024-07-07 13:48:31 +02:00
#[diesel(table_name = item_classes)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct ItemClass {
pub id: Uuid,
pub name: String,
pub r#type: ItemClassType,
#[serde(skip_serializing_if = "Option::is_none")]
pub parent: Option<Uuid>,
}
#[derive(Debug, Insertable, Deserialize, AsChangeset)]
#[diesel(table_name = item_classes)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct NewItemClass {
pub name: String,
pub r#type: ItemClassType,
#[serde(default)]
pub parent: Option<Uuid>,
2024-06-24 22:46:04 +02:00
}