add full CRUD for boats
This commit is contained in:
parent
c0bb6d51de
commit
524d1acee2
@ -88,19 +88,37 @@ ORDER BY amount_seats DESC
|
|||||||
.is_ok()
|
.is_ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub async fn update(&self, db: &SqlitePool, is_cox: bool, is_admin: bool, is_guest: bool) {
|
pub async fn update(
|
||||||
// sqlx::query!(
|
&self,
|
||||||
// "UPDATE user SET is_cox = ?, is_admin = ?, is_guest = ? where id = ?",
|
db: &SqlitePool,
|
||||||
// is_cox,
|
name: &str,
|
||||||
// is_admin,
|
amount_seats: i64,
|
||||||
// is_guest,
|
year_built: Option<i64>,
|
||||||
// self.id
|
boatbuilder: Option<&str>,
|
||||||
// )
|
default_shipmaster_only_steering: bool,
|
||||||
// .execute(db)
|
skull: bool,
|
||||||
// .await
|
external: bool,
|
||||||
// .unwrap(); //Okay, because we can only create a User of a valid id
|
location_id: Option<i64>,
|
||||||
// }
|
owner: Option<i64>,
|
||||||
//
|
) -> bool {
|
||||||
|
sqlx::query!(
|
||||||
|
"UPDATE boat SET name=?, amount_seats=?, year_built=?, boatbuilder=?, default_shipmaster_only_steering=?, skull=?, external=?, location_id=?, owner=? WHERE id=?",
|
||||||
|
name,
|
||||||
|
amount_seats,
|
||||||
|
year_built,
|
||||||
|
boatbuilder,
|
||||||
|
default_shipmaster_only_steering,
|
||||||
|
skull,
|
||||||
|
external,
|
||||||
|
location_id,
|
||||||
|
owner,
|
||||||
|
self.id
|
||||||
|
)
|
||||||
|
.execute(db)
|
||||||
|
.await
|
||||||
|
.is_ok()
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn delete(&self, db: &SqlitePool) {
|
pub async fn delete(&self, db: &SqlitePool) {
|
||||||
sqlx::query!("DELETE FROM boat WHERE id=?", self.id)
|
sqlx::query!("DELETE FROM boat WHERE id=?", self.id)
|
||||||
.execute(db)
|
.execute(db)
|
||||||
@ -109,124 +127,70 @@ ORDER BY amount_seats DESC
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//#[cfg(test)]
|
#[cfg(test)]
|
||||||
//mod test {
|
mod test {
|
||||||
// use crate::testdb;
|
use crate::{model::boat::Boat, testdb};
|
||||||
//
|
|
||||||
// use super::User;
|
use sqlx::SqlitePool;
|
||||||
// use sqlx::SqlitePool;
|
|
||||||
//
|
#[sqlx::test]
|
||||||
// #[sqlx::test]
|
fn test_find_correct_id() {
|
||||||
// fn test_find_correct_id() {
|
let pool = testdb!();
|
||||||
// let pool = testdb!();
|
let boat = Boat::find_by_id(&pool, 1).await.unwrap();
|
||||||
// let user = User::find_by_id(&pool, 1).await.unwrap();
|
assert_eq!(boat.id, 1);
|
||||||
// assert_eq!(user.id, 1);
|
}
|
||||||
// }
|
|
||||||
//
|
#[sqlx::test]
|
||||||
// #[sqlx::test]
|
fn test_find_wrong_id() {
|
||||||
// fn test_find_wrong_id() {
|
let pool = testdb!();
|
||||||
// let pool = testdb!();
|
let boat = Boat::find_by_id(&pool, 1337).await;
|
||||||
// let user = User::find_by_id(&pool, 1337).await;
|
assert!(boat.is_none());
|
||||||
// assert!(user.is_none());
|
}
|
||||||
// }
|
|
||||||
//
|
#[sqlx::test]
|
||||||
// #[sqlx::test]
|
fn test_all() {
|
||||||
// fn test_find_correct_name() {
|
let pool = testdb!();
|
||||||
// let pool = testdb!();
|
let res = Boat::all(&pool).await;
|
||||||
// let user = User::find_by_name(&pool, "admin".into()).await.unwrap();
|
assert!(res.len() > 3);
|
||||||
// assert_eq!(user.id, 1);
|
}
|
||||||
// }
|
|
||||||
//
|
#[sqlx::test]
|
||||||
// #[sqlx::test]
|
fn test_succ_create() {
|
||||||
// fn test_find_wrong_name() {
|
let pool = testdb!();
|
||||||
// let pool = testdb!();
|
|
||||||
// let user = User::find_by_name(&pool, "name-does-not-exist".into()).await;
|
assert_eq!(
|
||||||
// assert!(user.is_none());
|
Boat::create(
|
||||||
// }
|
&pool,
|
||||||
//
|
"new-boat-name".into(),
|
||||||
// #[sqlx::test]
|
42,
|
||||||
// fn test_all() {
|
None,
|
||||||
// let pool = testdb!();
|
"Best Boatbuilder".into(),
|
||||||
// let res = User::all(&pool).await;
|
true,
|
||||||
// assert!(res.len() > 3);
|
true,
|
||||||
// }
|
false
|
||||||
//
|
)
|
||||||
// #[sqlx::test]
|
.await,
|
||||||
// fn test_succ_create() {
|
true
|
||||||
// let pool = testdb!();
|
);
|
||||||
//
|
}
|
||||||
// assert_eq!(
|
|
||||||
// User::create(&pool, "new-user-name".into(), false).await,
|
#[sqlx::test]
|
||||||
// true
|
fn test_duplicate_name_create() {
|
||||||
// );
|
let pool = testdb!();
|
||||||
// }
|
|
||||||
//
|
assert_eq!(
|
||||||
// #[sqlx::test]
|
Boat::create(
|
||||||
// fn test_duplicate_name_create() {
|
&pool,
|
||||||
// let pool = testdb!();
|
"Haichenbach".into(),
|
||||||
//
|
42,
|
||||||
// assert_eq!(User::create(&pool, "admin".into(), false).await, false);
|
None,
|
||||||
// }
|
"Best Boatbuilder".into(),
|
||||||
//
|
true,
|
||||||
// #[sqlx::test]
|
true,
|
||||||
// fn test_update() {
|
false
|
||||||
// let pool = testdb!();
|
)
|
||||||
//
|
.await,
|
||||||
// let user = User::find_by_id(&pool, 1).await.unwrap();
|
false
|
||||||
// user.update(&pool, false, false, false).await;
|
);
|
||||||
//
|
}
|
||||||
// let user = User::find_by_id(&pool, 1).await.unwrap();
|
}
|
||||||
// assert_eq!(user.is_admin, false);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// #[sqlx::test]
|
|
||||||
// fn succ_login_with_test_db() {
|
|
||||||
// let pool = testdb!();
|
|
||||||
// User::login(&pool, "admin".into(), "admin".into())
|
|
||||||
// .await
|
|
||||||
// .unwrap();
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// #[sqlx::test]
|
|
||||||
// fn wrong_pw() {
|
|
||||||
// let pool = testdb!();
|
|
||||||
// assert!(User::login(&pool, "admin".into(), "admi".into())
|
|
||||||
// .await
|
|
||||||
// .is_err());
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// #[sqlx::test]
|
|
||||||
// fn wrong_username() {
|
|
||||||
// let pool = testdb!();
|
|
||||||
// assert!(User::login(&pool, "admi".into(), "admin".into())
|
|
||||||
// .await
|
|
||||||
// .is_err());
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// #[sqlx::test]
|
|
||||||
// fn reset() {
|
|
||||||
// let pool = testdb!();
|
|
||||||
// let user = User::find_by_id(&pool, 1).await.unwrap();
|
|
||||||
//
|
|
||||||
// user.reset_pw(&pool).await;
|
|
||||||
//
|
|
||||||
// let user = User::find_by_id(&pool, 1).await.unwrap();
|
|
||||||
// assert_eq!(user.pw, None);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// #[sqlx::test]
|
|
||||||
// fn update_pw() {
|
|
||||||
// let pool = testdb!();
|
|
||||||
// let user = User::find_by_id(&pool, 1).await.unwrap();
|
|
||||||
//
|
|
||||||
// assert!(User::login(&pool, "admin".into(), "abc".into())
|
|
||||||
// .await
|
|
||||||
// .is_err());
|
|
||||||
//
|
|
||||||
// user.update_pw(&pool, "abc".into()).await;
|
|
||||||
//
|
|
||||||
// User::login(&pool, "admin".into(), "abc".into())
|
|
||||||
// .await
|
|
||||||
// .unwrap();
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
94
src/model/location.rs
Normal file
94
src/model/location.rs
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use sqlx::{FromRow, SqlitePool};
|
||||||
|
|
||||||
|
#[derive(FromRow, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct Location {
|
||||||
|
pub id: i64,
|
||||||
|
pub name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Location {
|
||||||
|
pub async fn find_by_id(db: &SqlitePool, id: i32) -> Option<Self> {
|
||||||
|
sqlx::query_as!(
|
||||||
|
Self,
|
||||||
|
"
|
||||||
|
SELECT id, name
|
||||||
|
FROM location
|
||||||
|
WHERE id like ?
|
||||||
|
",
|
||||||
|
id
|
||||||
|
)
|
||||||
|
.fetch_one(db)
|
||||||
|
.await
|
||||||
|
.ok()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn all(db: &SqlitePool) -> Vec<Self> {
|
||||||
|
sqlx::query_as!(
|
||||||
|
Self,
|
||||||
|
"
|
||||||
|
SELECT id, name
|
||||||
|
FROM location
|
||||||
|
"
|
||||||
|
)
|
||||||
|
.fetch_all(db)
|
||||||
|
.await
|
||||||
|
.unwrap() //TODO: fixme
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn create(db: &SqlitePool, name: &str) -> bool {
|
||||||
|
sqlx::query!("INSERT INTO location(name) VALUES (?)", name,)
|
||||||
|
.execute(db)
|
||||||
|
.await
|
||||||
|
.is_ok()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete(&self, db: &SqlitePool) {
|
||||||
|
sqlx::query!("DELETE FROM location WHERE id=?", self.id)
|
||||||
|
.execute(db)
|
||||||
|
.await
|
||||||
|
.unwrap(); //Okay, because we can only create a Location of a valid id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use crate::{model::location::Location, testdb};
|
||||||
|
|
||||||
|
use sqlx::SqlitePool;
|
||||||
|
|
||||||
|
#[sqlx::test]
|
||||||
|
fn test_find_correct_id() {
|
||||||
|
let pool = testdb!();
|
||||||
|
let location = Location::find_by_id(&pool, 1).await.unwrap();
|
||||||
|
assert_eq!(location.id, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[sqlx::test]
|
||||||
|
fn test_find_wrong_id() {
|
||||||
|
let pool = testdb!();
|
||||||
|
let location = Location::find_by_id(&pool, 1337).await;
|
||||||
|
assert!(location.is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[sqlx::test]
|
||||||
|
fn test_all() {
|
||||||
|
let pool = testdb!();
|
||||||
|
let res = Location::all(&pool).await;
|
||||||
|
assert!(res.len() > 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[sqlx::test]
|
||||||
|
fn test_succ_create() {
|
||||||
|
let pool = testdb!();
|
||||||
|
|
||||||
|
assert_eq!(Location::create(&pool, "new-loc-name".into(),).await, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[sqlx::test]
|
||||||
|
fn test_duplicate_name_create() {
|
||||||
|
let pool = testdb!();
|
||||||
|
|
||||||
|
assert_eq!(Location::create(&pool, "Linz".into(),).await, false);
|
||||||
|
}
|
||||||
|
}
|
@ -8,6 +8,7 @@ use self::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub mod boat;
|
pub mod boat;
|
||||||
|
pub mod location;
|
||||||
pub mod log;
|
pub mod log;
|
||||||
pub mod planned_event;
|
pub mod planned_event;
|
||||||
pub mod trip;
|
pub mod trip;
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
use crate::model::{boat::Boat, user::AdminUser};
|
use crate::model::{
|
||||||
|
boat::Boat,
|
||||||
|
location::Location,
|
||||||
|
user::{AdminUser, User},
|
||||||
|
};
|
||||||
use rocket::{
|
use rocket::{
|
||||||
form::Form,
|
form::Form,
|
||||||
get, post,
|
get, post,
|
||||||
@ -16,12 +20,16 @@ async fn index(
|
|||||||
flash: Option<FlashMessage<'_>>,
|
flash: Option<FlashMessage<'_>>,
|
||||||
) -> Template {
|
) -> Template {
|
||||||
let boats = Boat::all(db).await;
|
let boats = Boat::all(db).await;
|
||||||
|
let locations = Location::all(db).await;
|
||||||
|
let users = User::all(db).await;
|
||||||
|
|
||||||
let mut context = Context::new();
|
let mut context = Context::new();
|
||||||
if let Some(msg) = flash {
|
if let Some(msg) = flash {
|
||||||
context.insert("flash", &msg.into_inner());
|
context.insert("flash", &msg.into_inner());
|
||||||
}
|
}
|
||||||
context.insert("boats", &boats);
|
context.insert("boats", &boats);
|
||||||
|
context.insert("locations", &locations);
|
||||||
|
context.insert("users", &users);
|
||||||
context.insert("loggedin_user", &admin.user);
|
context.insert("loggedin_user", &admin.user);
|
||||||
|
|
||||||
Template::render("admin/boat/index", context.into_json())
|
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)]
|
#[derive(FromForm)]
|
||||||
//struct UserEditForm {
|
struct BoatEditForm<'r> {
|
||||||
// id: i32,
|
id: i32,
|
||||||
// is_guest: bool,
|
name: &'r str,
|
||||||
// is_cox: bool,
|
amount_seats: i64,
|
||||||
// is_admin: bool,
|
year_built: Option<i64>,
|
||||||
//}
|
boatbuilder: Option<&'r str>,
|
||||||
//
|
default_shipmaster_only_steering: bool,
|
||||||
//#[post("/user", data = "<data>")]
|
skull: bool,
|
||||||
//async fn update(
|
external: bool,
|
||||||
// db: &State<SqlitePool>,
|
location_id: Option<i64>,
|
||||||
// data: Form<UserEditForm>,
|
owner: Option<i64>,
|
||||||
// _admin: AdminUser,
|
}
|
||||||
//) -> Flash<Redirect> {
|
|
||||||
// let user = User::find_by_id(db, data.id).await;
|
#[post("/boat", data = "<data>")]
|
||||||
// let Some(user) = user else {
|
async fn update(
|
||||||
// return Flash::error(
|
db: &State<SqlitePool>,
|
||||||
// Redirect::to("/admin/user"),
|
data: Form<BoatEditForm<'_>>,
|
||||||
// format!("User with ID {} does not exist!", data.id),
|
_admin: AdminUser,
|
||||||
// )
|
) -> Flash<Redirect> {
|
||||||
// };
|
let boat = Boat::find_by_id(db, data.id).await;
|
||||||
//
|
let Some(boat) = boat else {
|
||||||
// user.update(db, data.is_cox, data.is_admin, data.is_guest)
|
return Flash::error(
|
||||||
// .await;
|
Redirect::to("/admin/boat"),
|
||||||
//
|
format!("Boat with ID {} does not exist!", data.id),
|
||||||
// Flash::success(Redirect::to("/admin/user"), "Successfully updated user")
|
)
|
||||||
//}
|
};
|
||||||
//
|
|
||||||
|
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)]
|
#[derive(FromForm)]
|
||||||
struct BoatAddForm<'r> {
|
struct BoatAddForm<'r> {
|
||||||
name: &'r str,
|
name: &'r str,
|
||||||
@ -109,5 +141,5 @@ async fn create(
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn routes() -> Vec<Route> {
|
pub fn routes() -> Vec<Route> {
|
||||||
routes![index, create, delete] //, update]
|
routes![index, create, delete, update]
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,20 @@
|
|||||||
<form action="/admin/boat" data-filterable="true" method="post" class="bg-white p-3 rounded-md flex items-end md:items-center justify-between">
|
<form action="/admin/boat" data-filterable="true" method="post" class="bg-white p-3 rounded-md flex items-end md:items-center justify-between">
|
||||||
<div class="w-full">
|
<div class="w-full">
|
||||||
<input type="hidden" name="id" value="{{ boat.id }}" />
|
<input type="hidden" name="id" value="{{ boat.id }}" />
|
||||||
<div class="font-bold mb-1">{{ boat.name }}
|
<div class="font-bold mb-1">{{ boat.name }}<br />
|
||||||
|
</div>
|
||||||
|
<div class="grid md:grid-cols-3">
|
||||||
|
|
||||||
|
|
||||||
|
{{ macros::input(label='Name', name='name', type='text', value=boat.name) }}
|
||||||
|
{{ macros::input(label='Amount Seats', name='amount_seats', type='number', min=0, value=boat.amount_seats) }}
|
||||||
|
{{ macros::select(data=locations, label='location', select_name='location_id', selected_id=boat.location_id) }}
|
||||||
|
{{ macros::select(data=users, label='users', select_name='owner', selected_id=boat.owner, default="Vereinsboot") }}
|
||||||
|
{{ macros::input(label='Baujahr', name='year_built', type='number', min=1950, value=boat.year_built) }}
|
||||||
|
{{ macros::input(label='Bootsbauer', name='boatbuilder', type='text', value=boat.boatbuilder) }}
|
||||||
|
{{ macros::checkbox(label='default_shipmaster_only_steering', name='default_shipmaster_only_steering', id=loop.index , checked=boat.default_shipmaster_only_steering) }}
|
||||||
|
{{ macros::checkbox(label='skull', name='skull', id=loop.index , checked=boat.skull) }}
|
||||||
|
{{ macros::checkbox(label='external', name='external', id=loop.index , checked=boat.external) }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="grid gap-3">
|
<div class="grid gap-3">
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
{{ macros::checkbox(label='Gäste erlauben', name='allow_guests') }}
|
{{ macros::checkbox(label='Gäste erlauben', name='allow_guests') }}
|
||||||
{{ macros::checkbox(label='Immer anzeigen', name='always_show') }}
|
{{ macros::checkbox(label='Immer anzeigen', name='always_show') }}
|
||||||
{{ macros::input(label='Anmerkungen', name='notes', type='input') }}
|
{{ macros::input(label='Anmerkungen', name='notes', type='input') }}
|
||||||
{{ macros::select(select_name='trip_type', trip_types=trip_types, default='Reguläre Ausfahrt') }}
|
{{ macros::select(data=trip_types, select_name='trip_type', default='Reguläre Ausfahrt') }}
|
||||||
|
|
||||||
<input value="Erstellen" class="w-full btn btn-primary" type="submit" />
|
<input value="Erstellen" class="w-full btn btn-primary" type="submit" />
|
||||||
</form>
|
</form>
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
{{ macros::input(label='Anzahl Ruderer (ohne Steuerperson)', name='max_people', type='number', required=true, min='0') }}
|
{{ macros::input(label='Anzahl Ruderer (ohne Steuerperson)', name='max_people', type='number', required=true, min='0') }}
|
||||||
{{ macros::checkbox(label='Gäste erlauben', name='allow_guests') }}
|
{{ macros::checkbox(label='Gäste erlauben', name='allow_guests') }}
|
||||||
{{ macros::input(label='Anmerkungen', name='notes', type='input') }}
|
{{ macros::input(label='Anmerkungen', name='notes', type='input') }}
|
||||||
{{ macros::select(select_name='trip_type', trip_types=trip_types, default='Reguläre Ausfahrt') }}
|
{{ macros::select(data=trip_types, select_name='trip_type', default='Reguläre Ausfahrt') }}
|
||||||
|
|
||||||
<input value="Erstellen" class="w-full btn btn-primary" type="submit" />
|
<input value="Erstellen" class="w-full btn btn-primary" type="submit" />
|
||||||
</form>
|
</form>
|
||||||
|
@ -47,11 +47,13 @@
|
|||||||
</label>
|
</label>
|
||||||
{% endmacro checkbox %}
|
{% endmacro checkbox %}
|
||||||
|
|
||||||
{% macro select(trip_types, select_name='trip_type', default='', selected_id='') %}
|
{% macro select(data, select_name='trip_type', default='', selected_id='') %}
|
||||||
<select name="{{ select_name }}" class="input rounded-md h-10">
|
<select name="{{ select_name }}" class="input rounded-md h-10">
|
||||||
|
{% if default %}
|
||||||
<option selected value>{{ default }}</option>
|
<option selected value>{{ default }}</option>
|
||||||
{% for trip_type in trip_types %}
|
{% endif %}
|
||||||
<option value="{{ trip_type.id }}" {% if trip_type.id == selected_id %} selected {% endif %}>{{ trip_type.name }}</option>
|
{% for d in data %}
|
||||||
|
<option value="{{ d.id }}" {% if d.id == selected_id %} selected {% endif %}>{{ d.name }}</option>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</select>
|
</select>
|
||||||
{% endmacro select %}
|
{% endmacro select %}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user