create create/delete/view function for boats
This commit is contained in:
113
src/tera/admin/boat.rs
Normal file
113
src/tera/admin/boat.rs
Normal file
@ -0,0 +1,113 @@
|
||||
use crate::model::{boat::Boat, user::AdminUser};
|
||||
use rocket::{
|
||||
form::Form,
|
||||
get, post,
|
||||
request::FlashMessage,
|
||||
response::{Flash, Redirect},
|
||||
routes, FromForm, Route, State,
|
||||
};
|
||||
use rocket_dyn_templates::{tera::Context, Template};
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
#[get("/boat")]
|
||||
async fn index(
|
||||
db: &State<SqlitePool>,
|
||||
admin: AdminUser,
|
||||
flash: Option<FlashMessage<'_>>,
|
||||
) -> Template {
|
||||
let boats = Boat::all(db).await;
|
||||
|
||||
let mut context = Context::new();
|
||||
if let Some(msg) = flash {
|
||||
context.insert("flash", &msg.into_inner());
|
||||
}
|
||||
context.insert("boats", &boats);
|
||||
context.insert("loggedin_user", &admin.user);
|
||||
|
||||
Template::render("admin/boat/index", context.into_json())
|
||||
}
|
||||
|
||||
#[get("/boat/<boat>/delete")]
|
||||
async fn delete(db: &State<SqlitePool>, _admin: AdminUser, boat: i32) -> Flash<Redirect> {
|
||||
let boat = Boat::find_by_id(db, boat).await;
|
||||
match boat {
|
||||
Some(boat) => {
|
||||
boat.delete(db).await;
|
||||
Flash::success(
|
||||
Redirect::to("/admin/boat"),
|
||||
format!("Sucessfully deleted boat {}", boat.name),
|
||||
)
|
||||
}
|
||||
None => Flash::error(Redirect::to("/admin/boat"), "Boat does not exist"),
|
||||
}
|
||||
}
|
||||
|
||||
//#[derive(FromForm)]
|
||||
//struct UserEditForm {
|
||||
// id: i32,
|
||||
// is_guest: bool,
|
||||
// is_cox: bool,
|
||||
// is_admin: bool,
|
||||
//}
|
||||
//
|
||||
//#[post("/user", data = "<data>")]
|
||||
//async fn update(
|
||||
// db: &State<SqlitePool>,
|
||||
// data: Form<UserEditForm>,
|
||||
// _admin: AdminUser,
|
||||
//) -> Flash<Redirect> {
|
||||
// let user = User::find_by_id(db, data.id).await;
|
||||
// let Some(user) = user else {
|
||||
// return Flash::error(
|
||||
// Redirect::to("/admin/user"),
|
||||
// format!("User with ID {} does not exist!", data.id),
|
||||
// )
|
||||
// };
|
||||
//
|
||||
// user.update(db, data.is_cox, data.is_admin, data.is_guest)
|
||||
// .await;
|
||||
//
|
||||
// Flash::success(Redirect::to("/admin/user"), "Successfully updated user")
|
||||
//}
|
||||
//
|
||||
#[derive(FromForm)]
|
||||
struct BoatAddForm<'r> {
|
||||
name: &'r str,
|
||||
amount_seats: i64,
|
||||
year_built: Option<i64>,
|
||||
boatbuilder: Option<&'r str>,
|
||||
default_shipmaster_only_steering: bool,
|
||||
skull: bool,
|
||||
external: bool,
|
||||
}
|
||||
|
||||
#[post("/boat/new", data = "<data>")]
|
||||
async fn create(
|
||||
db: &State<SqlitePool>,
|
||||
data: Form<BoatAddForm<'_>>,
|
||||
_admin: AdminUser,
|
||||
) -> Flash<Redirect> {
|
||||
if Boat::create(
|
||||
db,
|
||||
data.name,
|
||||
data.amount_seats,
|
||||
data.year_built,
|
||||
data.boatbuilder,
|
||||
data.default_shipmaster_only_steering,
|
||||
data.skull,
|
||||
data.external,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Flash::success(Redirect::to("/admin/boat"), "Successfully created boat")
|
||||
} else {
|
||||
Flash::error(
|
||||
Redirect::to("/admin/boat"),
|
||||
format!("Error while creating boat {} in DB", data.name),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes![index, create, delete] //, update]
|
||||
}
|
@ -3,6 +3,7 @@ use sqlx::SqlitePool;
|
||||
|
||||
use crate::{model::log::Log, tera::Config};
|
||||
|
||||
pub mod boat;
|
||||
pub mod planned_event;
|
||||
pub mod user;
|
||||
|
||||
@ -17,6 +18,7 @@ async fn rss(db: &State<SqlitePool>, key: Option<&str>, config: &State<Config>)
|
||||
pub fn routes() -> Vec<Route> {
|
||||
let mut ret = Vec::new();
|
||||
ret.append(&mut user::routes());
|
||||
ret.append(&mut boat::routes());
|
||||
ret.append(&mut planned_event::routes());
|
||||
ret.append(&mut routes![rss]);
|
||||
ret
|
||||
|
Reference in New Issue
Block a user