Can get couple information

This commit is contained in:
2023-08-07 17:16:00 +02:00
parent ff8e3cce9d
commit 4997132bf8
4 changed files with 122 additions and 1 deletions

View File

@ -1,7 +1,8 @@
use crate::controllers::members_controller::RequestDate;
use crate::controllers::HttpResult;
use crate::extractors::couple_extractor::FamilyAndCoupleInPath;
use crate::extractors::family_extractor::FamilyInPath;
use crate::models::{Couple, CoupleState, MemberID};
use crate::models::{Couple, CoupleState, MemberID, PhotoID};
use crate::services::{couples_service, members_service};
use actix_web::{web, HttpResponse};
@ -79,6 +80,22 @@ impl CoupleRequest {
}
}
#[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,
}
}
}
/// 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?;
@ -97,3 +114,19 @@ pub async fn create(m: FamilyInPath, req: web::Json<CoupleRequest>) -> HttpResul
Ok(HttpResponse::Ok().json(couple))
}
/// 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())))
}