feat: edit and delete location
This commit is contained in:
parent
bdc57fd22c
commit
8f55757360
@ -1,18 +1,31 @@
|
||||
use actix_web::{http::header::LOCATION, web, HttpResponse, Responder};
|
||||
use actix_web::{web, HttpResponse, Responder};
|
||||
use sqlx::PgPool;
|
||||
|
||||
use crate::{endpoints::IdPath, models::User, utils::ApplicationError};
|
||||
|
||||
use crate::{
|
||||
endpoints::IdPath,
|
||||
models::{Location, Role, User},
|
||||
utils::ApplicationError,
|
||||
};
|
||||
|
||||
#[actix_web::delete("/locations/delete/{id}")]
|
||||
pub async fn delete (
|
||||
pub async fn delete(
|
||||
user: web::ReqData<User>,
|
||||
pool: web::Data<PgPool>,
|
||||
path: web::Path<IdPath>
|
||||
path: web::Path<IdPath>,
|
||||
) -> Result<impl Responder, ApplicationError> {
|
||||
if user.role != Role::AreaManager && user.role != Role::Admin {
|
||||
return Err(ApplicationError::Unauthorized);
|
||||
}
|
||||
|
||||
Ok(HttpResponse::Found()
|
||||
.insert_header((LOCATION, "/locations"))
|
||||
.insert_header(("HX-LOCATION", "/locations"))
|
||||
.finish())
|
||||
let Some(area) = Location::read_by_id(pool.get_ref(), path.id).await? else {
|
||||
return Ok(HttpResponse::NotFound().finish());
|
||||
};
|
||||
|
||||
if user.role == Role::AreaManager && area.id != user.area_id {
|
||||
return Err(ApplicationError::Unauthorized);
|
||||
}
|
||||
|
||||
Location::delete(pool.get_ref(), area.id).await?;
|
||||
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ pub async fn post(
|
||||
form: web::Form<LocationForm>,
|
||||
path: web::Path<IdPath>,
|
||||
) -> Result<impl Responder, ApplicationError> {
|
||||
if user.role == Role::AreaManager && user.role == Role::Admin {
|
||||
if user.role != Role::AreaManager && user.role != Role::Admin {
|
||||
return Err(ApplicationError::Unauthorized);
|
||||
}
|
||||
|
||||
|
@ -25,6 +25,9 @@ pub fn init(cfg: &mut ServiceConfig) {
|
||||
cfg.service(location::get_overview::get);
|
||||
cfg.service(location::get_new::get);
|
||||
cfg.service(location::post_new::post);
|
||||
cfg.service(location::get_edit::get);
|
||||
cfg.service(location::post_edit::post);
|
||||
cfg.service(location::delete::delete);
|
||||
|
||||
cfg.service(user::get_overview::get_overview);
|
||||
cfg.service(user::get_new::get_new);
|
||||
|
@ -62,18 +62,19 @@
|
||||
<em>{{ l.name }}</em>
|
||||
</div>
|
||||
<div clas="level-right">
|
||||
<a class="button is-primary is-light">
|
||||
<a class="button is-primary is-light" href="/locations/edit/{{ l.id }}">
|
||||
<svg class="icon">
|
||||
<use href="/static/feather-sprite.svg#edit" />
|
||||
</svg>
|
||||
<span>Bearbeiten</span>
|
||||
</a>
|
||||
<a class="button is-danger is-light">
|
||||
<button class="button is-danger is-light" hx-delete="/locations/delete/{{ l.id }}" hx-swap="delete"
|
||||
hx-target="closest .level" hx-trigger="confirmed">
|
||||
<svg class="icon">
|
||||
<use href="/static/feather-sprite.svg#x-circle" />
|
||||
</svg>
|
||||
<span>Löschen</span>
|
||||
</a>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
|
Loading…
x
Reference in New Issue
Block a user