use actix_web::{web, HttpResponse, Responder}; use serde::Deserialize; use sqlx::PgPool; use crate::{ endpoints::IdPath, models::{Role, User}, }; #[derive(Deserialize)] struct ToggleQuery { field: String, } #[actix_web::post("/users/{id}/toggle")] pub async fn post( user: web::ReqData, pool: web::Data, path: web::Path, query: web::Query, ) -> impl Responder { // Todo: rewrite if user.id != path.id && user.role != Role::Admin && user.role != Role::AreaManager { return HttpResponse::Unauthorized().finish(); } let user = if user.id != path.id { User::read_by_id(pool.get_ref(), path.id).await.unwrap().unwrap() } else { user.into_inner() }; match query.field.as_str() { "locked" => { User::update( pool.get_ref(), user.id, None, None, None, None, None, None, None, None, Some(!user.locked), ) .await .unwrap(); if !user.locked { return HttpResponse::Ok().body(format!( r##" Entsperren
ja
"##, id = user.id)); } else { return HttpResponse::Ok().body(format!( r##" Sperren
nein
"##, id = user.id)); } } "receiveNotifications" => { User::update( pool.get_ref(), user.id, None, None, None, None, None, None, None, Some(!user.receive_notifications), None, ) .await .unwrap(); } _ => return HttpResponse::BadRequest().body("Other PATCH paths are not supported!"), }; HttpResponse::Ok().finish() }