final touches

This commit is contained in:
2023-02-16 11:26:41 +01:00
parent a95445c53c
commit 6e54b29288
11 changed files with 114 additions and 32 deletions

View File

@ -1,4 +1,4 @@
use sea_orm::{DatabaseConnection, ModelTrait};
use sea_orm::{ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter};
use serde::Serialize;
use super::{day, trip, user};
@ -10,16 +10,8 @@ pub struct TripWithUser {
}
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(),
}
fn new(trip: trip::Model, user: user::Model) -> Self {
Self { trip, user }
}
}
@ -32,8 +24,17 @@ pub struct DayWithTrips {
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);
let trips_with_users: Vec<(trip::Model, Option<user::Model>)> = trip::Entity::find()
.filter(trip::Column::Day.eq(day.day))
.find_also_related(user::Entity)
.all(db)
.await
.unwrap();
println!("{:#?}", trips_with_users);
for (trip, users) in trips_with_users {
trips.push(TripWithUser::new(trip, users.unwrap()));
}
Self { day, trips }

View File

@ -14,6 +14,7 @@ pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub name: String,
pub pw: Option<String>,
pub is_cox: bool,
pub is_admin: bool,
}
@ -53,7 +54,7 @@ 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") {
match req.cookies().get_private("name") {
Some(name) => {
let db = req.guard::<&'r State<DatabaseConnection>>();
let name = name.value();
@ -70,7 +71,7 @@ impl<'r> FromRequest<'r> for AdminUser {
type Error = UserError;
async fn from_request(req: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
match req.cookies().get("name") {
match req.cookies().get_private("name") {
Some(name) => {
let db = req.guard::<&'r State<DatabaseConnection>>();
let name = name.value();