Can set photo to couple
This commit is contained in:
parent
3a219ff9e2
commit
0262889913
@ -3,7 +3,9 @@ use crate::controllers::HttpResult;
|
|||||||
use crate::extractors::couple_extractor::FamilyAndCoupleInPath;
|
use crate::extractors::couple_extractor::FamilyAndCoupleInPath;
|
||||||
use crate::extractors::family_extractor::FamilyInPath;
|
use crate::extractors::family_extractor::FamilyInPath;
|
||||||
use crate::models::{Couple, CoupleState, MemberID, PhotoID};
|
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};
|
use actix_web::{web, HttpResponse};
|
||||||
|
|
||||||
serde_with::with_prefix!(prefix_wedding "wedding_");
|
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?;
|
couples_service::delete(&mut m.to_couple()).await?;
|
||||||
Ok(HttpResponse::Ok().finish())
|
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())
|
||||||
|
}
|
||||||
|
@ -183,6 +183,14 @@ async fn main() -> std::io::Result<()> {
|
|||||||
"/family/{id}/couple/{couple_id}",
|
"/family/{id}/couple/{couple_id}",
|
||||||
web::delete().to(couples_controller::delete),
|
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
|
// Photos controller
|
||||||
.route(
|
.route(
|
||||||
"/photo/{id}",
|
"/photo/{id}",
|
||||||
|
Loading…
Reference in New Issue
Block a user