use chrono::Utc;
use uuid::Uuid;
use crate::{
config::data::SharedPool,
error::ServiceError,
model::{
dto::{ArchiveSeedDto, NewSeedDto, Page, PageParameters, SeedDto, SeedSearchParameters},
entity::Seed,
},
};
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)
}
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)
}
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)
}
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)
}
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(())
}
pub async fn archive(
id: i32,
user_id: Uuid,
archive_seed: ArchiveSeedDto,
pool: &SharedPool,
) -> Result<SeedDto, ServiceError> {
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());
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)
}