backend/service/
guided_tours.rs

1//! Service layer for guided tours.
2
3use uuid::Uuid;
4
5use crate::{
6    config::data::SharedPool,
7    error::ServiceError,
8    model::{
9        dto::{GuidedToursDto, UpdateGuidedToursDto},
10        entity::GuidedTours,
11    },
12};
13
14/// Setup the Guided Tour status for a new user.
15///
16/// # Errors
17/// If the connection to the database could not be established.
18pub async fn setup(user_id: Uuid, pool: &SharedPool) -> Result<GuidedToursDto, ServiceError> {
19    let mut conn = pool.get().await?;
20    let result = GuidedTours::setup(user_id, &mut conn).await?;
21    Ok(result)
22}
23
24/// Get the Guided Tour status for a user.
25/// A new Guided Tour status will be created if none for this user exist.
26///
27/// # Errors
28/// If the connection to the database could not be established.
29pub async fn find_by_user(
30    user_id: Uuid,
31    pool: &SharedPool,
32) -> Result<GuidedToursDto, ServiceError> {
33    let mut conn = pool.get().await?;
34    let result = GuidedTours::find_by_user(user_id, &mut conn).await;
35    match result {
36        Ok(result) => Ok(result),
37        Err(diesel::result::Error::NotFound) => Ok(GuidedTours::setup(user_id, &mut conn).await?),
38        Err(e) => Err(e.into()),
39    }
40}
41
42/// Update the Guided Tour status for a user.
43///
44/// # Errors
45/// If the connection to the database could not be established.
46pub async fn update(
47    status_update: UpdateGuidedToursDto,
48    user_id: Uuid,
49    pool: &SharedPool,
50) -> Result<GuidedToursDto, ServiceError> {
51    let mut conn = pool.get().await?;
52    let result = GuidedTours::update(status_update, user_id, &mut conn).await?;
53    Ok(result)
54}