31 lines
818 B
Rust
31 lines
818 B
Rust
use actix_web::{web, HttpResponse, Responder};
|
|
use sqlx::PgPool;
|
|
|
|
use crate::{
|
|
endpoints::{clothing::EditClothingPartialTemplate, IdPath},
|
|
models::{Clothing, Role, User},
|
|
utils::{ApplicationError, TemplateResponse},
|
|
};
|
|
|
|
#[actix_web::get("/clothing/edit/{id}")]
|
|
pub async fn get(
|
|
user: web::ReqData<User>,
|
|
pool: web::Data<PgPool>,
|
|
path: web::Path<IdPath>,
|
|
) -> Result<impl Responder, ApplicationError> {
|
|
if user.role != Role::Admin {
|
|
return Err(ApplicationError::Unauthorized);
|
|
}
|
|
|
|
let Some(clothing) = Clothing::read(pool.get_ref(), path.id).await? else {
|
|
return Ok(HttpResponse::NotFound().finish());
|
|
};
|
|
|
|
let template = EditClothingPartialTemplate {
|
|
id: Some(clothing.id),
|
|
name: Some(clothing.name),
|
|
};
|
|
|
|
Ok(template.to_response()?)
|
|
}
|