about summary refs log tree commit diff stats
path: root/crates/rocie-server/src/storage/sql/get/unit/mod.rs
blob: 62819d36e95efc1ac145188751292198a088cd99 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// rocie - An enterprise grocery management system
//
// Copyright (C) 2026 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of Rocie.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.

use crate::{
    app::App,
    storage::sql::{
        unit::{Unit, UnitId},
        unit_property::UnitPropertyId,
    },
};

use sqlx::query;

impl Unit {
    pub(crate) async fn get_all(app: &App) -> Result<Vec<Self>, get_all::Error> {
        let records = query!(
            "
        SELECT id, unit_property, full_name_singular, full_name_plural, short_name, description
        FROM units
"
        )
        .fetch_all(&app.db)
        .await?;

        Ok(records
            .into_iter()
            .map(|record| Self {
                id: UnitId::from_db(&record.id),
                unit_property: UnitPropertyId::from_db(&record.unit_property),
                full_name_singular: record.full_name_singular,
                full_name_plural: record.full_name_plural,
                short_name: record.short_name,
                description: record.description,
            })
            .collect())
    }

    pub(crate) async fn from_id(app: &App, id: UnitId) -> Result<Option<Self>, from_id::Error> {
        let record = query!(
            "
        SELECT full_name_singular, unit_property, full_name_plural, short_name, description
        FROM units
        WHERE id = ?
",
            id
        )
        .fetch_optional(&app.db)
        .await?;

        if let Some(record) = record {
            Ok(Some(Self {
                id,
                unit_property: UnitPropertyId::from_db(&record.unit_property),
                full_name_singular: record.full_name_singular,
                full_name_plural: record.full_name_plural,
                short_name: record.short_name,
                description: record.description,
            }))
        } else {
            Ok(None)
        }
    }

    pub(crate) async fn from_name(app: &App, name: &str) -> Result<Option<Self>, from_name::Error> {
        let record = query!(
            "
        SELECT id, full_name_singular, unit_property, full_name_plural, short_name, description
        FROM units
        WHERE full_name_singular = ? OR full_name_plural = ? OR short_name = ?
",
            name,
            name,
            name
        )
        .fetch_optional(&app.db)
        .await?;

        if let Some(record) = record {
            Ok(Some(Self {
                id: UnitId::from_db(&record.id),
                unit_property: UnitPropertyId::from_db(&record.unit_property),
                full_name_singular: record.full_name_singular,
                full_name_plural: record.full_name_plural,
                short_name: record.short_name,
                description: record.description,
            }))
        } else {
            Ok(None)
        }
    }
}

pub(crate) mod get_all {
    use actix_web::ResponseError;

    #[derive(thiserror::Error, Debug)]
    pub(crate) enum Error {
        #[error("Failed to execute the sql query")]
        SqlError(#[from] sqlx::Error),
    }

    impl ResponseError for Error {}
}

pub(crate) mod from_id {
    use actix_web::ResponseError;

    #[derive(thiserror::Error, Debug)]
    pub(crate) enum Error {
        #[error("Failed to execute the sql query")]
        SqlError(#[from] sqlx::Error),
    }

    impl ResponseError for Error {}
}

pub(crate) mod from_name {
    use actix_web::ResponseError;

    #[derive(thiserror::Error, Debug)]
    pub(crate) enum Error {
        #[error("Failed to execute the sql query")]
        SqlError(#[from] sqlx::Error),
    }

    impl ResponseError for Error {}
}