aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2020-08-22 20:14:03 +0200
committerHarald Eilertsen <haraldei@anduin.net>2020-08-22 20:14:03 +0200
commit701ff00b72b423b77e3d1e09dc7fc1b817b207cb (patch)
treeb1914d2b292aa3462ffb3dff7856d7436469da28
parent86178bb3a5ad131766a57cdcac9f22fed3c55f8a (diff)
downloadramaskrik-social-701ff00b72b423b77e3d1e09dc7fc1b817b207cb.tar.gz
ramaskrik-social-701ff00b72b423b77e3d1e09dc7fc1b817b207cb.tar.bz2
ramaskrik-social-701ff00b72b423b77e3d1e09dc7fc1b817b207cb.zip
Implement adding rooms through the web ui.
-rw-r--r--src/controllers/room.rs22
-rw-r--r--src/db.rs7
-rw-r--r--src/lib.rs2
-rw-r--r--src/models/room.rs2
-rw-r--r--templates/room/list.tera6
-rw-r--r--templates/room/new.tera14
6 files changed, 50 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"))
+}
diff --git a/src/db.rs b/src/db.rs
index 60bbcde..09b6f4f 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -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)
diff --git a/src/lib.rs b/src/lib.rs
index 3b35594..41eea66 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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,
diff --git a/templates/room/list.tera b/templates/room/list.tera
index fddad57..23b2086 100644
--- a/templates/room/list.tera
+++ b/templates/room/list.tera
@@ -8,6 +8,12 @@
{% if rooms | length <= 0%}
No rooms defined.
+{% else %}
+<ul>
+{% for r in rooms -%}
+ <li>{{ r.name }} ({{ r.id }})
+{% endfor -%}
+</ul>
{% endif %}
</body>
</html>
diff --git a/templates/room/new.tera b/templates/room/new.tera
new file mode 100644
index 0000000..2bbe27c
--- /dev/null
+++ b/templates/room/new.tera
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html>
+<head>
+<title>New room</title>
+</head>
+<body>
+<h1>New room</h1>
+
+<form name="new_room_form" action="/rooms" method="POST">
+ <label for="name">Name: </label><input name="name" type="text">
+ <input type="submit" value="Save">
+</form>
+</body>
+</html>