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
//! Service layer for images on the base layer.

use uuid::Uuid;

use crate::{
    config::data::SharedPool,
    error::ServiceError,
    model::dto::base_layer_images::{BaseLayerImageDto, UpdateBaseLayerImageDto},
    model::entity::base_layer_images::BaseLayerImages,
};

/// Fetch all base layer images for the layer from the database.
///
/// # Errors
/// If the connection to the database could not be established.
pub async fn find(
    pool: &SharedPool,
    layer_id: Uuid,
) -> Result<Vec<BaseLayerImageDto>, ServiceError> {
    let mut conn = pool.get().await?;
    let result = BaseLayerImages::find(&mut conn, layer_id).await?;
    Ok(result)
}

/// Create a base layer image in the database.
///
/// # Errors
/// If the connection to the database could not be established.
pub async fn create(
    dto: BaseLayerImageDto,
    pool: &SharedPool,
) -> Result<BaseLayerImageDto, ServiceError> {
    let mut conn = pool.get().await?;
    let result = BaseLayerImages::create(dto, &mut conn).await?;
    Ok(result)
}

/// Update the base layer image in the database.
///
/// # Errors
/// If the connection to the database could not be established.
pub async fn update(
    id: Uuid,
    dto: UpdateBaseLayerImageDto,
    pool: &SharedPool,
) -> Result<BaseLayerImageDto, ServiceError> {
    let mut conn = pool.get().await?;
    let result = BaseLayerImages::update(id, dto, &mut conn).await?;
    Ok(result)
}

/// Delete the base layer image from the database.
///
/// # Errors
/// If the connection to the database could not be established.
pub async fn delete_by_id(id: Uuid, pool: &SharedPool) -> Result<(), ServiceError> {
    let mut conn = pool.get().await?;
    let _ = BaseLayerImages::delete_by_id(id, &mut conn).await?;
    Ok(())
}