From 4e1c78724ffcd185a6bc48cfe526ba72b7d08cea Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Thu, 22 Jun 2023 14:37:48 +0200 Subject: [PATCH] Add PATCH /family/{id} route --- .../src/controllers/families_controller.rs | 19 +++++++++++++++ geneit_backend/src/main.rs | 1 + .../src/services/families_service.rs | 23 +++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/geneit_backend/src/controllers/families_controller.rs b/geneit_backend/src/controllers/families_controller.rs index a62cc1b..8091007 100644 --- a/geneit_backend/src/controllers/families_controller.rs +++ b/geneit_backend/src/controllers/families_controller.rs @@ -86,6 +86,25 @@ pub async fn leave(f: FamilyInPath) -> HttpResult { Ok(HttpResponse::Accepted().finish()) } +#[derive(serde::Deserialize)] +pub struct UpdateFamilyBody { + name: String, +} + +/// Update a family +pub async fn update( + f: FamilyInPathWithAdminMembership, + req: web::Json, +) -> HttpResult { + let mut family = families_service::get_by_id(f.family_id()).await?; + family.name = req.0.name; + families_service::update_family(&family).await?; + + log::info!("User {:?} updated family {:?}", f.user_id(), f.family_id()); + + Ok(HttpResponse::Accepted().finish()) +} + /// Delete a family pub async fn delete(f: FamilyInPathWithAdminMembership) -> HttpResult { families_service::delete_family(f.family_id()).await?; diff --git a/geneit_backend/src/main.rs b/geneit_backend/src/main.rs index 7d17e8a..8fe9cb3 100644 --- a/geneit_backend/src/main.rs +++ b/geneit_backend/src/main.rs @@ -101,6 +101,7 @@ async fn main() -> std::io::Result<()> { "/family/{id}/leave", web::post().to(families_controller::leave), ) + .route("/family/{id}", web::patch().to(families_controller::update)) .route( "/family/{id}", web::delete().to(families_controller::delete), diff --git a/geneit_backend/src/services/families_service.rs b/geneit_backend/src/services/families_service.rs index fe41744..2a00354 100644 --- a/geneit_backend/src/services/families_service.rs +++ b/geneit_backend/src/services/families_service.rs @@ -49,6 +49,15 @@ pub async fn add_member( }) } +/// Find a family by id +pub async fn get_by_id(id: FamilyID) -> anyhow::Result { + db_connection::execute(|conn| { + families::table + .filter(families::dsl::id.eq(id.0)) + .first(conn) + }) +} + /// Find a family by invitation code pub async fn get_by_invitation_code(code: &str) -> anyhow::Result { db_connection::execute(|conn| { @@ -141,6 +150,20 @@ pub async fn get_family_membership( }) } +/// Update a family +pub async fn update_family(family: &Family) -> anyhow::Result<()> { + db_connection::execute(|conn| { + diesel::update(families::dsl::families.filter(families::dsl::id.eq(family.id().0))) + .set(( + families::dsl::name.eq(family.name.clone()), + families::dsl::invitation_code.eq(family.invitation_code.clone()), + )) + .execute(conn) + })?; + + Ok(()) +} + /// Delete a family pub async fn delete_family(family_id: FamilyID) -> anyhow::Result<()> { // TODO : delete members and couples