Add PATCH /family/{id} route

This commit is contained in:
Pierre HUBERT 2023-06-22 14:37:48 +02:00
parent 6bbe69d01f
commit 4e1c78724f
3 changed files with 43 additions and 0 deletions

View File

@ -86,6 +86,25 @@ pub async fn leave(f: FamilyInPath) -> HttpResult {
Ok(HttpResponse::Accepted().finish()) Ok(HttpResponse::Accepted().finish())
} }
#[derive(serde::Deserialize)]
pub struct UpdateFamilyBody {
name: String,
}
/// Update a family
pub async fn update(
f: FamilyInPathWithAdminMembership,
req: web::Json<UpdateFamilyBody>,
) -> 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 /// Delete a family
pub async fn delete(f: FamilyInPathWithAdminMembership) -> HttpResult { pub async fn delete(f: FamilyInPathWithAdminMembership) -> HttpResult {
families_service::delete_family(f.family_id()).await?; families_service::delete_family(f.family_id()).await?;

View File

@ -101,6 +101,7 @@ async fn main() -> std::io::Result<()> {
"/family/{id}/leave", "/family/{id}/leave",
web::post().to(families_controller::leave), web::post().to(families_controller::leave),
) )
.route("/family/{id}", web::patch().to(families_controller::update))
.route( .route(
"/family/{id}", "/family/{id}",
web::delete().to(families_controller::delete), web::delete().to(families_controller::delete),

View File

@ -49,6 +49,15 @@ pub async fn add_member(
}) })
} }
/// Find a family by id
pub async fn get_by_id(id: FamilyID) -> anyhow::Result<Family> {
db_connection::execute(|conn| {
families::table
.filter(families::dsl::id.eq(id.0))
.first(conn)
})
}
/// Find a family by invitation code /// Find a family by invitation code
pub async fn get_by_invitation_code(code: &str) -> anyhow::Result<Family> { pub async fn get_by_invitation_code(code: &str) -> anyhow::Result<Family> {
db_connection::execute(|conn| { 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 /// Delete a family
pub async fn delete_family(family_id: FamilyID) -> anyhow::Result<()> { pub async fn delete_family(family_id: FamilyID) -> anyhow::Result<()> {
// TODO : delete members and couples // TODO : delete members and couples