Can update couple information

This commit is contained in:
Pierre HUBERT 2023-08-07 19:28:44 +02:00
parent 4997132bf8
commit ea84c45e8a
3 changed files with 19 additions and 1 deletions

View File

@ -130,3 +130,17 @@ pub async fn get_all(m: FamilyInPath) -> HttpResult {
pub async fn get_single(m: FamilyAndCoupleInPath) -> HttpResult { pub async fn get_single(m: FamilyAndCoupleInPath) -> HttpResult {
Ok(HttpResponse::Ok().json(CoupleAPI::new(m.to_couple()))) Ok(HttpResponse::Ok().json(CoupleAPI::new(m.to_couple())))
} }
/// Update a couple information
pub async fn update(m: FamilyAndCoupleInPath, req: web::Json<CoupleRequest>) -> HttpResult {
let mut couple = m.to_couple();
if let Err(e) = req.0.to_couple(&mut couple).await {
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())
}

View File

@ -175,6 +175,10 @@ async fn main() -> std::io::Result<()> {
"/family/{id}/couple/{couple_id}", "/family/{id}/couple/{couple_id}",
web::get().to(couples_controller::get_single), web::get().to(couples_controller::get_single),
) )
.route(
"/family/{id}/couple/{couple_id}",
web::put().to(couples_controller::update),
)
// Photos controller // Photos controller
.route( .route(
"/photo/{id}", "/photo/{id}",

View File

@ -332,7 +332,7 @@ impl CoupleState {
} }
pub fn parse_str(s: &str) -> Option<Self> { pub fn parse_str(s: &str) -> Option<Self> {
serde_json::from_str(s).ok() serde_json::from_str(&format!("\"{s}\"")).ok()
} }
pub fn states_list() -> Vec<CoupleStateDesc> { pub fn states_list() -> Vec<CoupleStateDesc> {