add planned_trip functionality

This commit is contained in:
2023-04-04 12:19:56 +02:00
parent 3dfc64071c
commit 3d907487a1
12 changed files with 273 additions and 32 deletions

View File

@ -1,2 +1,25 @@
use chrono::NaiveDate;
use serde::Serialize;
use sqlx::SqlitePool;
use self::planned_event::PlannedEvent;
pub mod planned_event;
pub mod tripdetails;
pub mod user;
//pub mod users;
#[derive(Serialize)]
pub struct Day {
day: NaiveDate,
planned_events: Vec<PlannedEvent>,
}
impl Day {
pub async fn new(db: &SqlitePool, day: NaiveDate) -> Self {
Self {
day,
planned_events: PlannedEvent::get_for_day(db, day).await,
}
}
}

View File

@ -0,0 +1,55 @@
use chrono::NaiveDate;
use serde::Serialize;
use sqlx::SqlitePool;
#[derive(Serialize)]
pub struct PlannedEvent {
id: i64,
name: String,
planned_amount_cox: i64,
allow_guests: bool,
trip_details_id: i64,
planned_starting_time: String,
max_people: i64,
day: String,
notes: Option<String>,
}
impl PlannedEvent {
pub async fn get_for_day(db: &SqlitePool, day: NaiveDate) -> Vec<Self> {
sqlx::query_as!(
PlannedEvent,
"
SELECT planned_event.id, name, planned_amount_cox, allow_guests, trip_details_id, planned_starting_time, max_people, day, notes
FROM planned_event
INNER JOIN trip_details ON planned_event.trip_details_id = trip_details.id
"
)
.fetch_all(db)
.await
.unwrap() //TODO: fixme
}
pub async fn new(
db: &SqlitePool,
name: String,
planned_amount_cox: i32,
allow_guests: bool,
trip_details_id: i64,
) {
sqlx::query!(
"INSERT INTO planned_event(name, planned_amount_cox, allow_guests, trip_details_id) VALUES(?, ?, ?, ?)",
name, planned_amount_cox, allow_guests, trip_details_id
)
.execute(db)
.await
.unwrap(); //TODO: fixme
}
pub async fn delete(db: &SqlitePool, id: i64) {
sqlx::query!("DELETE FROM planned_event WHERE id = ?", id)
.execute(db)
.await
.unwrap(); //TODO: fixme
}
}

33
src/model/tripdetails.rs Normal file
View File

@ -0,0 +1,33 @@
use serde::{Deserialize, Serialize};
use sqlx::{FromRow, SqlitePool};
#[derive(FromRow, Debug, Serialize, Deserialize)]
pub struct TripDetails {
pub id: i64,
planned_starting_time: String,
max_people: i32,
day: String,
notes: Option<String>,
}
impl TripDetails {
pub async fn new(
db: &SqlitePool,
planned_starting_time: String,
max_people: i32,
day: String,
notes: Option<String>,
) -> i64 {
let query = sqlx::query!(
"INSERT INTO trip_details(planned_starting_time, max_people, day, notes) VALUES(?, ?, ?, ?)" ,
planned_starting_time,
max_people,
day,
notes
)
.execute(db)
.await
.unwrap(); //TODO: fixme
query.last_insert_rowid()
}
}