70 lines
1.9 KiB
Rust
70 lines
1.9 KiB
Rust
use actix_web::{web, HttpResponse, Responder};
|
|
use rinja::Template;
|
|
use serde::Deserialize;
|
|
use sqlx::PgPool;
|
|
|
|
use crate::{
|
|
endpoints::{
|
|
availability::NewOrEditAvailabilityTemplate,
|
|
IdPath,
|
|
},
|
|
models::{find_free_time_slots, Availability, AvailabilityTime, User},
|
|
utils::ApplicationError,
|
|
};
|
|
|
|
#[derive(Deserialize)]
|
|
struct EditAvailabilityQuery {
|
|
#[serde(rename(deserialize = "wholeday"))]
|
|
whole_day: Option<bool>,
|
|
}
|
|
|
|
#[actix_web::get("/availabillity/edit/{id}")]
|
|
pub async fn get(
|
|
user: web::ReqData<User>,
|
|
pool: web::Data<PgPool>,
|
|
path: web::Path<IdPath>,
|
|
query: web::Query<EditAvailabilityQuery>,
|
|
) -> Result<impl Responder, ApplicationError> {
|
|
let Some(availability) = Availability::read_by_id(pool.get_ref(), path.id).await? else {
|
|
return Ok(HttpResponse::NotFound().finish());
|
|
};
|
|
|
|
if availability.user_id != user.id {
|
|
return Err(ApplicationError::Unauthorized);
|
|
}
|
|
|
|
let suggestions = if let AvailabilityTime::Temporarily(start, end) = availability.time {
|
|
let availabilities = Availability::read_by_user_and_date(
|
|
pool.get_ref(),
|
|
user.id,
|
|
&availability.date,
|
|
)
|
|
.await?;
|
|
|
|
find_free_time_slots(&availabilities)
|
|
.into_iter()
|
|
.filter(|(a, b)| *b == start || *a == end)
|
|
.collect()
|
|
} else {
|
|
Vec::new()
|
|
};
|
|
|
|
let time_selection = if query.whole_day.unwrap_or(availability.time == AvailabilityTime::WholeDay) {
|
|
AvailabilityTime::WholeDay
|
|
} else {
|
|
availability.time.clone()
|
|
};
|
|
|
|
println!("{:?}", availability.time);
|
|
let template = NewOrEditAvailabilityTemplate {
|
|
user: user.into_inner(),
|
|
date: availability.date,
|
|
id: Some(path.id),
|
|
time_selection,
|
|
comment: availability.comment.as_deref(),
|
|
slot_suggestions: suggestions,
|
|
};
|
|
|
|
Ok(HttpResponse::Ok().body(template.render()?))
|
|
}
|