56 lines
1.7 KiB
Rust
56 lines
1.7 KiB
Rust
use actix_web::{web, HttpResponse, Responder};
|
|
use chrono::NaiveDate;
|
|
use rinja::Template;
|
|
use serde::Deserialize;
|
|
use sqlx::PgPool;
|
|
|
|
use crate::endpoints::availability::NewOrEditAvailabilityTemplate;
|
|
use crate::models::{
|
|
find_free_time_slots, only_one_availability_exists_and_is_whole_day, Availability, AvailabilityTime, User
|
|
};
|
|
use crate::utils::ApplicationError;
|
|
use crate::{END_OF_DAY, START_OF_DAY};
|
|
|
|
#[derive(Deserialize)]
|
|
struct AvailabilityNewQuery {
|
|
#[serde(rename(deserialize = "wholeday"))]
|
|
whole_day: Option<bool>,
|
|
date: NaiveDate,
|
|
}
|
|
|
|
#[actix_web::get("/availabillity/new")]
|
|
pub async fn get(
|
|
user: web::ReqData<User>,
|
|
pool: web::Data<PgPool>,
|
|
query: web::Query<AvailabilityNewQuery>,
|
|
) -> Result<impl Responder, ApplicationError> {
|
|
let availabilities_from_user =
|
|
Availability::read_by_user_and_date(pool.get_ref(), user.id, &query.date).await?;
|
|
let free_slots = find_free_time_slots(&availabilities_from_user);
|
|
|
|
let user_can_create_availabillity = availabilities_from_user.is_empty()
|
|
|| !only_one_availability_exists_and_is_whole_day(&availabilities_from_user)
|
|
|| !free_slots.is_empty();
|
|
|
|
if !user_can_create_availabillity {
|
|
return Ok(HttpResponse::BadRequest().finish());
|
|
}
|
|
|
|
let time_selection = if query.whole_day.unwrap_or(true) {
|
|
AvailabilityTime::WholeDay
|
|
} else {
|
|
AvailabilityTime::Temporarily(START_OF_DAY, END_OF_DAY)
|
|
};
|
|
|
|
let template = NewOrEditAvailabilityTemplate {
|
|
user: user.into_inner(),
|
|
date: query.date,
|
|
time_selection,
|
|
id: None,
|
|
comment: None,
|
|
slot_suggestions: free_slots,
|
|
};
|
|
|
|
Ok(HttpResponse::Ok().body(template.render()?))
|
|
}
|