From 639eb16af544ea051d2e091111bcd6427a1644d0 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Tue, 25 Aug 2020 16:12:16 +0200 Subject: Add info about the event to the db and render landing page. --- Cargo.toml | 1 + src/controllers.rs | 1 + src/controllers/event.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 8 ++++-- src/models.rs | 2 ++ src/models/event.rs | 45 ++++++++++++++++++++++++++++++ src/schema.rs | 9 ++++++ templates/event/index.tera | 14 ++++++++++ templates/event/new.tera | 16 +++++++++++ 9 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 src/controllers/event.rs create mode 100644 src/models/event.rs create mode 100644 templates/event/index.tera create mode 100644 templates/event/new.tera diff --git a/Cargo.toml b/Cargo.toml index 7e25b9e..0e75d1f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 + + 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 . +*/ + +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 { + #[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 = "
")] +pub fn create(db: db::Connection, form: Form) -> Result> { + models::Event::create(&db, &form.name, &form.description)?; + Ok(Redirect::to("/")) +} diff --git a/src/lib.rs b/src/lib.rs index 330c462..d01d817 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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> { 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 + + 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 . +*/ + +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, +} + +impl Event { + pub fn get(db: &db::Connection) -> QueryResult { + use crate::schema::event::dsl::*; + event.first(&**db) + } + + pub fn create(db: &db::Connection, eventname: &str, desc: &str) -> QueryResult { + 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,3 +1,11 @@ +table! { + event (id) { + id -> Int4, + name -> Varchar, + description -> Nullable, + } +} + table! { films (id) { id -> Int4, @@ -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 @@ + + + + {{ event.name }} + + +

{{ event.name }}

+ + {% if event.description -%} +

{{ event.description }}

+ {% endif -%} + + + 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 @@ + + + + Create the event + + +

Create event

+ + + + + + + + + -- cgit v1.2.3