use actix_http::StatusCode;
use chrono::Days;
use crate::config::data::SharedPool;
use crate::error::ServiceError;
use crate::model::dto::core::TimelinePage;
use crate::model::dto::shadings::{
DeleteShadingDto, NewShadingDto, ShadingDto, ShadingSearchParameters, UpdateShadingDto,
};
use crate::model::entity::shadings::Shading;
use crate::model::entity::shadings_impl::FindShadingsParameters;
pub const TIME_LINE_LOADING_OFFSET_DAYS: u64 = 356;
pub async fn find(
search_parameters: ShadingSearchParameters,
pool: &SharedPool,
) -> Result<TimelinePage<ShadingDto>, ServiceError> {
let mut conn = pool.get().await?;
let from = search_parameters
.relative_to_date
.checked_sub_days(Days::new(TIME_LINE_LOADING_OFFSET_DAYS))
.ok_or_else(|| {
ServiceError::new(
StatusCode::BAD_REQUEST,
"Could not add days to relative_to_date",
)
})?;
let to = search_parameters
.relative_to_date
.checked_add_days(Days::new(TIME_LINE_LOADING_OFFSET_DAYS))
.ok_or_else(|| {
ServiceError::new(
StatusCode::BAD_REQUEST,
"Could not add days to relative_to_date",
)
})?;
let search_parameters = FindShadingsParameters {
layer_id: search_parameters.layer_id,
from,
to,
};
let result = Shading::find(search_parameters, &mut conn).await?;
Ok(TimelinePage {
results: result,
from,
to,
})
}
pub async fn create(
dto: Vec<NewShadingDto>,
pool: &SharedPool,
) -> Result<Vec<ShadingDto>, ServiceError> {
let mut conn = pool.get().await?;
let result = Shading::create(dto, &mut conn).await?;
Ok(result)
}
pub async fn update(
dto: UpdateShadingDto,
pool: &SharedPool,
) -> Result<Vec<ShadingDto>, ServiceError> {
let mut conn = pool.get().await?;
let result = Shading::update(dto, &mut conn).await?;
Ok(result)
}
pub async fn delete_by_ids(
dtos: Vec<DeleteShadingDto>,
pool: &SharedPool,
) -> Result<(), ServiceError> {
let mut conn = pool.get().await?;
let _ = Shading::delete_by_ids(dtos, &mut conn).await?;
Ok(())
}