From 129e8d34906584b5e6c3eeeb18958fc5e2c39c33 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 23 Aug 2020 14:40:03 +0200 Subject: Implement adding screenings in the web ui. --- src/controllers/screening.rs | 60 ++++++++++++++++++++++++++++++++++++++++--- src/db.rs | 24 +++++++++-------- src/lib.rs | 7 ++++- templates/screening/list.tera | 22 ++++++++++++++++ templates/screening/new.tera | 36 ++++++++++++++++++++++++++ 5 files changed, 134 insertions(+), 15 deletions(-) create mode 100644 templates/screening/list.tera create mode 100644 templates/screening/new.tera diff --git a/src/controllers/screening.rs b/src/controllers/screening.rs index 5052075..b76ae65 100644 --- a/src/controllers/screening.rs +++ b/src/controllers/screening.rs @@ -18,15 +18,67 @@ use crate::{ db, - models::AggregatedScreening, + models, }; -use rocket::get; +use std::result::Result; +use rocket::{get, post}; +use rocket::request::{Form, FromForm}; +use rocket::response::Redirect; use rocket_contrib::{ json::Json, + templates::Template, }; +use serde::Serialize; +use std::error::Error; -#[get("/")] -pub fn get_aggregated_screenings(db: db::Connection) -> Json> { +#[get("/", format = "application/json")] +pub fn get_aggregated_screenings(db: db::Connection) -> Json> { Json(db.get_aggregated_screenings().unwrap()) } + +#[get("/", rank = 2)] +pub fn list_screenings(db: db::Connection) -> Result> { + #[derive(Serialize)] + struct Context { + screenings: Vec, + } + + let ctx = Context { screenings: db.get_aggregated_screenings()? }; + Ok(Template::render("screening/list", &ctx)) +} + +#[get("/new")] +pub fn new_screening(db: db::Connection) -> Result> { + #[derive(Serialize)] + struct Context { + rooms: Vec, + films: Vec, + } + + let ctx = Context { + rooms: db.get_rooms()?, + films: db.get_films()?, + }; + + Ok(Template::render("screening/new", &ctx)) +} + +#[derive(FromForm)] +pub struct NewScreeningForm { + film_id: i32, + room_id: i32, + date: String, + start_time: String, + end_time: String, +} + +#[post("/", format = "application/x-www-form-urlencoded", data = "")] +pub fn create_screening(db: db::Connection, screening: Form) -> Result> { + let date = chrono::NaiveDate::parse_from_str(dbg!(&screening.date), "%Y-%m-%d")?; + let start_time = chrono::NaiveTime::parse_from_str(dbg!(&screening.start_time), "%H:%M")?; + let end_time = chrono::NaiveTime::parse_from_str(dbg!(&screening.end_time), "%H:%M")?; + + db.create_screening(dbg!(screening.room_id), dbg!(screening.film_id), date, start_time, end_time)?; + Ok(Redirect::to("/screenings")) +} diff --git a/src/db.rs b/src/db.rs index 60ff4fc..cf1c0fb 100644 --- a/src/db.rs +++ b/src/db.rs @@ -34,25 +34,29 @@ pub struct Connection(diesel::PgConnection); impl Connection { pub fn create_screening( &self, - room: &models::Room, - film: &models::Film, + room_id: i32, + film_id: i32, date: chrono::NaiveDate, start_time: chrono::NaiveTime, end_time: chrono::NaiveTime) -> QueryResult { - let s = models::NewScreening { - room_id: room.id, - film_id: film.id, - date: date, - start_time: start_time, - end_time: end_time, - }; - + let s = models::NewScreening { room_id, film_id, date, start_time, end_time }; diesel::insert_into(screenings::table) .values(&s) .execute(&**self) } + pub fn create_screening_from_aggregate( + &self, + room: &models::Room, + film: &models::Film, + date: chrono::NaiveDate, + start_time: chrono::NaiveTime, + end_time: chrono::NaiveTime) -> QueryResult + { + self.create_screening(room.id, film.id, date, start_time, end_time) + } + pub fn get_screenings(&self) -> QueryResult> { use crate::schema::screenings::dsl::*; screenings.load(&**self) diff --git a/src/lib.rs b/src/lib.rs index 76e4df8..4972205 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,5 +46,10 @@ pub fn build_rocket(db_url: &str) -> Result> { .mount("/", rocket_contrib::serve::StaticFiles::from("./public")) .mount("/rooms", routes![room::get_rooms_json, room::list_rooms, room::new_room, room::create_room]) .mount("/films", routes![film::get_films_json, film::list_films, film::new_film, film::create_film]) - .mount("/screenings", routes![screening::get_aggregated_screenings])) + .mount("/screenings", routes![ + screening::get_aggregated_screenings, + screening::list_screenings, + screening::new_screening, + screening::create_screening, + ])) } diff --git a/templates/screening/list.tera b/templates/screening/list.tera new file mode 100644 index 0000000..7c92303 --- /dev/null +++ b/templates/screening/list.tera @@ -0,0 +1,22 @@ + + + +Screenings + + +

Screenings

+ +

+Add screening

+ +{% if screenings | length <= 0%} +No screenings defined. +{% else %} +
    +{% for s in screenings -%} +
  • {{ s.date }} {{ s.start_time }} - {{ s.end_time }}: {{ s.film.title }} ({{ s.room.name }})
  • +{% endfor -%} +
+{% endif %} + + diff --git a/templates/screening/new.tera b/templates/screening/new.tera new file mode 100644 index 0000000..0c3eb73 --- /dev/null +++ b/templates/screening/new.tera @@ -0,0 +1,36 @@ + + + +New screening + + +

New screening

+ +
+ + + + + + + + + + + + + + +
+ + -- cgit v1.2.3