diff --git a/geneit_backend/src/controllers/couples_controller.rs b/geneit_backend/src/controllers/couples_controller.rs index 1a64aaa..52fd088 100644 --- a/geneit_backend/src/controllers/couples_controller.rs +++ b/geneit_backend/src/controllers/couples_controller.rs @@ -3,7 +3,9 @@ use crate::controllers::HttpResult; use crate::extractors::couple_extractor::FamilyAndCoupleInPath; use crate::extractors::family_extractor::FamilyInPath; use crate::models::{Couple, CoupleState, MemberID, PhotoID}; -use crate::services::{couples_service, members_service}; +use crate::services::{couples_service, members_service, photos_service}; +use actix_multipart::form::tempfile::TempFile; +use actix_multipart::form::MultipartForm; use actix_web::{web, HttpResponse}; serde_with::with_prefix!(prefix_wedding "wedding_"); @@ -150,3 +152,33 @@ pub async fn delete(m: FamilyAndCoupleInPath) -> HttpResult { couples_service::delete(&mut m.to_couple()).await?; Ok(HttpResponse::Ok().finish()) } + +#[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, +) -> HttpResult { + let photo = photos_service::finalize_upload(form.photo).await?; + 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()) +} diff --git a/geneit_backend/src/main.rs b/geneit_backend/src/main.rs index a28d1a7..5a5223f 100644 --- a/geneit_backend/src/main.rs +++ b/geneit_backend/src/main.rs @@ -183,6 +183,14 @@ async fn main() -> std::io::Result<()> { "/family/{id}/couple/{couple_id}", web::delete().to(couples_controller::delete), ) + .route( + "/family/{id}/couple/{couple_id}/photo", + web::put().to(couples_controller::set_photo), + ) + .route( + "/family/{id}/couple/{couple_id}/photo", + web::delete().to(couples_controller::remove_photo), + ) // Photos controller .route( "/photo/{id}",