35 lines
1.2 KiB
Rust
35 lines
1.2 KiB
Rust
|
|
use crate::model::{ModelController, Property, PropertyForCreate};
|
||
|
|
use crate::Result;
|
||
|
|
use axum::extract::{Path, State};
|
||
|
|
use axum::routing::{delete, post};
|
||
|
|
use axum::{Json, Router};
|
||
|
|
|
||
|
|
pub fn routes(mc: ModelController) -> Router {
|
||
|
|
Router::new()
|
||
|
|
.route("/properties", post(create_property).get(list_properties))
|
||
|
|
.route("/properties/:id", delete(delete_property))
|
||
|
|
.with_state(mc)
|
||
|
|
}
|
||
|
|
|
||
|
|
// region: --- REST Handlers
|
||
|
|
|
||
|
|
async fn create_property(State(mc): State<ModelController>, Json(property_fc): Json<PropertyForCreate>) -> Result<Json<Property>> {
|
||
|
|
println!("->> {:<12} - create_property", "HANDLER");
|
||
|
|
let property = mc.create_property(property_fc).await?;
|
||
|
|
Ok(Json(property))
|
||
|
|
}
|
||
|
|
|
||
|
|
async fn list_properties(State(mc): State<ModelController>) -> Result<Json<Vec<Property>>> {
|
||
|
|
println!("->> {:<12} - list_properties", "HANDLER");
|
||
|
|
let properties = mc.list_properties().await?;
|
||
|
|
Ok(Json(properties))
|
||
|
|
}
|
||
|
|
|
||
|
|
async fn delete_property(State(mc): State<ModelController>, Path(id): Path<u64>) -> Result<Json<Property>> {
|
||
|
|
println!("->> {:<12} - delete_property", "HANDLER");
|
||
|
|
let property = mc.delete_property(id).await?;
|
||
|
|
Ok(Json(property))
|
||
|
|
}
|
||
|
|
|
||
|
|
// endregion: --- REST Handlers
|