use actix_web::web::Data;
use uuid::Uuid;
use crate::{
config::data::AppDataInner,
error::ServiceError,
model::{
dto::{GuidedToursDto, UpdateGuidedToursDto},
entity::GuidedTours,
},
};
pub async fn setup(
user_id: Uuid,
app_data: &Data<AppDataInner>,
) -> Result<GuidedToursDto, ServiceError> {
let mut conn = app_data.pool.get().await?;
let result = GuidedTours::setup(user_id, &mut conn).await?;
Ok(result)
}
pub async fn find_by_user(
user_id: Uuid,
app_data: &Data<AppDataInner>,
) -> Result<GuidedToursDto, ServiceError> {
let mut conn = app_data.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()),
}
}
pub async fn update(
status_update: UpdateGuidedToursDto,
user_id: Uuid,
app_data: &Data<AppDataInner>,
) -> Result<GuidedToursDto, ServiceError> {
let mut conn = app_data.pool.get().await?;
let result = GuidedTours::update(status_update, user_id, &mut conn).await?;
Ok(result)
}