diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/controllers/room.rs | 22 | ||||
-rw-r--r-- | src/db.rs | 7 | ||||
-rw-r--r-- | src/lib.rs | 2 | ||||
-rw-r--r-- | src/models/room.rs | 2 |
4 files changed, 30 insertions, 3 deletions
diff --git a/src/controllers/room.rs b/src/controllers/room.rs index 8b85344..6458f86 100644 --- a/src/controllers/room.rs +++ b/src/controllers/room.rs @@ -22,12 +22,15 @@ use crate::{ }; use std::result::Result; -use rocket::get; +use rocket::{get, post}; +use rocket::request::{Form, FromForm}; +use rocket::response::Redirect; use rocket_contrib::{ json::Json, templates::Template, }; use serde::Serialize; +use std::collections::HashMap; use std::error::Error; #[derive(Serialize)] @@ -45,3 +48,20 @@ pub fn list_rooms(db: db::Connection) -> Result<Template, Box<dyn Error>> { let ctx = Context { rooms: db.get_rooms()? }; Ok(Template::render("room/list", &ctx)) } + +#[get("/new")] +pub fn new_room() -> Result<Template, Box<dyn Error>> { + let ctx: HashMap<String, String> = HashMap::new(); + Ok(Template::render("room/new", &ctx)) +} + +#[derive(FromForm)] +pub struct NewRoomForm { + name: String, +} + +#[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)?; + Ok(Redirect::to("/rooms")) +} @@ -67,6 +67,13 @@ impl Connection { Ok(models::AggregatedScreening::from_query(s)) } + 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 fn get_rooms(&self) -> QueryResult<Vec<models::Room>> { use crate::schema::rooms::dsl::*; rooms.load(&**self) @@ -44,7 +44,7 @@ pub fn build_rocket(db_url: &str) -> Result<rocket::Rocket, Box<dyn Error>> { .attach(db::Connection::fairing()) .attach(rocket_contrib::templates::Template::fairing()) .mount("/", rocket_contrib::serve::StaticFiles::from("./public")) - .mount("/rooms", routes![room::get_rooms_json, room::list_rooms]) + .mount("/rooms", routes![room::get_rooms_json, room::list_rooms, room::new_room, room::create_room]) .mount("/films", routes![film::get_films]) .mount("/screenings", routes![screening::get_aggregated_screenings])) } diff --git a/src/models/room.rs b/src/models/room.rs index 33fb931..1ec6e56 100644 --- a/src/models/room.rs +++ b/src/models/room.rs @@ -19,7 +19,7 @@ use crate::schema::rooms; use serde::{Deserialize, Serialize}; -#[derive(Clone, Default, Deserialize, Identifiable, PartialEq, Serialize, Queryable)] +#[derive(Clone, Default, Deserialize, Identifiable, Insertable, PartialEq, Serialize, Queryable)] pub struct Room { pub id: i32, pub name: String, |