add full CRUD for boats
This commit is contained in:
@ -1,4 +1,8 @@
|
||||
use crate::model::{boat::Boat, user::AdminUser};
|
||||
use crate::model::{
|
||||
boat::Boat,
|
||||
location::Location,
|
||||
user::{AdminUser, User},
|
||||
};
|
||||
use rocket::{
|
||||
form::Form,
|
||||
get, post,
|
||||
@ -16,12 +20,16 @@ async fn index(
|
||||
flash: Option<FlashMessage<'_>>,
|
||||
) -> Template {
|
||||
let boats = Boat::all(db).await;
|
||||
let locations = Location::all(db).await;
|
||||
let users = User::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("locations", &locations);
|
||||
context.insert("users", &users);
|
||||
context.insert("loggedin_user", &admin.user);
|
||||
|
||||
Template::render("admin/boat/index", context.into_json())
|
||||
@ -42,34 +50,58 @@ async fn delete(db: &State<SqlitePool>, _admin: AdminUser, boat: i32) -> Flash<R
|
||||
}
|
||||
}
|
||||
|
||||
//#[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 BoatEditForm<'r> {
|
||||
id: i32,
|
||||
name: &'r str,
|
||||
amount_seats: i64,
|
||||
year_built: Option<i64>,
|
||||
boatbuilder: Option<&'r str>,
|
||||
default_shipmaster_only_steering: bool,
|
||||
skull: bool,
|
||||
external: bool,
|
||||
location_id: Option<i64>,
|
||||
owner: Option<i64>,
|
||||
}
|
||||
|
||||
#[post("/boat", data = "<data>")]
|
||||
async fn update(
|
||||
db: &State<SqlitePool>,
|
||||
data: Form<BoatEditForm<'_>>,
|
||||
_admin: AdminUser,
|
||||
) -> Flash<Redirect> {
|
||||
let boat = Boat::find_by_id(db, data.id).await;
|
||||
let Some(boat) = boat else {
|
||||
return Flash::error(
|
||||
Redirect::to("/admin/boat"),
|
||||
format!("Boat with ID {} does not exist!", data.id),
|
||||
)
|
||||
};
|
||||
|
||||
if !boat
|
||||
.update(
|
||||
db,
|
||||
data.name,
|
||||
data.amount_seats,
|
||||
data.year_built,
|
||||
data.boatbuilder,
|
||||
data.default_shipmaster_only_steering,
|
||||
data.skull,
|
||||
data.external,
|
||||
data.location_id,
|
||||
data.owner,
|
||||
)
|
||||
.await
|
||||
{
|
||||
return Flash::error(
|
||||
Redirect::to("/admin/boat"),
|
||||
format!("Boat with ID {} could not be updated!", data.id),
|
||||
);
|
||||
}
|
||||
|
||||
Flash::success(Redirect::to("/admin/boat"), "Successfully updated boat")
|
||||
}
|
||||
|
||||
#[derive(FromForm)]
|
||||
struct BoatAddForm<'r> {
|
||||
name: &'r str,
|
||||
@ -109,5 +141,5 @@ async fn create(
|
||||
}
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes![index, create, delete] //, update]
|
||||
routes![index, create, delete, update]
|
||||
}
|
||||
|
Reference in New Issue
Block a user