forked from Ruderverein-Donau-Linz/rowt
Merge pull request 'clean-backend-code' (#1008) from clean-backend-code into staging
Reviewed-on: Ruderverein-Donau-Linz/rowt#1008
This commit is contained in:
commit
4f34cc180c
@ -1,9 +1,8 @@
|
|||||||
use std::ops::DerefMut;
|
use std::ops::DerefMut;
|
||||||
|
|
||||||
use chrono::NaiveDateTime;
|
use chrono::NaiveDateTime;
|
||||||
use itertools::Itertools;
|
|
||||||
use rocket::FromForm;
|
|
||||||
use rocket::serde::{Deserialize, Serialize};
|
use rocket::serde::{Deserialize, Serialize};
|
||||||
|
use rocket::FromForm;
|
||||||
use sqlx::{FromRow, Sqlite, SqlitePool, Transaction};
|
use sqlx::{FromRow, Sqlite, SqlitePool, Transaction};
|
||||||
|
|
||||||
use crate::model::boathouse::Boathouse;
|
use crate::model::boathouse::Boathouse;
|
||||||
@ -102,24 +101,10 @@ impl Boat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn shipmaster_allowed(&self, db: &SqlitePool, user: &User) -> bool {
|
pub async fn shipmaster_allowed(&self, db: &SqlitePool, user: &User) -> bool {
|
||||||
if let Some(owner_id) = self.owner {
|
let mut tx = db.begin().await.unwrap();
|
||||||
return owner_id == user.id;
|
let ret = self.shipmaster_allowed_tx(&mut tx, user).await;
|
||||||
}
|
tx.commit().await.unwrap();
|
||||||
|
ret
|
||||||
if user.has_role(db, "Rennrudern").await {
|
|
||||||
let ottensheim = Location::find_by_name(db, "Ottensheim".into())
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
if self.location_id == ottensheim.id {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.amount_seats == 1 {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
user.allowed_to_steer(db).await
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn shipmaster_allowed_tx(
|
pub async fn shipmaster_allowed_tx(
|
||||||
@ -127,10 +112,27 @@ impl Boat {
|
|||||||
db: &mut Transaction<'_, Sqlite>,
|
db: &mut Transaction<'_, Sqlite>,
|
||||||
user: &User,
|
user: &User,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
|
if user.has_role_tx(db, "admin").await {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(owner_id) = self.owner {
|
if let Some(owner_id) = self.owner {
|
||||||
return owner_id == user.id;
|
return owner_id == user.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if user.has_role_tx(db, "Rennrudern").await {
|
||||||
|
let ottensheim = Location::find_by_name_tx(db, "Ottensheim".into())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
if self.location_id == ottensheim.id {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.name == "Externes Boot" {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
if self.amount_seats == 1 {
|
if self.amount_seats == 1 {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -257,58 +259,16 @@ ORDER BY
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn for_user(db: &SqlitePool, user: &User) -> Vec<BoatWithDetails> {
|
pub async fn for_user(db: &SqlitePool, user: &User) -> Vec<BoatWithDetails> {
|
||||||
if user.has_role(db, "admin").await {
|
let all_boats = Self::all(db).await;
|
||||||
return Self::all(db).await;
|
let mut filtered_boats = Vec::new();
|
||||||
}
|
|
||||||
let mut boats = if user.allowed_to_steer(db).await {
|
|
||||||
sqlx::query_as!(
|
|
||||||
Boat,
|
|
||||||
"
|
|
||||||
SELECT id, name, amount_seats, location_id, owner, year_built, boatbuilder, default_shipmaster_only_steering, default_destination, skull, external, deleted, convert_handoperated_possible
|
|
||||||
FROM boat
|
|
||||||
WHERE (owner is null or owner = ?) AND deleted = 0
|
|
||||||
ORDER BY amount_seats DESC
|
|
||||||
",
|
|
||||||
user.id
|
|
||||||
)
|
|
||||||
.fetch_all(db)
|
|
||||||
.await
|
|
||||||
.unwrap() //TODO: fixme
|
|
||||||
} else {
|
|
||||||
sqlx::query_as!(
|
|
||||||
Boat,
|
|
||||||
"
|
|
||||||
SELECT id, name, amount_seats, location_id, owner, year_built, boatbuilder, default_shipmaster_only_steering, default_destination, skull, external, deleted, convert_handoperated_possible
|
|
||||||
FROM boat
|
|
||||||
WHERE (owner = ? OR (owner is null and amount_seats = 1)) AND deleted = 0
|
|
||||||
ORDER BY amount_seats DESC
|
|
||||||
",
|
|
||||||
user.id
|
|
||||||
)
|
|
||||||
.fetch_all(db)
|
|
||||||
.await
|
|
||||||
.unwrap() //TODO: fixme
|
|
||||||
};
|
|
||||||
|
|
||||||
if user.has_role(db, "Rennrudern").await {
|
for boat in all_boats {
|
||||||
let ottensheim = Location::find_by_name(db, "Ottensheim".into())
|
if boat.boat.shipmaster_allowed(db, user).await {
|
||||||
.await
|
filtered_boats.push(boat);
|
||||||
.unwrap();
|
}
|
||||||
let boats_in_ottensheim = sqlx::query_as!(
|
|
||||||
Boat,
|
|
||||||
"SELECT id, name, amount_seats, location_id, owner, year_built, boatbuilder, default_shipmaster_only_steering, default_destination, skull, external, deleted, convert_handoperated_possible
|
|
||||||
FROM boat
|
|
||||||
WHERE (owner is null and location_id = ?) AND deleted = 0
|
|
||||||
ORDER BY amount_seats DESC
|
|
||||||
",ottensheim.id)
|
|
||||||
.fetch_all(db)
|
|
||||||
.await
|
|
||||||
.unwrap(); //TODO: fixme
|
|
||||||
boats.extend(boats_in_ottensheim.into_iter());
|
|
||||||
}
|
}
|
||||||
let boats = boats.into_iter().unique().collect();
|
|
||||||
|
|
||||||
Self::boats_to_details(db, boats).await
|
filtered_boats
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn all_at_location(db: &SqlitePool, location: String) -> Vec<BoatWithDetails> {
|
pub async fn all_at_location(db: &SqlitePool, location: String) -> Vec<BoatWithDetails> {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sqlx::{FromRow, SqlitePool};
|
use sqlx::{FromRow, Sqlite, SqlitePool, Transaction};
|
||||||
|
use std::ops::DerefMut;
|
||||||
|
|
||||||
#[derive(FromRow, Debug, Serialize, Deserialize)]
|
#[derive(FromRow, Debug, Serialize, Deserialize)]
|
||||||
pub struct Location {
|
pub struct Location {
|
||||||
@ -37,6 +38,20 @@ impl Location {
|
|||||||
.await
|
.await
|
||||||
.ok()
|
.ok()
|
||||||
}
|
}
|
||||||
|
pub async fn find_by_name_tx(db: &mut Transaction<'_, Sqlite>, name: String) -> Option<Self> {
|
||||||
|
sqlx::query_as!(
|
||||||
|
Self,
|
||||||
|
"
|
||||||
|
SELECT id, name
|
||||||
|
FROM location
|
||||||
|
WHERE name=?
|
||||||
|
",
|
||||||
|
name
|
||||||
|
)
|
||||||
|
.fetch_one(db.deref_mut())
|
||||||
|
.await
|
||||||
|
.ok()
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn all(db: &SqlitePool) -> Vec<Self> {
|
pub async fn all(db: &SqlitePool) -> Vec<Self> {
|
||||||
sqlx::query_as!(Self, "SELECT id, name FROM location")
|
sqlx::query_as!(Self, "SELECT id, name FROM location")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user