diff options
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/controllers.rs | 1 | ||||
-rw-r--r-- | src/controllers/event.rs | 68 | ||||
-rw-r--r-- | src/lib.rs | 8 | ||||
-rw-r--r-- | src/models.rs | 2 | ||||
-rw-r--r-- | src/models/event.rs | 45 | ||||
-rw-r--r-- | src/schema.rs | 9 | ||||
-rw-r--r-- | templates/event/index.tera | 14 | ||||
-rw-r--r-- | templates/event/new.tera | 16 |
9 files changed, 162 insertions, 2 deletions
@@ -10,6 +10,7 @@ diesel = { version = "1.4.1", features = ["chrono", "postgres"] } dotenv = "0.13.0" rocket = "0.4.5" serde = { version = "1.0.115", features = ["derive"] } +serde_json = "1.0" [dependencies.rocket_contrib] version = "0.4.5" diff --git a/src/controllers.rs b/src/controllers.rs index 40d910c..9b89f90 100644 --- a/src/controllers.rs +++ b/src/controllers.rs @@ -19,3 +19,4 @@ pub mod film; pub mod room; pub mod screening; +pub mod event; diff --git a/src/controllers/event.rs b/src/controllers/event.rs new file mode 100644 index 0000000..1b754f2 --- /dev/null +++ b/src/controllers/event.rs @@ -0,0 +1,68 @@ +/* + Social program for Ramaskrik. + Copyright (C) 2019, 2020 Harald Eilertsen <haraldei@anduin.net> + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + 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/>. +*/ + +use crate::{ + db, + models, +}; + +use serde::Serialize; +use serde_json::json; +use std::error::Error; +use std::result::Result; +use rocket::{get, post}; +use rocket::response::Redirect; +use rocket::request::{Form, FromForm}; +use rocket_contrib::templates::Template; + +#[get("/")] +pub fn index(db: db::Connection) -> Result<Template, Redirect> { + #[derive(Serialize)] + struct Context { + event: models::Event, + } + + let db_res = models::Event::get(&db); + match db_res { + Ok(event) => { + let ctx = Context { event }; + Ok(Template::render("event/index", &ctx)) + }, + Err(_) => { + // Create new event if it's not already in the db. + Err(Redirect::to("/new")) + } + } +} + +#[get("/new")] +pub fn new() -> Template { + Template::render("event/new", &json!({})) +} + +#[derive(FromForm)] +pub struct NewEventForm { + pub name: String, + 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)?; + Ok(Redirect::to("/")) +} @@ -23,7 +23,7 @@ mod controllers; pub mod db; pub mod models; pub mod schema; -use crate::controllers::{film, room, screening}; +use crate::controllers::{event, film, room, screening}; use rocket::routes; use std::error::Error; @@ -33,7 +33,11 @@ pub fn build_rocket() -> Result<rocket::Rocket, Box<dyn Error>> { Ok(rocket::ignite() .attach(db::Connection::fairing()) .attach(rocket_contrib::templates::Template::fairing()) - .mount("/", rocket_contrib::serve::StaticFiles::from("./public")) + .mount("/", routes![ + event::index, + event::new, + event::create, + ]) .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![ diff --git a/src/models.rs b/src/models.rs index a009f4f..e6a0f03 100644 --- a/src/models.rs +++ b/src/models.rs @@ -19,7 +19,9 @@ pub mod film; pub mod room; pub mod screening; +pub mod event; pub use film::{Film, NewFilm}; pub use room::Room; pub use screening::{AggregatedScreening, NewScreening, Screening}; +pub use event::Event; diff --git a/src/models/event.rs b/src/models/event.rs new file mode 100644 index 0000000..0b31cbe --- /dev/null +++ b/src/models/event.rs @@ -0,0 +1,45 @@ +/* + Social program for Ramaskrik. + Copyright (C) 2019, 2020 Harald Eilertsen <haraldei@anduin.net> + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + 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/>. +*/ + +use crate::db; +use crate::schema::*; +use diesel::prelude::*; +use serde::Serialize; + +#[derive(Identifiable, Queryable, Serialize)] +#[table_name = "event"] +pub struct Event { + pub id: i32, + pub name: String, + pub description: Option<String>, +} + +impl Event { + pub fn get(db: &db::Connection) -> QueryResult<Event> { + use crate::schema::event::dsl::*; + event.first(&**db) + } + + 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) + } +} diff --git a/src/schema.rs b/src/schema.rs index 8931558..fc3066c 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -1,4 +1,12 @@ table! { + event (id) { + id -> Int4, + name -> Varchar, + description -> Nullable<Text>, + } +} + +table! { films (id) { id -> Int4, title -> Varchar, @@ -24,6 +32,7 @@ table! { } allow_tables_to_appear_in_same_query!( + event, films, rooms, screenings, diff --git a/templates/event/index.tera b/templates/event/index.tera new file mode 100644 index 0000000..411a5f5 --- /dev/null +++ b/templates/event/index.tera @@ -0,0 +1,14 @@ +<!DOCTYPE html> +<html> + <head> + <title>{{ event.name }}</title> + </head> + <body> + <h1>{{ event.name }}</h1> + + {% if event.description -%} + <p>{{ event.description }}</p> + {% endif -%} + + </body> +</html> diff --git a/templates/event/new.tera b/templates/event/new.tera new file mode 100644 index 0000000..41a9aca --- /dev/null +++ b/templates/event/new.tera @@ -0,0 +1,16 @@ +<!DOCTYPE html> +<html> + <head> + <title>Create the event</title> + </head> + <body> + <h1>Create event</h1> + + <form name="new_event_form" action="/" method="POST"> + <label for="name">Name: </label><input name="name" type="text"> + <label for="description">Description: </label> + <textarea name="description"></textarea> + <input type="submit" value="Save"> + </form> + </body> +</html> |