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
//! Contains the implementation of [`BaseLayerImages`].

use diesel::pg::Pg;
use diesel::{debug_query, ExpressionMethods, QueryDsl, QueryResult};
use diesel_async::{AsyncPgConnection, RunQueryDsl};
use log::debug;
use uuid::Uuid;

use crate::{
    model::{
        dto::base_layer_images::{BaseLayerImageDto, UpdateBaseLayerImageDto},
        entity::base_layer_images::BaseLayerImages,
    },
    schema::base_layer_images::{self, all_columns},
};

impl BaseLayerImages {
    /// Get all `BaseLayerImages` for the layer.
    ///
    /// # Errors
    /// * Unknown, diesel doesn't say why it might error.
    pub async fn find(
        conn: &mut AsyncPgConnection,
        layer_id: Uuid,
    ) -> QueryResult<Vec<BaseLayerImageDto>> {
        let query = base_layer_images::table
            .select(all_columns)
            .filter(base_layer_images::layer_id.eq(layer_id));

        debug!("{}", debug_query::<Pg, _>(&query));
        Ok(query
            .load::<Self>(conn)
            .await?
            .into_iter()
            .map(Into::into)
            .collect())
    }

    /// Create a new `BaseLayerImages` in the database.
    ///
    /// # Errors
    /// * If the `layer_id` references a layer that is not of type `base`.
    /// * Unknown, diesel doesn't say why it might error.
    pub async fn create(
        dto: BaseLayerImageDto,
        conn: &mut AsyncPgConnection,
    ) -> QueryResult<BaseLayerImageDto> {
        let insert = Self::from(dto);
        let query = diesel::insert_into(base_layer_images::table).values(&insert);
        debug!("{}", debug_query::<Pg, _>(&query));
        query.get_result::<Self>(conn).await.map(Into::into)
    }

    /// Update a `BaseLayerImages` in the database.
    ///
    /// # Errors
    /// * Unknown, diesel doesn't say why it might error.
    pub async fn update(
        id: Uuid,
        dto: UpdateBaseLayerImageDto,
        conn: &mut AsyncPgConnection,
    ) -> QueryResult<BaseLayerImageDto> {
        let update = Self::from((id, dto));
        let query = diesel::update(base_layer_images::table.find(id)).set(&update);
        debug!("{}", debug_query::<Pg, _>(&query));
        query.get_result::<Self>(conn).await.map(Into::into)
    }

    /// Delete the `BaseLayerImages` from the database.
    ///
    /// # Errors
    /// * Unknown, diesel doesn't say why it might error.
    pub async fn delete_by_id(id: Uuid, conn: &mut AsyncPgConnection) -> QueryResult<usize> {
        let query = diesel::delete(base_layer_images::table.find(id));
        debug!("{}", debug_query::<Pg, _>(&query));
        query.execute(conn).await
    }
}