aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/controllers/event.rs28
-rw-r--r--src/controllers/film.rs26
-rw-r--r--src/controllers/room.rs26
-rw-r--r--src/controllers/screening.rs51
-rw-r--r--src/db.rs137
-rw-r--r--src/lib.rs31
-rw-r--r--src/main.rs35
-rw-r--r--src/models/event.rs22
8 files changed, 194 insertions, 162 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"))
}
diff --git a/src/db.rs b/src/db.rs
index 6bce5e8..a5341f1 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -23,16 +23,16 @@ use crate::{
use diesel::prelude::*;
-use rocket_contrib::{
+use rocket_sync_db_pools::{
database,
- databases::diesel,
+ diesel,
};
#[database("main")]
pub struct Connection(diesel::PgConnection);
impl Connection {
- pub fn create_screening(
+ pub async fn create_screening(
&self,
room_id: i32,
film_id: i32,
@@ -40,22 +40,24 @@ impl Connection {
end_time: chrono::DateTime<chrono::Utc>) -> QueryResult<usize>
{
let s = models::NewScreening { room_id, film_id, start_time, end_time };
- diesel::insert_into(screenings::table)
- .values(&s)
- .execute(&**self)
+ self.run(move |conn| {
+ diesel::insert_into(screenings::table)
+ .values(&s)
+ .execute(conn)
+ }).await
}
- pub fn create_screening_from_aggregate(
+ pub async fn create_screening_from_aggregate(
&self,
room: &models::Room,
film: &models::Film,
start_time: chrono::DateTime<chrono::Utc>,
end_time: chrono::DateTime<chrono::Utc>) -> QueryResult<usize>
{
- self.create_screening(room.id, film.id, start_time, end_time)
+ self.create_screening(room.id, film.id, start_time, end_time).await
}
- pub fn update_screening(
+ pub async fn update_screening(
&self,
screening_id: i32,
room_id: i32,
@@ -64,72 +66,95 @@ impl Connection {
end_time: chrono::DateTime<chrono::Utc>) -> QueryResult<usize>
{
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)
+ self.run(move |conn| {
+ diesel::update(screenings::table)
+ .filter(screenings::id.eq(screening_id))
+ .set(&s)
+ .execute(conn)
+ }).await
}
- pub fn get_screenings(&self) -> QueryResult<Vec<models::Screening>> {
- use crate::schema::screenings::dsl::*;
- screenings.load(&**self)
+ pub async fn get_screenings(&self) -> QueryResult<Vec<models::Screening>> {
+ self.run(|conn| {
+ use crate::schema::screenings::dsl::*;
+ screenings.load(conn)
+ }).await
}
- pub fn get_screening(&self, screening_id: i32) -> QueryResult<models::Screening> {
- use crate::schema::screenings::dsl::*;
- screenings.filter(id.eq(screening_id)).get_result(&**self)
+ pub async fn get_screening(&self, screening_id: i32) -> QueryResult<models::Screening> {
+ self.run(move |conn| {
+ use crate::schema::screenings::dsl::*;
+ screenings.filter(id.eq(screening_id)).get_result(conn)
+ }).await
}
- pub fn get_aggregated_screenings(&self) -> QueryResult<Vec<models::AggregatedScreening>> {
- use crate::schema::screenings::dsl::*;
- let s = screenings
- .left_join(films::table)
- .left_join(rooms::table)
- .get_results(&**self)?;
- Ok(models::AggregatedScreening::from_query(s))
+ pub async fn get_aggregated_screenings(&self) -> QueryResult<Vec<models::AggregatedScreening>> {
+ let s = self.run(|conn| {
+ use crate::schema::screenings::dsl::*;
+ screenings
+ .left_join(films::table)
+ .left_join(rooms::table)
+ .get_results(conn)
+ }).await;
+
+ Ok(models::AggregatedScreening::from_query(s?))
}
- pub fn delete_screening(&self, screening_id: i32) -> QueryResult<usize> {
- use crate::schema::screenings::dsl::*;
- diesel::delete(screenings.filter(id.eq(screening_id)))
- .execute(&**self)
+ pub async fn delete_screening(&self, screening_id: i32) -> QueryResult<usize> {
+ self.run(move |conn| {
+ use crate::schema::screenings::dsl::*;
+ diesel::delete(screenings.filter(id.eq(screening_id)))
+ .execute(conn)
+ }).await
}
- pub fn create_room(&self, room_name: &String) -> QueryResult<usize> {
- use crate::schema::rooms::dsl::*;
- diesel::insert_into(rooms)
- .values(&name.eq(room_name))
- .execute(&**self)
+ pub async fn create_room(&self, room_name: String) -> QueryResult<usize> {
+ self.run(move |conn| {
+ use crate::schema::rooms::dsl::*;
+ diesel::insert_into(rooms)
+ .values(&name.eq(room_name))
+ .execute(conn)
+ }).await
}
- pub fn get_rooms(&self) -> QueryResult<Vec<models::Room>> {
- use crate::schema::rooms::dsl::*;
- rooms.load(&**self)
+ pub async fn get_rooms(&self) -> QueryResult<Vec<models::Room>> {
+ self.run(|conn| {
+ use crate::schema::rooms::dsl::*;
+ rooms.load(conn)
+ }).await
}
- pub fn get_room_by_name(&self, room_name: &str) -> QueryResult<models::Room> {
- use crate::schema::rooms::dsl::*;
- rooms
- .filter(name.eq(&room_name))
- .get_result(&**self)
+ pub async fn get_room_by_name(&self, room_name: String) -> QueryResult<models::Room> {
+ self.run(move |conn| {
+ use crate::schema::rooms::dsl::*;
+ rooms
+ .filter(name.eq(&room_name))
+ .get_result(conn)
+ }).await
}
- pub fn create_film(&self, film_title: &str, film_url: &Option<String>) -> QueryResult<usize> {
- use crate::schema::films::dsl::*;
- diesel::insert_into(films)
- .values(&(title.eq(film_title), url.eq(film_url)))
- .execute(&**self)
+ pub async fn create_film(&self, film_title: String, film_url: Option<String>) -> QueryResult<usize> {
+ self.run(move |conn| {
+ use crate::schema::films::dsl::*;
+ diesel::insert_into(films)
+ .values(&(title.eq(film_title), url.eq(film_url)))
+ .execute(conn)
+ }).await
}
- pub fn get_films(&self) -> QueryResult<Vec<models::Film>> {
- use crate::schema::films::dsl::*;
- films.load(&**self)
+ pub async fn get_films(&self) -> QueryResult<Vec<models::Film>> {
+ self.run(|conn| {
+ use crate::schema::films::dsl::*;
+ films.load(conn)
+ }).await
}
- pub fn get_film_by_title(&self, film_title: &str) -> QueryResult<models::Film> {
- use crate::schema::films::dsl::*;
- films
- .filter(title.eq(&film_title))
- .get_result(&**self)
+ pub async fn get_film_by_title(&self, film_title: String) -> QueryResult<models::Film> {
+ self.run(move |conn| {
+ use crate::schema::films::dsl::*;
+ films
+ .filter(title.eq(&film_title))
+ .get_result(conn)
+ }).await
}
}
diff --git a/src/lib.rs b/src/lib.rs
index 6a9e488..dae5380 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -15,39 +15,10 @@
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate diesel;
-mod controllers;
+pub mod controllers;
pub mod db;
pub mod models;
pub mod schema;
-use crate::controllers::{event, film, room, screening};
-
-use rocket::routes;
-use std::error::Error;
-use std::result::Result;
-
-pub fn build_rocket() -> Result<rocket::Rocket, Box<dyn Error>> {
- Ok(rocket::ignite()
- .attach(db::Connection::fairing())
- .attach(rocket_contrib::templates::Template::fairing())
- .mount("/", routes![
- event::index,
- event::new,
- event::create,
- ])
- .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,
- screening::list_screenings,
- screening::new_screening,
- screening::create_screening,
- screening::edit,
- screening::update,
- screening::delete,
- ]))
-}
diff --git a/src/main.rs b/src/main.rs
index fdc8ff3..2b4d01b 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -16,11 +16,36 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
+#[macro_use] extern crate rocket;
+
use ramaskrik;
-use std::error::Error;
-use std::result::Result;
+use ramaskrik::controllers::{event, film, room, screening};
+use ramaskrik::db;
+
+use rocket::routes;
+use rocket::fs::FileServer;
+use rocket_dyn_templates::Template;
-fn main() -> Result<(), Box<dyn Error>> {
- ramaskrik::build_rocket()?.launch();
- Ok(())
+#[launch]
+fn rocket() -> _ {
+ rocket::build()
+ .attach(db::Connection::fairing())
+ .attach(Template::fairing())
+ .mount("/", routes![
+ event::index,
+ event::new,
+ event::create,
+ ])
+ .mount("/", FileServer::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,
+ screening::list_screenings,
+ screening::new_screening,
+ screening::create_screening,
+ screening::edit,
+ screening::update,
+ screening::delete,
+ ])
}
diff --git a/src/models/event.rs b/src/models/event.rs
index 0b31cbe..646673d 100644
--- a/src/models/event.rs
+++ b/src/models/event.rs
@@ -30,16 +30,20 @@ pub struct Event {
}
impl Event {
- pub fn get(db: &db::Connection) -> QueryResult<Event> {
- use crate::schema::event::dsl::*;
- event.first(&**db)
+ pub async fn get(db: &db::Connection) -> QueryResult<Event> {
+ db.run(|conn| {
+ use crate::schema::event::dsl::*;
+ event.first(conn)
+ }).await
}
- pub fn create(db: &db::Connection, eventname: &str, desc: &str) -> QueryResult<usize> {
- use crate::schema::event::dsl::*;
- let new_event = (name.eq(&eventname), description.eq(&desc));
- diesel::insert_into(event)
- .values(&new_event)
- .execute(&**db)
+ pub async fn create(db: &db::Connection, eventname: String, desc: String) -> QueryResult<usize> {
+ db.run(move |conn| {
+ use crate::schema::event::dsl::*;
+ let new_event = (name.eq(&eventname), description.eq(&desc));
+ diesel::insert_into(event)
+ .values(&new_event)
+ .execute(conn)
+ }).await
}
}