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

use uuid::Uuid;

use crate::{
    config::data::SharedPool,
    error::ServiceError,
    model::{
        dto::{GuidedToursDto, UpdateGuidedToursDto},
        entity::GuidedTours,
    },
};

/// Setup the Guided Tour status for a new user.
///
/// # Errors
/// If the connection to the database could not be established.
pub async fn setup(user_id: Uuid, pool: &SharedPool) -> Result<GuidedToursDto, ServiceError> {
    let mut conn = pool.get().await?;
    let result = GuidedTours::setup(user_id, &mut conn).await?;
    Ok(result)
}

/// Get the Guided Tour status for a user.
/// A new Guided Tour status will be created if none for this user exist.
///
/// # Errors
/// If the connection to the database could not be established.
pub async fn find_by_user(
    user_id: Uuid,
    pool: &SharedPool,
) -> Result<GuidedToursDto, ServiceError> {
    let mut conn = pool.get().await?;
    let result = GuidedTours::find_by_user(user_id, &mut conn).await;
    match result {
        Ok(result) => Ok(result),
        Err(diesel::result::Error::NotFound) => Ok(GuidedTours::setup(user_id, &mut conn).await?),
        Err(e) => Err(e.into()),
    }
}

/// Update the Guided Tour status for a user.
///
/// # Errors
/// If the connection to the database could not be established.
pub async fn update(
    status_update: UpdateGuidedToursDto,
    user_id: Uuid,
    pool: &SharedPool,
) -> Result<GuidedToursDto, ServiceError> {
    let mut conn = pool.get().await?;
    let result = GuidedTours::update(status_update, user_id, &mut conn).await?;
    Ok(result)
}