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 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}")]
|
#[actix_web::delete("/locations/delete/{id}")]
|
||||||
pub async fn delete (
|
pub async fn delete(
|
||||||
user: web::ReqData<User>,
|
user: web::ReqData<User>,
|
||||||
pool: web::Data<PgPool>,
|
pool: web::Data<PgPool>,
|
||||||
path: web::Path<IdPath>
|
path: web::Path<IdPath>,
|
||||||
) -> Result<impl Responder, ApplicationError> {
|
) -> Result<impl Responder, ApplicationError> {
|
||||||
|
if user.role != Role::AreaManager && user.role != Role::Admin {
|
||||||
|
return Err(ApplicationError::Unauthorized);
|
||||||
|
}
|
||||||
|
|
||||||
Ok(HttpResponse::Found()
|
let Some(area) = Location::read_by_id(pool.get_ref(), path.id).await? else {
|
||||||
.insert_header((LOCATION, "/locations"))
|
return Ok(HttpResponse::NotFound().finish());
|
||||||
.insert_header(("HX-LOCATION", "/locations"))
|
};
|
||||||
.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>,
|
form: web::Form<LocationForm>,
|
||||||
path: web::Path<IdPath>,
|
path: web::Path<IdPath>,
|
||||||
) -> Result<impl Responder, ApplicationError> {
|
) -> 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);
|
return Err(ApplicationError::Unauthorized);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,6 +25,9 @@ pub fn init(cfg: &mut ServiceConfig) {
|
|||||||
cfg.service(location::get_overview::get);
|
cfg.service(location::get_overview::get);
|
||||||
cfg.service(location::get_new::get);
|
cfg.service(location::get_new::get);
|
||||||
cfg.service(location::post_new::post);
|
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_overview::get_overview);
|
||||||
cfg.service(user::get_new::get_new);
|
cfg.service(user::get_new::get_new);
|
||||||
|
@ -62,18 +62,19 @@
|
|||||||
<em>{{ l.name }}</em>
|
<em>{{ l.name }}</em>
|
||||||
</div>
|
</div>
|
||||||
<div clas="level-right">
|
<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">
|
<svg class="icon">
|
||||||
<use href="/static/feather-sprite.svg#edit" />
|
<use href="/static/feather-sprite.svg#edit" />
|
||||||
</svg>
|
</svg>
|
||||||
<span>Bearbeiten</span>
|
<span>Bearbeiten</span>
|
||||||
</a>
|
</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">
|
<svg class="icon">
|
||||||
<use href="/static/feather-sprite.svg#x-circle" />
|
<use href="/static/feather-sprite.svg#x-circle" />
|
||||||
</svg>
|
</svg>
|
||||||
<span>Löschen</span>
|
<span>Löschen</span>
|
||||||
</a>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user