2023-08-07 14:50:22 +00:00
|
|
|
use crate::controllers::members_controller::RequestDate;
|
|
|
|
use crate::controllers::HttpResult;
|
2023-08-07 15:16:00 +00:00
|
|
|
use crate::extractors::couple_extractor::FamilyAndCoupleInPath;
|
2023-08-07 14:50:22 +00:00
|
|
|
use crate::extractors::family_extractor::FamilyInPath;
|
2023-08-07 15:16:00 +00:00
|
|
|
use crate::models::{Couple, CoupleState, MemberID, PhotoID};
|
2023-08-08 08:33:12 +00:00
|
|
|
use crate::services::{couples_service, members_service, photos_service};
|
|
|
|
use actix_multipart::form::tempfile::TempFile;
|
|
|
|
use actix_multipart::form::MultipartForm;
|
2023-08-07 14:50:22 +00:00
|
|
|
use actix_web::{web, HttpResponse};
|
|
|
|
|
|
|
|
serde_with::with_prefix!(prefix_wedding "wedding_");
|
|
|
|
serde_with::with_prefix!(prefix_divorce "divorce_");
|
|
|
|
|
|
|
|
#[derive(thiserror::Error, Debug)]
|
|
|
|
enum CoupleControllerErr {
|
|
|
|
#[error("Wife and husband are identical!")]
|
|
|
|
IdenticalWifeHusband,
|
|
|
|
#[error("Wife does not exist!")]
|
|
|
|
WifeNotExisting,
|
|
|
|
#[error("Husband does not exist!")]
|
|
|
|
HusbandNotExisting,
|
|
|
|
#[error("Invalid date of wedding")]
|
|
|
|
MalformedDateOfWedding,
|
|
|
|
#[error("Invalid date of divorce")]
|
|
|
|
MalformedDateOfDivorce,
|
|
|
|
}
|
|
|
|
|
2023-08-18 09:05:32 +00:00
|
|
|
#[derive(serde::Deserialize, Clone)]
|
2023-08-07 14:50:22 +00:00
|
|
|
pub struct CoupleRequest {
|
2023-08-18 09:05:32 +00:00
|
|
|
pub wife: Option<MemberID>,
|
|
|
|
pub husband: Option<MemberID>,
|
2023-08-07 14:50:22 +00:00
|
|
|
state: Option<CoupleState>,
|
|
|
|
#[serde(flatten, with = "prefix_wedding")]
|
|
|
|
wedding: Option<RequestDate>,
|
|
|
|
#[serde(flatten, with = "prefix_divorce")]
|
|
|
|
divorce: Option<RequestDate>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CoupleRequest {
|
2023-08-18 09:06:57 +00:00
|
|
|
pub async fn to_couple(self, couple: &mut Couple) -> anyhow::Result<()> {
|
2023-08-07 14:50:22 +00:00
|
|
|
if let Some(wife) = self.wife {
|
2023-08-18 09:06:57 +00:00
|
|
|
if !members_service::exists(couple.family_id(), wife).await? {
|
2023-08-07 14:50:22 +00:00
|
|
|
return Err(CoupleControllerErr::WifeNotExisting.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.wife == self.husband {
|
|
|
|
return Err(CoupleControllerErr::IdenticalWifeHusband.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(husband) = self.husband {
|
2023-08-18 09:06:57 +00:00
|
|
|
if !members_service::exists(couple.family_id(), husband).await? {
|
2023-08-07 14:50:22 +00:00
|
|
|
return Err(CoupleControllerErr::HusbandNotExisting.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(d) = &self.wedding {
|
|
|
|
if !d.check() {
|
|
|
|
return Err(CoupleControllerErr::MalformedDateOfWedding.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(d) = &self.divorce {
|
|
|
|
if !d.check() {
|
|
|
|
return Err(CoupleControllerErr::MalformedDateOfDivorce.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
couple.set_wife(self.wife);
|
|
|
|
couple.set_husband(self.husband);
|
|
|
|
couple.set_state(self.state);
|
|
|
|
|
|
|
|
couple.wedding_year = self.wedding.as_ref().map(|m| m.year).unwrap_or_default();
|
|
|
|
couple.wedding_month = self.wedding.as_ref().map(|m| m.month).unwrap_or_default();
|
|
|
|
couple.wedding_day = self.wedding.as_ref().map(|m| m.day).unwrap_or_default();
|
|
|
|
|
|
|
|
couple.divorce_year = self.divorce.as_ref().map(|m| m.year).unwrap_or_default();
|
|
|
|
couple.divorce_month = self.divorce.as_ref().map(|m| m.month).unwrap_or_default();
|
|
|
|
couple.divorce_day = self.divorce.as_ref().map(|m| m.day).unwrap_or_default();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-07 15:16:00 +00:00
|
|
|
#[derive(serde::Serialize)]
|
|
|
|
struct CoupleAPI {
|
|
|
|
#[serde(flatten)]
|
|
|
|
member: Couple,
|
|
|
|
signed_photo_id: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CoupleAPI {
|
|
|
|
pub fn new(member: Couple) -> Self {
|
|
|
|
Self {
|
|
|
|
signed_photo_id: member.photo_id().as_ref().map(PhotoID::to_signed_hash),
|
|
|
|
member,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-07 14:50:22 +00:00
|
|
|
/// Create a new couple
|
|
|
|
pub async fn create(m: FamilyInPath, req: web::Json<CoupleRequest>) -> HttpResult {
|
|
|
|
let mut couple = couples_service::create(m.family_id()).await?;
|
|
|
|
|
2023-08-18 09:06:57 +00:00
|
|
|
if let Err(e) = req.0.to_couple(&mut couple).await {
|
2023-08-07 14:50:22 +00:00
|
|
|
log::error!("Failed to apply couple information! {e}");
|
|
|
|
couples_service::delete(&mut couple).await?;
|
|
|
|
return Ok(HttpResponse::BadRequest().body(e.to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Err(e) = couples_service::update(&mut couple).await {
|
|
|
|
log::error!("Failed to update couple information! {e}");
|
|
|
|
couples_service::delete(&mut couple).await?;
|
|
|
|
return Ok(HttpResponse::InternalServerError().finish());
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(couple))
|
|
|
|
}
|
2023-08-07 15:16:00 +00:00
|
|
|
|
|
|
|
/// Get the entire list of couples
|
|
|
|
pub async fn get_all(m: FamilyInPath) -> HttpResult {
|
|
|
|
let couples = couples_service::get_all_of_family(m.family_id())
|
|
|
|
.await?
|
|
|
|
.into_iter()
|
|
|
|
.map(CoupleAPI::new)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(couples))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a single couple entry
|
|
|
|
pub async fn get_single(m: FamilyAndCoupleInPath) -> HttpResult {
|
|
|
|
Ok(HttpResponse::Ok().json(CoupleAPI::new(m.to_couple())))
|
|
|
|
}
|
2023-08-07 17:28:44 +00:00
|
|
|
|
|
|
|
/// Update a couple information
|
|
|
|
pub async fn update(m: FamilyAndCoupleInPath, req: web::Json<CoupleRequest>) -> HttpResult {
|
|
|
|
let mut couple = m.to_couple();
|
|
|
|
|
2023-08-18 09:06:57 +00:00
|
|
|
if let Err(e) = req.0.to_couple(&mut couple).await {
|
2023-08-07 17:28:44 +00:00
|
|
|
log::error!("Failed to parse couple information {e}!");
|
|
|
|
return Ok(HttpResponse::BadRequest().body(e.to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
couples_service::update(&mut couple).await?;
|
|
|
|
|
|
|
|
Ok(HttpResponse::Accepted().finish())
|
|
|
|
}
|
2023-08-07 17:31:05 +00:00
|
|
|
|
|
|
|
/// Delete a couple
|
|
|
|
pub async fn delete(m: FamilyAndCoupleInPath) -> HttpResult {
|
|
|
|
couples_service::delete(&mut m.to_couple()).await?;
|
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|
2023-08-08 08:33:12 +00:00
|
|
|
|
|
|
|
#[derive(Debug, MultipartForm)]
|
|
|
|
pub struct UploadPhotoForm {
|
|
|
|
#[multipart(rename = "photo")]
|
|
|
|
photo: TempFile,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Upload a new photo for a user
|
|
|
|
pub async fn set_photo(
|
|
|
|
m: FamilyAndCoupleInPath,
|
|
|
|
MultipartForm(form): MultipartForm<UploadPhotoForm>,
|
|
|
|
) -> HttpResult {
|
2023-08-18 11:41:20 +00:00
|
|
|
let photo = photos_service::finalize_upload(form.photo.into()).await?;
|
2023-08-08 08:33:12 +00:00
|
|
|
let mut couple = m.to_couple();
|
|
|
|
|
|
|
|
couples_service::remove_photo(&mut couple).await?;
|
|
|
|
|
|
|
|
couple.set_photo_id(Some(photo.id()));
|
|
|
|
couples_service::update(&mut couple).await?;
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove a photo
|
|
|
|
pub async fn remove_photo(m: FamilyAndCoupleInPath) -> HttpResult {
|
|
|
|
let mut couple = m.to_couple();
|
|
|
|
couples_service::remove_photo(&mut couple).await?;
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|