what does loco offers
This commit is contained in:
117
ht_booking/src/controllers/court.rs
Normal file
117
ht_booking/src/controllers/court.rs
Normal file
@@ -0,0 +1,117 @@
|
||||
#![allow(clippy::missing_errors_doc)]
|
||||
#![allow(clippy::unnecessary_struct_initialization)]
|
||||
#![allow(clippy::unused_async)]
|
||||
use loco_rs::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use axum::response::Redirect;
|
||||
use axum_extra::extract::Form;
|
||||
use sea_orm::{sea_query::Order, QueryOrder};
|
||||
|
||||
use crate::{
|
||||
models::_entities::courts::{ActiveModel, Column, Entity, Model},
|
||||
views,
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct Params {
|
||||
pub name: Option<String>,
|
||||
pub surface: Option<String>,
|
||||
pub indoor: Option<bool>,
|
||||
}
|
||||
|
||||
impl Params {
|
||||
fn update(&self, item: &mut ActiveModel) {
|
||||
item.name = Set(self.name.clone());
|
||||
item.surface = Set(self.surface.clone());
|
||||
item.indoor = Set(self.indoor);
|
||||
}
|
||||
}
|
||||
|
||||
async fn load_item(ctx: &AppContext, id: i32) -> Result<Model> {
|
||||
let item = Entity::find_by_id(id).one(&ctx.db).await?;
|
||||
item.ok_or_else(|| Error::NotFound)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn list(
|
||||
ViewEngine(v): ViewEngine<TeraView>,
|
||||
State(ctx): State<AppContext>,
|
||||
) -> Result<Response> {
|
||||
let item = Entity::find()
|
||||
.order_by(Column::Id, Order::Desc)
|
||||
.all(&ctx.db)
|
||||
.await?;
|
||||
views::court::list(&v, &item)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn new(
|
||||
ViewEngine(v): ViewEngine<TeraView>,
|
||||
State(_ctx): State<AppContext>,
|
||||
) -> Result<Response> {
|
||||
views::court::create(&v)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn update(
|
||||
Path(id): Path<i32>,
|
||||
State(ctx): State<AppContext>,
|
||||
Form(params): Form<Params>,
|
||||
) -> Result<Redirect> {
|
||||
let item = load_item(&ctx, id).await?;
|
||||
let mut item = item.into_active_model();
|
||||
params.update(&mut item);
|
||||
item.update(&ctx.db).await?;
|
||||
Ok(Redirect::to("../courts"))
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn edit(
|
||||
Path(id): Path<i32>,
|
||||
ViewEngine(v): ViewEngine<TeraView>,
|
||||
State(ctx): State<AppContext>,
|
||||
) -> Result<Response> {
|
||||
let item = load_item(&ctx, id).await?;
|
||||
views::court::edit(&v, &item)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn show(
|
||||
Path(id): Path<i32>,
|
||||
ViewEngine(v): ViewEngine<TeraView>,
|
||||
State(ctx): State<AppContext>,
|
||||
) -> Result<Response> {
|
||||
let item = load_item(&ctx, id).await?;
|
||||
views::court::show(&v, &item)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn add(
|
||||
State(ctx): State<AppContext>,
|
||||
Form(params): Form<Params>,
|
||||
) -> Result<Redirect> {
|
||||
let mut item = ActiveModel {
|
||||
..Default::default()
|
||||
};
|
||||
params.update(&mut item);
|
||||
item.insert(&ctx.db).await?;
|
||||
Ok(Redirect::to("courts"))
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn remove(Path(id): Path<i32>, State(ctx): State<AppContext>) -> Result<Response> {
|
||||
load_item(&ctx, id).await?.delete(&ctx.db).await?;
|
||||
format::empty()
|
||||
}
|
||||
|
||||
pub fn routes() -> Routes {
|
||||
Routes::new()
|
||||
.prefix("courts/")
|
||||
.add("/", get(list))
|
||||
.add("/", post(add))
|
||||
.add("new", get(new))
|
||||
.add("{id}", get(show))
|
||||
.add("{id}/edit", get(edit))
|
||||
.add("{id}", delete(remove))
|
||||
.add("{id}", post(update))
|
||||
}
|
||||
16
ht_booking/src/controllers/home.rs
Normal file
16
ht_booking/src/controllers/home.rs
Normal file
@@ -0,0 +1,16 @@
|
||||
#![allow(clippy::missing_errors_doc)]
|
||||
#![allow(clippy::unnecessary_struct_initialization)]
|
||||
#![allow(clippy::unused_async)]
|
||||
use loco_rs::prelude::*;
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn home(
|
||||
ViewEngine(v): ViewEngine<TeraView>,
|
||||
State(_ctx): State<AppContext>
|
||||
) -> Result<Response> {
|
||||
format::render().view(&v, "home/home.html", data!({}))
|
||||
}
|
||||
|
||||
pub fn routes() -> Routes {
|
||||
Routes::new().add("/", get(home))
|
||||
}
|
||||
@@ -1 +1,4 @@
|
||||
pub mod auth;
|
||||
|
||||
pub mod court;
|
||||
pub mod home;
|
||||
Reference in New Issue
Block a user