25 lines
735 B
Rust
25 lines
735 B
Rust
use actix_web::{web, HttpResponse, Responder};
|
|
use sqlx::PgPool;
|
|
|
|
use crate::{endpoints::IdPath, utils::ApplicationError};
|
|
use brass_db::models::{Availability, User};
|
|
|
|
#[actix_web::delete("/availability/delete/{id}")]
|
|
pub async fn delete(
|
|
user: web::ReqData<User>,
|
|
pool: web::Data<PgPool>,
|
|
path: web::Path<IdPath>,
|
|
) -> Result<impl Responder, ApplicationError> {
|
|
let Some(availability) = Availability::read_by_id(pool.get_ref(), path.id).await? else {
|
|
return Ok(HttpResponse::NotFound().finish());
|
|
};
|
|
|
|
if availability.user_id != user.id {
|
|
return Err(ApplicationError::Unauthorized);
|
|
}
|
|
|
|
Availability::delete(pool.get_ref(), availability.id).await?;
|
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
}
|