forked from Ruderverein-Donau-Linz/rowt
Push
This commit is contained in:
41
src/models/all.rs
Normal file
41
src/models/all.rs
Normal file
@ -0,0 +1,41 @@
|
||||
use sea_orm::{DatabaseConnection, ModelTrait};
|
||||
use serde::Serialize;
|
||||
|
||||
use super::{day, trip, user};
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
pub struct TripWithUser {
|
||||
pub trip: trip::Model,
|
||||
pub user: user::Model,
|
||||
}
|
||||
|
||||
impl TripWithUser {
|
||||
async fn new(trip: trip::Model, db: &DatabaseConnection) -> Self {
|
||||
Self {
|
||||
trip: trip.clone(),
|
||||
user: trip
|
||||
.find_related(user::Entity)
|
||||
.one(db)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
pub struct DayWithTrips {
|
||||
pub day: day::Model,
|
||||
pub trips: Vec<TripWithUser>,
|
||||
}
|
||||
|
||||
impl DayWithTrips {
|
||||
pub async fn new(day: day::Model, db: &DatabaseConnection) -> Self {
|
||||
let mut trips = Vec::new();
|
||||
for trip in day.find_related(trip::Entity).all(db).await.unwrap() {
|
||||
trips.push(TripWithUser::new(trip, db).await);
|
||||
}
|
||||
|
||||
Self { day, trips }
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.0
|
||||
|
||||
use sea_orm::entity::prelude::*;
|
||||
use sea_orm::{entity::prelude::*, Set};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
|
||||
@ -13,6 +13,22 @@ pub struct Model {
|
||||
pub open_registration: bool,
|
||||
}
|
||||
|
||||
impl Model {
|
||||
pub async fn find_or_create_day(date: chrono::NaiveDate, db: &DatabaseConnection) -> Model {
|
||||
let day = Entity::find_by_id(date).one(db).await.unwrap();
|
||||
match day {
|
||||
Some(day) => day,
|
||||
None => {
|
||||
let new_day = ActiveModel {
|
||||
day: Set(date),
|
||||
..Default::default()
|
||||
};
|
||||
new_day.insert(db).await.unwrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(has_many = "super::trip::Entity")]
|
||||
|
@ -2,6 +2,8 @@
|
||||
|
||||
pub mod prelude;
|
||||
|
||||
pub mod all;
|
||||
|
||||
pub mod day;
|
||||
pub mod trip;
|
||||
pub mod user;
|
||||
|
@ -1,6 +1,11 @@
|
||||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.0
|
||||
|
||||
use sea_orm::entity::prelude::*;
|
||||
use rocket::{
|
||||
http::Status,
|
||||
request::{self, FromRequest, Outcome},
|
||||
Request, State,
|
||||
};
|
||||
use sea_orm::{entity::prelude::*, Set};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
|
||||
@ -13,6 +18,49 @@ pub struct Model {
|
||||
pub is_admin: bool,
|
||||
}
|
||||
|
||||
impl Model {
|
||||
pub async fn find_or_create_user(name: &str, db: &DatabaseConnection) -> Model {
|
||||
let user = Entity::find()
|
||||
.filter(Column::Name.eq(name))
|
||||
.one(db)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
match user {
|
||||
Some(user) => user,
|
||||
None => {
|
||||
let user = ActiveModel {
|
||||
name: Set(name.clone().into()),
|
||||
..Default::default()
|
||||
};
|
||||
user.insert(db).await.unwrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum UserError {
|
||||
NoCookieSet,
|
||||
}
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl<'r> FromRequest<'r> for Model {
|
||||
type Error = UserError;
|
||||
|
||||
async fn from_request(req: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
|
||||
match req.cookies().get("name") {
|
||||
Some(name) => {
|
||||
let db = req.guard::<&'r State<DatabaseConnection>>();
|
||||
let name = name.value();
|
||||
let user = Model::find_or_create_user(name, db.await.unwrap().inner()).await;
|
||||
Outcome::Success(user)
|
||||
}
|
||||
None => Outcome::Failure((Status::Unauthorized, UserError::NoCookieSet)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
|
||||
|
Reference in New Issue
Block a user