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

use chrono::Utc;
use uuid::Uuid;

use crate::{
    config::data::SharedPool,
    error::ServiceError,
    model::{
        dto::{ArchiveSeedDto, NewSeedDto, Page, PageParameters, SeedDto, SeedSearchParameters},
        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,
    pool: &SharedPool,
) -> Result<Page<SeedDto>, ServiceError> {
    let mut conn = 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,
    pool: &SharedPool,
) -> Result<SeedDto, ServiceError> {
    let mut conn = 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,
    pool: &SharedPool,
) -> Result<SeedDto, ServiceError> {
    let mut conn = 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,
    pool: &SharedPool,
) -> Result<SeedDto, ServiceError> {
    let mut conn = 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, pool: &SharedPool) -> Result<(), ServiceError> {
    let mut conn = 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,
    pool: &SharedPool,
) -> 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, pool).await?;
    let mut conn = 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)
}