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
//! Service layer for shadings.

use actix_http::StatusCode;
use chrono::Days;

use crate::config::data::SharedPool;
use crate::error::ServiceError;
use crate::model::dto::core::TimelinePage;
use crate::model::dto::shadings::{
    DeleteShadingDto, NewShadingDto, ShadingDto, ShadingSearchParameters, UpdateShadingDto,
};
use crate::model::entity::shadings::Shading;
use crate::model::entity::shadings_impl::FindShadingsParameters;

/// Time offset in days for loading shadings in the timeline.
pub const TIME_LINE_LOADING_OFFSET_DAYS: u64 = 356;

/// Search shadings from the database.
///
/// # Errors
/// If the connection to the database could not be established.
pub async fn find(
    search_parameters: ShadingSearchParameters,
    pool: &SharedPool,
) -> Result<TimelinePage<ShadingDto>, ServiceError> {
    let mut conn = pool.get().await?;

    let from = search_parameters
        .relative_to_date
        .checked_sub_days(Days::new(TIME_LINE_LOADING_OFFSET_DAYS))
        .ok_or_else(|| {
            ServiceError::new(
                StatusCode::BAD_REQUEST,
                "Could not add days to relative_to_date",
            )
        })?;

    let to = search_parameters
        .relative_to_date
        .checked_add_days(Days::new(TIME_LINE_LOADING_OFFSET_DAYS))
        .ok_or_else(|| {
            ServiceError::new(
                StatusCode::BAD_REQUEST,
                "Could not add days to relative_to_date",
            )
        })?;

    let search_parameters = FindShadingsParameters {
        layer_id: search_parameters.layer_id,
        from,
        to,
    };
    let result = Shading::find(search_parameters, &mut conn).await?;

    Ok(TimelinePage {
        results: result,
        from,
        to,
    })
}

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

/// Update the shading in the database.
///
/// # Errors
/// If the connection to the database could not be established.
pub async fn update(
    dto: UpdateShadingDto,
    pool: &SharedPool,
) -> Result<Vec<ShadingDto>, ServiceError> {
    let mut conn = pool.get().await?;
    let result = Shading::update(dto, &mut conn).await?;
    Ok(result)
}

/// Delete the shading from the database.
///
/// # Errors
/// If the connection to the database could not be established.
pub async fn delete_by_ids(
    dtos: Vec<DeleteShadingDto>,
    pool: &SharedPool,
) -> Result<(), ServiceError> {
    let mut conn = pool.get().await?;
    let _ = Shading::delete_by_ids(dtos, &mut conn).await?;
    Ok(())
}