Merge branch 'main' of gitlab.com:PhilippHofer/rot

This commit is contained in:
Philipp 2023-07-23 18:18:29 +02:00
commit e601630e9d
6 changed files with 150 additions and 72 deletions

View File

@ -4,8 +4,7 @@
## New large features
### Logbuch
Next:
- Start with CREATE for logbook
- Next: add rower to logbook
### Guest-Scheckbuch
- guest_trip

View File

@ -2,6 +2,8 @@ use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use sqlx::{FromRow, SqlitePool};
use super::user::User;
#[derive(FromRow, Debug, Serialize, Deserialize)]
pub struct Logbook {
pub id: i64,
@ -34,22 +36,31 @@ pub struct LogbookWithBoatAndUsers {
pub shipmaster_name: String,
}
pub enum LogbookUpdateError {
NotYourEntry,
}
pub enum LogbookCreateError {
BoatAlreadyOnWater,
BoatLocked
}
impl Logbook {
//pub async fn find_by_id(db: &SqlitePool, id: i32) -> Option<Self> {
// sqlx::query_as!(
// Self,
// "
//SELECT id, name, amount_seats, location_id, owner, year_built, boatbuilder, default_shipmaster_only_steering, skull, external
//FROM boat
//WHERE id like ?
// ",
// id
// )
// .fetch_one(db)
// .await
// .ok()
//}
//
pub async fn find_by_id(db: &SqlitePool, id: i32) -> Option<Self> {
sqlx::query_as!(
Self,
"
SELECT id,boat_id,shipmaster,shipmaster_only_steering,departure,arrival,destination,distance_in_km,comments,logtype
FROM logbook
WHERE id like ?
",
id
)
.fetch_one(db)
.await
.ok()
}
// pub async fn find_by_name(db: &SqlitePool, name: &str) -> Option<Self> {
// sqlx::query_as!(
// User,
@ -110,46 +121,48 @@ impl Logbook {
distance_in_km: Option<i64>,
comments: Option<String>,
logtype: Option<i64>,
) -> bool {
) -> Result<(), LogbookCreateError> {
//Check if boat is not locked
//Check if boat is already on water
sqlx::query!(
"INSERT INTO logbook(boat_id, shipmaster, shipmaster_only_steering, departure, arrival, destination, distance_in_km, comments, logtype) VALUES (?,?,?,?,?,?,?,?,?)",
boat_id, shipmaster, shipmaster_only_steering, departure, arrival, destination, distance_in_km, comments, logtype
)
.execute(db)
.await.is_ok()
.await;
Ok(())
}
//
// pub async fn update(
// &self,
// db: &SqlitePool,
// name: &str,
// amount_seats: i64,
// year_built: Option<i64>,
// boatbuilder: Option<&str>,
// default_shipmaster_only_steering: bool,
// skull: bool,
// external: bool,
// 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 home(
&self,
db: &SqlitePool,
user: &User,
destination: String,
distance_in_km: i64,
comments: Option<String>,
logtype: Option<i64>,
) -> Result<(), LogbookUpdateError> {
if user.id != self.shipmaster {
return Err(LogbookUpdateError::NotYourEntry);
}
//TODO: check current date
let arrival = format!("{}", chrono::offset::Local::now().format("%Y-%m-%d %H:%M"));
sqlx::query!(
"UPDATE logbook SET destination=?, distance_in_km=?, comments=?, logtype=?, arrival=? WHERE id=?",
destination,
distance_in_km,
comments,
logtype,
arrival,
self.id
)
.execute(db)
.await.unwrap(); //TODO: fixme
Ok(())
}
// pub async fn delete(&self, db: &SqlitePool) {
// sqlx::query!("DELETE FROM boat WHERE id=?", self.id)
// .execute(db)

View File

@ -64,7 +64,7 @@ async fn create(
data: Form<LogAddForm>,
_adminuser: AdminUser,
) -> Flash<Redirect> {
if Logbook::create(
match Logbook::create(
db,
data.boat_id,
data.shipmaster,
@ -80,14 +80,61 @@ async fn create(
)
.await
{
Flash::success(Redirect::to("/log"), "Ausfahrt erfolgreich hinzugefügt")
} else {
Flash::error(Redirect::to("/log"), format!("Fehler beim hinzufügen!"))
}
Ok(_) => Flash::success(Redirect::to("/log"), "Ausfahrt erfolgreich hinzugefügt"),
Err(_) => Flash::error(Redirect::to("/log"), format!("Fehler beim hinzufügen!"))
}
}
#[derive(FromForm)]
struct LogHomeForm {
destination: String,
distance_in_km: i64,
comments: Option<String>,
logtype: Option<i64>,
}
#[post("/<logbook_id>", data = "<data>")]
async fn home(
db: &State<SqlitePool>,
data: Form<LogHomeForm>,
logbook_id: i32,
_adminuser: AdminUser,
) -> Flash<Redirect> {
let logbook = Logbook::find_by_id(db, logbook_id).await;
let Some(logbook) = logbook else {
return Flash::error(
Redirect::to("/admin/log"),
format!("Log with ID {} does not exist!", logbook_id),
)
};
match logbook
.home(
db,
&_adminuser.user,
data.destination.clone(), //TODO: fixme
data.distance_in_km,
data.comments.clone(), //TODO: fixme
data.logtype
)
.await
{
Ok(_) => Flash::success(Redirect::to("/log"), "Successfully updated log"),
Err(_) =>
Flash::error(
Redirect::to("/log"),
format!("Logbook with ID {} could not be updated!", logbook_id),
)
}
}
pub fn routes() -> Vec<Route> {
routes![index, create]
routes![index, create, home]
}
#[cfg(test)]

View File

@ -15,9 +15,9 @@
<div class="-space-y-px rounded-md shadow-sm">
<div>
{% if name %}
{{ macros::input(label='Name', name='name', type='input', required=true, class='rounded-t-md',hide_label=true,value=name) }}
{{ macros::input(label='Name', name='name', type='input', required=true, class='rounded-t-md',hide_label=true,value=name, autofocus=true) }}
{% else %}
{{ macros::input(label='Name', name='name', type='input', required=true, class='rounded-t-md',hide_label=true) }}
{{ macros::input(label='Name', name='name', type='input', required=true, class='rounded-t-md',hide_label=true, autofocus=true) }}
{% endif %}
</div>

View File

@ -38,10 +38,10 @@
<div class="h-8"></div>
{% endmacro header %}
{% macro input(label, name, type, required=false, class='rounded-md', value='', min='', hide_label=false) %}
{% macro input(label, name, type, required=false, class='rounded-md', value='', min='', hide_label=false, id='', autofocus=false) %}
<div>
<label for="{{ name }}" class="{% if hide_label %} sr-only {% else %} small text-gray-600 {% endif %}">{{ label }}</label>
<input id="{{ name }}" name="{{ name }}" type="{{ type }}" {% if required %} required {% endif %} value="{{ value }}" class="input {{ class }}" placeholder="{% if hide_label %}{{ label }}{% endif %}" {% if min %} min="{{ min }}" {% endif %}>
<input {% if id %} id="{{ id }}" {% else %} id="{{ name }}" {% endif %} name="{{ name }}" type="{{ type }}" {% if required %} required {% endif %} value="{{ value }}" class="input {{ class }}" placeholder="{% if hide_label %}{{ label }}{% endif %}" {% if min %} min="{{ min }}" {% endif %}{% if autofocus %}autofocus {% endif %}>
</div>
{% endmacro input %}

View File

@ -32,7 +32,7 @@
<option value="Ottensheim" distance=25 />
<option value="Ottensheim + Regattastrecke" distance=29 />
</datalist>
{{ macros::input(label="Distanz", name="distance_in_km", type="number", min=0) }}<!-- TODO: depending on destination, pre-fill this distance -->
{{ macros::input(label="Distanz", name="distance_in_km", type="number", min=0) }}
{{ macros::input(label="Kommentar", name="comments", type="text") }}
{{ macros::select(data=logtypes, select_name='logtype', default="Normal") }}
<input type="submit" />
@ -53,16 +53,34 @@
<h2 style="font-size: 100px">Am Wasser</h2>
{% for log in on_water %}
Bootsname: {{ log.boat }}<br />
Schiffsführer: {{ log.shipmaster_name }}<br />
{% if log.shipmaster_only_steering %}
Schiffsführer steuert nur
{% endif %}
Weggefahren: {{ log.departure }}<br />
Ziel: {{ log.destination }}<br />
Kommentare: {{ log.comments }}<br />
Logtype: {{ log.logtype }}<br />
<hr />
Bootsname: {{ log.boat }}<br />
Schiffsführer: {{ log.shipmaster_name }}<br />
{% if log.shipmaster_only_steering %}
Schiffsführer steuert nur
{% endif %}
Weggefahren: {{ log.departure }}<br />
Ziel: {{ log.destination }}<br />
Kommentare: {{ log.comments }}<br />
Logtype: {{ log.logtype }}<br />
{% if log.shipmaster == loggedin_user.id %}
<form action="/log/{{log.id}}" method="post">
Destination: <input type="search" list="destinations" placeholder="Destination" id="destination-home" name="destination" value="{{log.destination}}" oninput="var inputElement = document.getElementById('destination-home');
var dataList = document.getElementById('destinations');
var selectedValue = inputElement.value;
for (var i = 0; i < dataList.options.length; i++) {
if (dataList.options[i].value === selectedValue) {
var distance = dataList.options[i].getAttribute('distance');
document.getElementById('distance_in_km_home').value = distance;
break;
}
}">
{{ macros::input(label="Distanz", name="distance_in_km", id="distance_in_km_home", type="number", min=0, value=log.distance_in_km) }}
{{ macros::input(label="Kommentar", name="comments", type="text", value=log.comments) }}
{{ macros::select(data=logtypes, select_name='logtype', default="Normal", selected_id=log.logtype) }}
<input type="submit" value="AUSFAHRT BEENDEN"/>
</form>
{% endif %}
<hr />
{% endfor %}
<h2 style="font-size: 100px">Einträge</h2>
@ -75,6 +93,7 @@
Weggefahren: {{ log.departure }}<br />
Angekommen: {{ log.arrival}}<br />
Ziel: {{ log.destination }}<br />
Km: {{ log.distance_in_km }}<br />
Kommentare: {{ log.comments }}<br />
Logtype: {{ log.logtype }}<br />
<hr />