backend/service/
plants.rs

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

use crate::{
    config::data::SharedPool,
    error::ServiceError,
    model::{
        dto::{Page, PageParameters, PlantsSearchParameters, PlantsSummaryDto},
        entity::Plants,
    },
};

/// Search plants from in the database.
///
/// # Errors
/// If the connection to the database could not be established.
pub async fn find(
    search_parameters: PlantsSearchParameters,
    page_parameters: PageParameters,
    pool: &SharedPool,
) -> Result<Page<PlantsSummaryDto>, ServiceError> {
    let mut conn = pool.get().await?;
    let result = match &search_parameters.name {
        // Empty search queries should be treated like nonexistent queries.
        Some(query) if !query.is_empty() => {
            Plants::search(query, page_parameters, &mut conn).await?
        }
        _ => Plants::find_any(page_parameters, &mut conn).await?,
    };

    Ok(result)
}

/// Find the plant by id from the database.
///
/// # Errors
/// If the connection to the database could not be established.
pub async fn find_by_id(id: i32, pool: &SharedPool) -> Result<PlantsSummaryDto, ServiceError> {
    let mut conn = pool.get().await?;
    let result = Plants::find_by_id(id, &mut conn).await?;
    Ok(result)
}