Skip to main content

autter_core/database/
properties.rs

1use crate::{
2    DataManager,
3    model::{Error, Result, User, UserPermission, properties::Property},
4};
5use oiseau::{PostgresRow, cache::Cache, execute, get, params, query_rows};
6use tetratto_core2::{auto_method, model::id::Id};
7
8impl DataManager {
9    /// Get a [`Property`] from an SQL row.
10    pub(crate) fn get_property_from_row(x: &PostgresRow) -> Property {
11        Property {
12            id: Id::deserialize(&get!(x->0(String))),
13            created: get!(x->1(i64)) as u128,
14            owner: Id::Legacy(get!(x->2(i64)) as usize),
15            name: get!(x->3(String)),
16            product: serde_json::from_str(&get!(x->4(String))).unwrap(),
17            custom_domain: get!(x->5(String)),
18        }
19    }
20
21    auto_method!(get_property_by_id()@get_property_from_row -> "SELECT * FROM a_properties WHERE id = $1" --name="property" --returns=Property --cache-key-tmpl="srmp.property:{}");
22
23    /// Get all properties by user (paginated).
24    ///
25    /// # Arguments
26    /// * `user` - the ID of the user to fetch properties for
27    pub async fn get_properties_by_user(&self, user: &Id) -> Result<Vec<Property>> {
28        let conn = match self.0.connect().await {
29            Ok(c) => c,
30            Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
31        };
32
33        let res = query_rows!(
34            &conn,
35            "SELECT * FROM a_properties WHERE owner = $1 ORDER BY created",
36            &[&(user.as_usize() as i64)],
37            |x| { Self::get_property_from_row(x) }
38        );
39
40        if res.is_err() {
41            return Err(Error::GeneralNotFound("property".to_string()));
42        }
43
44        Ok(res.unwrap())
45    }
46
47    /// Create a new property in the database.
48    ///
49    /// # Arguments
50    /// * `data` - a mock [`Property`] object to insert
51    pub async fn create_property(&self, data: Property) -> Result<()> {
52        let conn = match self.0.connect().await {
53            Ok(c) => c,
54            Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
55        };
56
57        let res = execute!(
58            &conn,
59            "INSERT INTO a_properties VALUES ($1, $2, $3, $4, $5, $6)",
60            params![
61                &data.id.printable(),
62                &(data.created as i64),
63                &(data.owner.as_usize() as i64),
64                &data.name,
65                &serde_json::to_string(&data.product).unwrap(),
66                &data.custom_domain
67            ]
68        );
69
70        if let Err(e) = res {
71            return Err(Error::DatabaseError(e.to_string()));
72        }
73
74        // return
75        Ok(())
76    }
77
78    pub async fn delete_property(&self, id: &Id, user: User) -> Result<()> {
79        let property = self.get_property_by_id(&id).await?;
80
81        if user.id != property.owner
82            && !user.permissions.contains(&UserPermission::ManageProperties)
83        {
84            return Err(Error::NotAllowed);
85        }
86
87        let conn = match self.0.connect().await {
88            Ok(c) => c,
89            Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
90        };
91
92        let res = execute!(
93            &conn,
94            "DELETE FROM a_properties WHERE id = $1",
95            &[&id.printable()]
96        );
97
98        if let Err(e) = res {
99            return Err(Error::DatabaseError(e.to_string()));
100        }
101
102        self.0.1.remove(format!("srmp.property:{}", id)).await;
103
104        // return
105        Ok(())
106    }
107
108    auto_method!(update_property_custom_domain(&str) -> "UPDATE a_properties SET custom_domain = $1 WHERE id = $2" --cache-key-tmpl="srmp.property:{}");
109    auto_method!(update_property_name(&str) -> "UPDATE a_properties SET name = $1 WHERE id = $2" --cache-key-tmpl="srmp.property:{}");
110}