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

use actix_web::web::Data;
use uuid::Uuid;

use chrono::Utc;

use crate::config::data::AppDataInner;
use crate::model::dto::PageParameters;
use crate::model::dto::{Page, SeedSearchParameters};
use crate::{
    error::ServiceError,
    model::{
        dto::{ArchiveSeedDto, NewSeedDto, SeedDto},
        entity::Seed,
    },
};

/// Search seeds from the database.
/// Seeds are returned in ascending order of their `use_by` dates.
/// If that is not available, the harvest year is used instead.
///
/// By default, archived seeds will not be returned.
/// This behaviour can be changed using `search_parameters`.
///
/// # Errors
/// If the connection to the database could not be established.
pub async fn find(
    search_parameters: SeedSearchParameters,
    page_parameters: PageParameters,
    user_id: Uuid,
    app_data: &Data<AppDataInner>,
) -> Result<Page<SeedDto>, ServiceError> {
    let mut conn = app_data.pool.get().await?;
    let result = Seed::find(search_parameters, user_id, page_parameters, &mut conn).await?;
    Ok(result)
}

/// Find the seed by id from the database.
///
/// # Errors
/// If the connection to the database could not be established.
pub async fn find_by_id(
    id: i32,
    user_id: Uuid,
    app_data: &Data<AppDataInner>,
) -> Result<SeedDto, ServiceError> {
    let mut conn = app_data.pool.get().await?;
    let result = Seed::find_by_id(id, user_id, &mut conn).await?;
    Ok(result)
}

/// Create a new seed in the database.
///
/// # Errors
/// If the connection to the database could not be established.
pub async fn create(
    new_seed: NewSeedDto,
    user_id: Uuid,
    app_data: &Data<AppDataInner>,
) -> Result<SeedDto, ServiceError> {
    let mut conn = app_data.pool.get().await?;

    let seed_trimmed_name = NewSeedDto {
        name: new_seed.name.trim().to_owned(),
        ..new_seed
    };

    let result = Seed::create(seed_trimmed_name, user_id, &mut conn).await?;
    Ok(result)
}

/// Edits a seed in the database.
///
/// # Errors
/// If the connection to the database could not be established.
pub async fn edit(
    id: i32,
    user_id: Uuid,
    new_seed: NewSeedDto,
    app_data: &Data<AppDataInner>,
) -> Result<SeedDto, ServiceError> {
    let mut conn = app_data.pool.get().await?;
    let result = Seed::edit(id, user_id, new_seed, &mut conn).await?;
    Ok(result)
}

/// Delete the seed from the database.
///
/// # Errors
/// If the connection to the database could not be established.
pub async fn delete_by_id(
    id: i32,
    user_id: Uuid,
    app_data: &Data<AppDataInner>,
) -> Result<(), ServiceError> {
    let mut conn = app_data.pool.get().await?;
    let _ = Seed::delete_by_id(id, user_id, &mut conn).await?;
    Ok(())
}

/// Archive or unarchive a seed in the database.
///
/// # Errors
/// If the connection to the database could not be established.
pub async fn archive(
    id: i32,
    user_id: Uuid,
    archive_seed: ArchiveSeedDto,
    app_data: &Data<AppDataInner>,
) -> Result<SeedDto, ServiceError> {
    // Retrieve the seed before getting the db connection to avoid deadlocks when
    // fetching two database connections at the same time.
    let current_seed = find_by_id(id, user_id, app_data).await?;
    let mut conn = app_data.pool.get().await?;

    let current_naive_date_time = archive_seed.archived.then(|| Utc::now().naive_utc());

    // Don't archive a seed twice
    if archive_seed.archived && current_seed.archived_at.is_some() {
        return Ok(current_seed);
    }

    let result = Seed::archive(id, current_naive_date_time, user_id, &mut conn).await?;
    Ok(result)
}