diff options
Diffstat (limited to 'src/controllers/screening.rs')
-rw-r--r-- | src/controllers/screening.rs | 51 |
1 files changed, 25 insertions, 26 deletions
diff --git a/src/controllers/screening.rs b/src/controllers/screening.rs index 06b0b84..5df8602 100644 --- a/src/controllers/screening.rs +++ b/src/controllers/screening.rs @@ -23,33 +23,32 @@ use crate::{ use std::result::Result; use rocket::{delete, get, patch, post}; -use rocket::request::{Form, FromForm}; +use rocket::form::{Form, FromForm}; +use rocket::http::Status; use rocket::response::Redirect; -use rocket_contrib::{ - json::Json, - templates::Template, -}; +use rocket::serde::json::Json; +use rocket_dyn_templates::Template; use serde::Serialize; use std::error::Error; #[get("/", format = "application/json")] -pub fn get_aggregated_screenings(db: db::Connection) -> Json<Vec<models::AggregatedScreening>> { - Json(db.get_aggregated_screenings().unwrap()) +pub async fn get_aggregated_screenings(db: db::Connection) -> Json<Vec<models::AggregatedScreening>> { + Json(db.get_aggregated_screenings().await.unwrap()) } #[get("/", rank = 2)] -pub fn list_screenings(db: db::Connection) -> Result<Template, Box<dyn Error>> { +pub async fn list_screenings(db: db::Connection) -> Result<Template, Status> { #[derive(Serialize)] struct Context { screenings: Vec<models::AggregatedScreening>, } - let ctx = Context { screenings: db.get_aggregated_screenings()? }; + let ctx = Context { screenings: db.get_aggregated_screenings().await.map_err(|_| Status::InternalServerError)? }; Ok(Template::render("screening/list", &ctx)) } #[get("/new")] -pub fn new_screening(db: db::Connection) -> Result<Template, Box<dyn Error>> { +pub async fn new_screening(db: db::Connection) -> Result<Template, Status> { #[derive(Serialize)] struct Context { rooms: Vec<models::Room>, @@ -57,8 +56,8 @@ pub fn new_screening(db: db::Connection) -> Result<Template, Box<dyn Error>> { } let ctx = Context { - rooms: db.get_rooms()?, - films: db.get_films()?, + rooms: db.get_rooms().await.map_err(|_| Status::InternalServerError)?, + films: db.get_films().await.map_err(|_| Status::InternalServerError)?, }; Ok(Template::render("screening/new", &ctx)) @@ -79,9 +78,9 @@ fn parse_datetime(date: &str, time: &str) -> Result<chrono::DateTime<chrono::Utc } #[post("/", format = "application/x-www-form-urlencoded", data = "<screening>")] -pub fn create_screening(db: db::Connection, screening: Form<NewScreeningForm>) -> Result<Redirect, Box<dyn Error>> { - let start_time = parse_datetime(&screening.date, &screening.start_time)?; - let mut end_time = parse_datetime(&screening.date, &screening.end_time)?; +pub async fn create_screening(db: db::Connection, screening: Form<NewScreeningForm>) -> Result<Redirect, Status> { + let start_time = parse_datetime(&screening.date, &screening.start_time).map_err(|_| Status::InternalServerError)?; + let mut end_time = parse_datetime(&screening.date, &screening.end_time).map_err(|_| Status::InternalServerError)?; if end_time < start_time { end_time = end_time + chrono::Duration::days(1); @@ -91,13 +90,13 @@ pub fn create_screening(db: db::Connection, screening: Form<NewScreeningForm>) - screening.room_id, screening.film_id, start_time, - end_time)?; + end_time).await.map_err(|_| Status::InternalServerError)?; Ok(Redirect::to("screenings")) } #[get("/<id>")] -pub fn edit(db: db::Connection, id: i32) -> Result<Template, Box<dyn Error>> { +pub async fn edit(db: db::Connection, id: i32) -> Result<Template, Status> { #[derive(Serialize)] struct Context { screening: models::Screening, @@ -106,9 +105,9 @@ pub fn edit(db: db::Connection, id: i32) -> Result<Template, Box<dyn Error>> { } let ctx = Context { - screening: db.get_screening(id)?, - rooms: db.get_rooms()?, - films: db.get_films()?, + screening: db.get_screening(id).await.map_err(|_| Status::InternalServerError)?, + rooms: db.get_rooms().await.map_err(|_| Status::InternalServerError)?, + films: db.get_films().await.map_err(|_| Status::InternalServerError)?, }; Ok(Template::render("screening/edit", &ctx)) @@ -125,9 +124,9 @@ pub struct EditScreeningForm { } #[patch("/", format = "application/x-www-form-urlencoded", data = "<screening>")] -pub fn update(db: db::Connection, screening: Form<EditScreeningForm>) -> Result<Redirect, Box<dyn Error>> { - let start_time = parse_datetime(&screening.date, &screening.start_time)?; - let mut end_time = parse_datetime(&screening.date, &screening.end_time)?; +pub async fn update(db: db::Connection, screening: Form<EditScreeningForm>) -> Result<Redirect, Status> { + let start_time = parse_datetime(&screening.date, &screening.start_time).map_err(|_| Status::InternalServerError)?; + let mut end_time = parse_datetime(&screening.date, &screening.end_time).map_err(|_| Status::InternalServerError)?; if end_time < start_time { end_time = end_time + chrono::Duration::days(1); @@ -138,7 +137,7 @@ pub fn update(db: db::Connection, screening: Form<EditScreeningForm>) -> Result< screening.room_id, screening.film_id, start_time, - end_time)?; + end_time).await.map_err(|_| Status::InternalServerError)?; Ok(Redirect::to("screenings")) } @@ -149,7 +148,7 @@ pub struct DeleteScreeningForm { } #[delete("/", format = "application/x-www-form-urlencoded", data = "<screening>")] -pub fn delete(db: db::Connection, screening: Form<DeleteScreeningForm>) -> Result<Redirect, Box<dyn Error>> { - db.delete_screening(screening.screening_id)?; +pub async fn delete(db: db::Connection, screening: Form<DeleteScreeningForm>) -> Result<Redirect, Status> { + db.delete_screening(screening.screening_id).await.map_err(|_| Status::InternalServerError)?; Ok(Redirect::to("screenings")) } |