brass/web/src/endpoints/area/post_new.rs

29 lines
829 B
Rust

use actix_web::{http::header::LOCATION, web, HttpResponse, Responder};
use sqlx::PgPool;
use crate::{endpoints::area::AreaForm, models::{Area, Role, User}};
#[actix_web::post("/area/new")]
pub async fn post(
user: web::ReqData<User>,
pool: web::Data<PgPool>,
form: web::Form<AreaForm>,
) -> impl Responder {
if user.role != Role::Admin {
return HttpResponse::Unauthorized().finish();
}
match Area::create(pool.get_ref(), &form.name).await {
Ok(_) => {
return HttpResponse::Found()
.insert_header((LOCATION, "/locations"))
.insert_header(("HX-LOCATION", "/locations"))
.finish()
}
Err(err) => {
println!("{}", err);
return HttpResponse::InternalServerError().finish();
}
}
}