diff options
Diffstat (limited to 'src/controllers')
-rw-r--r-- | src/controllers/event.rs | 28 | ||||
-rw-r--r-- | src/controllers/film.rs | 26 | ||||
-rw-r--r-- | src/controllers/room.rs | 26 | ||||
-rw-r--r-- | src/controllers/screening.rs | 51 |
4 files changed, 69 insertions, 62 deletions
diff --git a/src/controllers/event.rs b/src/controllers/event.rs index 4de8740..c585933 100644 --- a/src/controllers/event.rs +++ b/src/controllers/event.rs @@ -23,21 +23,21 @@ use crate::{ use serde::Serialize; use serde_json::json; -use std::error::Error; use std::result::Result; use rocket::{get, post}; +use rocket::http::Status; use rocket::response::Redirect; -use rocket::request::{Form, FromForm}; -use rocket_contrib::templates::Template; +use rocket::form::{Form, FromForm}; +use rocket_dyn_templates::Template; #[get("/")] -pub fn index(db: db::Connection) -> Result<Template, Redirect> { +pub async fn index(db: db::Connection) -> Result<Template, Redirect> { #[derive(Serialize)] struct Context { event: models::Event, } - let db_res = models::Event::get(&db); + let db_res = models::Event::get(&db).await; match db_res { Ok(event) => { let ctx = Context { event }; @@ -61,8 +61,20 @@ pub struct NewEventForm { pub description: String, } -#[post("/", format = "application/x-www-form-urlencoded", data = "<form>")] -pub fn create(db: db::Connection, form: Form<NewEventForm>) -> Result<Redirect, Box<dyn Error>> { - models::Event::create(&db, &form.name, &form.description)?; +// fn full_uri(path: &str) -> String { +// let config = rocket::rocket.config(); +// if Some(base_uri) = config.extras.get("base_uri") { +// String::from(base_uri.as_str().unwrap()) + path +// } else { +// String::from(path) +// } +// } + +#[post("/create", format = "application/x-www-form-urlencoded", data = "<form>")] +pub async fn create(db: db::Connection, form: Form<NewEventForm>) -> Result<Redirect, Status> { + models::Event::create(&db, form.name.to_owned(), form.description.to_owned()) + .await + .map_err(|_| Status::InternalServerError)?; + Ok(Redirect::to("")) } diff --git a/src/controllers/film.rs b/src/controllers/film.rs index f407a4b..4a3dfa1 100644 --- a/src/controllers/film.rs +++ b/src/controllers/film.rs @@ -22,37 +22,35 @@ use crate::{ }; use rocket::{get, 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::collections::HashMap; -use std::error::Error; use std::result::Result; #[get("/", format = "application/json", rank = 1)] -pub fn get_films_json(db: db::Connection) -> Json<Vec<models::Film>> { - Json(db.get_films().unwrap()) +pub async fn get_films_json(db: db::Connection) -> Json<Vec<models::Film>> { + Json(db.get_films().await.unwrap()) } #[get("/", rank = 2)] -pub fn list_films(db: db::Connection) -> Result<Template, Box<dyn Error>> { +pub async fn list_films(db: db::Connection) -> Result<Template, Status> { #[derive(Serialize)] struct Context { films: Vec<models::Film>, } - let ctx = Context { films: db.get_films()? }; + let ctx = Context { films: db.get_films().await.map_err(|_| Status::InternalServerError)? }; Ok(Template::render("film/list", &ctx)) } #[get("/new")] -pub fn new_film() -> Result<Template, Box<dyn Error>> { +pub fn new_film() -> Template { let ctx: HashMap<String, String> = HashMap::new(); - Ok(Template::render("film/new", &ctx)) + Template::render("film/new", &ctx) } #[derive(FromForm)] @@ -62,7 +60,7 @@ pub struct NewFilmForm { } #[post("/", format = "application/x-www-form-urlencoded", data = "<film>")] -pub fn create_film(db: db::Connection, film: Form<NewFilmForm>) -> Result<Redirect, Box<dyn Error>> { - db.create_film(&film.title, &film.url)?; +pub async fn create_film(db: db::Connection, film: Form<NewFilmForm>) -> Result<Redirect, Status> { + db.create_film(film.title.to_owned(), film.url.to_owned()).await.map_err(|_| Status::InternalServerError)?; Ok(Redirect::to("films")) } diff --git a/src/controllers/room.rs b/src/controllers/room.rs index 9a4f4c2..08ce56d 100644 --- a/src/controllers/room.rs +++ b/src/controllers/room.rs @@ -23,15 +23,13 @@ use crate::{ use std::result::Result; use rocket::{get, 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::collections::HashMap; -use std::error::Error; #[derive(Serialize)] struct Context { @@ -39,20 +37,20 @@ struct Context { } #[get("/", format = "application/json", rank = 1)] -pub fn get_rooms_json(db: db::Connection) -> Json<Vec<models::Room>> { - Json(db.get_rooms().unwrap()) +pub async fn get_rooms_json(db: db::Connection) -> Json<Vec<models::Room>> { + Json(db.get_rooms().await.unwrap()) } #[get("/", rank = 2)] -pub fn list_rooms(db: db::Connection) -> Result<Template, Box<dyn Error>> { - let ctx = Context { rooms: db.get_rooms()? }; +pub async fn list_rooms(db: db::Connection) -> Result<Template, Status> { + let ctx = Context { rooms: db.get_rooms().await.map_err(|_| Status::InternalServerError)? }; Ok(Template::render("room/list", &ctx)) } #[get("/new")] -pub fn new_room() -> Result<Template, Box<dyn Error>> { +pub fn new_room() -> Template { let ctx: HashMap<String, String> = HashMap::new(); - Ok(Template::render("room/new", &ctx)) + Template::render("room/new", &ctx) } #[derive(FromForm)] @@ -61,7 +59,7 @@ pub struct NewRoomForm { } #[post("/", format = "application/x-www-form-urlencoded", data = "<room>")] -pub fn create_room(db: db::Connection, room: Form<NewRoomForm>) -> Result<Redirect, Box<dyn Error>> { - db.create_room(&room.name)?; +pub async fn create_room(db: db::Connection, room: Form<NewRoomForm>) -> Result<Redirect, Status> { + db.create_room(room.name.to_owned()).await.map_err(|_| Status::InternalServerError)?; Ok(Redirect::to("rooms")) } 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")) } |