Can set photo to couple

This commit is contained in:
Pierre HUBERT 2023-08-08 10:33:12 +02:00
parent 3a219ff9e2
commit 0262889913
2 changed files with 41 additions and 1 deletions

View File

@ -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<UploadPhotoForm>,
) -> 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())
}

View File

@ -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}",