From 87734ea6b0311903d42ed9ae55965bdf4ea13cb4 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 23 Aug 2020 21:59:25 +0200 Subject: Implement edit screenings in web ui. --- src/controllers/screening.rs | 49 ++++++++++++++++++++++++++++++++++++++++++- src/db.rs | 20 ++++++++++++++++++ src/lib.rs | 2 ++ src/models/screening.rs | 3 ++- templates/screening/edit.tera | 39 ++++++++++++++++++++++++++++++++++ templates/screening/list.tera | 1 + 6 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 templates/screening/edit.tera diff --git a/src/controllers/screening.rs b/src/controllers/screening.rs index 40eefd3..cf7d1b1 100644 --- a/src/controllers/screening.rs +++ b/src/controllers/screening.rs @@ -22,7 +22,7 @@ use crate::{ }; use std::result::Result; -use rocket::{delete, get, post}; +use rocket::{delete, get, patch, post}; use rocket::request::{Form, FromForm}; use rocket::response::Redirect; use rocket_contrib::{ @@ -96,6 +96,53 @@ pub fn create_screening(db: db::Connection, screening: Form) - Ok(Redirect::to("/screenings")) } +#[get("/")] +pub fn edit(db: db::Connection, id: i32) -> Result> { + #[derive(Serialize)] + struct Context { + screening: models::Screening, + rooms: Vec, + films: Vec, + } + + let ctx = Context { + screening: db.get_screening(id)?, + rooms: db.get_rooms()?, + films: db.get_films()?, + }; + + Ok(Template::render("screening/edit", &ctx)) +} + +#[derive(FromForm)] +pub struct EditScreeningForm { + id: i32, + film_id: i32, + room_id: i32, + date: String, + start_time: String, + end_time: String, +} + +#[patch("/", format = "application/x-www-form-urlencoded", data = "")] +pub fn update(db: db::Connection, screening: Form) -> Result> { + let start_time = parse_datetime(&screening.date, &screening.start_time)?; + let mut end_time = parse_datetime(&screening.date, &screening.end_time)?; + + if end_time < start_time { + end_time = end_time + chrono::Duration::days(1); + } + + db.update_screening( + screening.id, + screening.room_id, + screening.film_id, + start_time, + end_time)?; + + Ok(Redirect::to("/screenings")) +} + #[derive(FromForm)] pub struct DeleteScreeningForm { screening_id: i32, diff --git a/src/db.rs b/src/db.rs index 4c562ef..6bce5e8 100644 --- a/src/db.rs +++ b/src/db.rs @@ -55,11 +55,31 @@ impl Connection { self.create_screening(room.id, film.id, start_time, end_time) } + pub fn update_screening( + &self, + screening_id: i32, + room_id: i32, + film_id: i32, + start_time: chrono::DateTime, + end_time: chrono::DateTime) -> QueryResult + { + let s = models::Screening { id: screening_id, film_id, room_id, start_time, end_time }; + diesel::update(screenings::table) + .filter(screenings::id.eq(screening_id)) + .set(&s) + .execute(&**self) + } + pub fn get_screenings(&self) -> QueryResult> { use crate::schema::screenings::dsl::*; screenings.load(&**self) } + pub fn get_screening(&self, screening_id: i32) -> QueryResult { + use crate::schema::screenings::dsl::*; + screenings.filter(id.eq(screening_id)).get_result(&**self) + } + pub fn get_aggregated_screenings(&self) -> QueryResult> { use crate::schema::screenings::dsl::*; let s = screenings diff --git a/src/lib.rs b/src/lib.rs index 0f9d7b4..57d8b7a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -51,6 +51,8 @@ pub fn build_rocket(db_url: &str) -> Result> { screening::list_screenings, screening::new_screening, screening::create_screening, + screening::edit, + screening::update, screening::delete, ])) } diff --git a/src/models/screening.rs b/src/models/screening.rs index 1426605..8ab96e0 100644 --- a/src/models/screening.rs +++ b/src/models/screening.rs @@ -18,6 +18,7 @@ use crate::models::{Film, Room}; use crate::schema::*; +use diesel::query_builder::AsChangeset; use serde::{Deserialize, Serialize}; joinable!(screenings -> rooms (room_id)); @@ -32,7 +33,7 @@ pub struct NewScreening { pub end_time: chrono::DateTime, } -#[derive(Deserialize, Identifiable, PartialEq, Serialize, Queryable)] +#[derive(AsChangeset, Deserialize, Identifiable, PartialEq, Serialize, Queryable)] #[table_name = "screenings"] pub struct Screening { pub id: i32, diff --git a/templates/screening/edit.tera b/templates/screening/edit.tera new file mode 100644 index 0000000..39268fe --- /dev/null +++ b/templates/screening/edit.tera @@ -0,0 +1,39 @@ + + + +Edit screening + + +

Edit screening

+ +
+ + + + + + + + + + + + + + + + + +
+ + diff --git a/templates/screening/list.tera b/templates/screening/list.tera index 21ef7e2..7112796 100644 --- a/templates/screening/list.tera +++ b/templates/screening/list.tera @@ -25,6 +25,7 @@ No screenings defined. {{ s.film.title }} ({{ s.room.name }}) + Edit
-- cgit v1.2.3