diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2021-08-25 19:45:34 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2021-08-25 19:45:34 +0200 |
commit | 6f27c2f3ac7fb75e1f4b071f84f879a447712725 (patch) | |
tree | 4d5066922d3e45877bcb37b7aaeab2242db697a7 | |
parent | 0ba9752b0211c9add44ffb652e99444f4b4e36f3 (diff) | |
download | ramaskrik-social-6f27c2f3ac7fb75e1f4b071f84f879a447712725.tar.gz ramaskrik-social-6f27c2f3ac7fb75e1f4b071f84f879a447712725.tar.bz2 ramaskrik-social-6f27c2f3ac7fb75e1f4b071f84f879a447712725.zip |
Refactor event templates and add edit form.
-rw-r--r-- | src/controllers/event.rs | 26 | ||||
-rw-r--r-- | src/main.rs | 13 | ||||
-rw-r--r-- | src/models/event.rs | 18 | ||||
-rw-r--r-- | templates/base.html.tera | 29 | ||||
-rw-r--r-- | templates/event/edit.html.tera | 12 | ||||
-rw-r--r-- | templates/event/index.html.tera | 45 | ||||
-rw-r--r-- | templates/event/new.html.tera | 14 |
7 files changed, 103 insertions, 54 deletions
diff --git a/src/controllers/event.rs b/src/controllers/event.rs index c585933..eca8c21 100644 --- a/src/controllers/event.rs +++ b/src/controllers/event.rs @@ -34,13 +34,13 @@ use rocket_dyn_templates::Template; pub async fn index(db: db::Connection) -> Result<Template, Redirect> { #[derive(Serialize)] struct Context { - event: models::Event, + events: Vec<models::Event>, } - let db_res = models::Event::get(&db).await; + let db_res = models::Event::get_all_events(&db).await; match db_res { - Ok(event) => { - let ctx = Context { event }; + Ok(events) => { + let ctx = Context { events }; Ok(Template::render("event/index", &ctx)) }, Err(_) => { @@ -72,9 +72,23 @@ pub struct NewEventForm { #[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()) + let event = models::Event::create(&db, form.name.to_owned(), form.description.to_owned()) + .await + .map_err(|_| Status::InternalServerError)?; + + Ok(Redirect::to(format!("/{}/edit", event.id))) +} + +#[get("/<eventid>/edit")] +pub async fn edit(db: db::Connection, eventid: i32) -> Result<Template, Status> { + #[derive(Serialize)] + struct Context { + event: models::Event, + } + + let event = models::Event::get_event(&db, eventid) .await .map_err(|_| Status::InternalServerError)?; - Ok(Redirect::to("")) + Ok(Template::render("event/edit", Context { event })) } diff --git a/src/main.rs b/src/main.rs index 2b4d01b..978c702 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,10 +35,19 @@ fn rocket() -> _ { event::index, event::new, event::create, + event::edit, ]) .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("/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, diff --git a/src/models/event.rs b/src/models/event.rs index 646673d..baa44fc 100644 --- a/src/models/event.rs +++ b/src/models/event.rs @@ -37,13 +37,27 @@ impl Event { }).await } - pub async fn create(db: &db::Connection, eventname: String, desc: String) -> QueryResult<usize> { + pub async fn get_event(db: &db::Connection, eventid: i32) -> QueryResult<Event> { + db.run(move |conn| { + use crate::schema::event::dsl::*; + event.filter(id.eq(eventid)).first(conn) + }).await + } + + pub async fn get_all_events(db: &db::Connection) -> QueryResult<Vec<Event>> { + db.run(|conn| { + use crate::schema::event::dsl::*; + event.load(conn) + }).await + } + + pub async fn create(db: &db::Connection, eventname: String, desc: String) -> QueryResult<Event> { 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) + .get_result(conn) }).await } } diff --git a/templates/base.html.tera b/templates/base.html.tera new file mode 100644 index 0000000..cca4d1e --- /dev/null +++ b/templates/base.html.tera @@ -0,0 +1,29 @@ +<!DOCTYPE html> +<html> + <head> + <meta charset="utf-8"> + <title>{% block title %}{% endblock title %}Festival Schedule</title> + <link rel="stylesheet" href="/app.css"> + </head> + <body> + {% block contents %}{% endblock contents %} + <footer> + <a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/"> + <img alt="Creative Commons-lisens" style="border-width:0" src="license.png"> + </a> + <span id="copyright-text"> + <span xmlns:dct="http://purl.org/dc/terms/" property="dct:title"> + Festival Schedule + </span> + av + <a xmlns:cc="http://creativecommons.org/ns#" href="https://code.volse.no/ramaskrik-social.git/" + property="cc:attributionName" rel="cc:attributionURL">Harald Eilertsen</a> + er lisensiert under en + <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/"> + Creative Commons Navngivelse-DelPåSammeVilkår 4.0 Internasjonal Lisens + </a>. + </span> + </footer> + </body> +</html> + diff --git a/templates/event/edit.html.tera b/templates/event/edit.html.tera new file mode 100644 index 0000000..6e9fe3d --- /dev/null +++ b/templates/event/edit.html.tera @@ -0,0 +1,12 @@ +{% extends "base" %} +{% block title %}Edit {{ event.name }} — {% endblock title %} +{% block contents %} + <h1>Edit {{ event.name }}</h1> + + <form name="new_event_form" action="update" method="POST"> + <label for="name">Name: </label><input name="name" type="text" value="{{ event.name }}"> + <label for="description">Description: </label> + <textarea name="description">{{ event.description }}</textarea> + <input type="submit" value="Save"> + </form> +{% endblock contents %} diff --git a/templates/event/index.html.tera b/templates/event/index.html.tera index 504e2b4..0a9f7d6 100644 --- a/templates/event/index.html.tera +++ b/templates/event/index.html.tera @@ -1,37 +1,12 @@ -<!DOCTYPE html> -<html> - <head> - <meta charset="utf-8"> - <title>{{ event.name }}</title> - <link rel="stylesheet" href="app.css"> - <script type="text/javascript" src="js/app.js"></script> - </head> - <body> - <h1>{{ event.name }}</h1> +{% extends "base" %} +{% block content %} + {% for event in events %} + <h1>{{ event.name }}</h1> - {% if event.description -%} - <p>{{ event.description }}</p> - {% endif -%} + {% if event.description -%} + <p>{{ event.description }}</p> + {% endif -%} + {% endfor %} - <div id="program"></div> - - <footer> - <a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/"> - <img alt="Creative Commons-lisens" style="border-width:0" src="license.png"> - </a> - <span id="copyright-text"> - <span xmlns:dct="http://purl.org/dc/terms/" property="dct:title"> - {{ event.name }} - </span> - av - <a xmlns:cc="http://creativecommons.org/ns#" href="https://code.volse.no/ramaskrik-social.git/" - property="cc:attributionName" rel="cc:attributionURL">Harald Eilertsen</a> - er lisensiert under en - <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/"> - Creative Commons Navngivelse-DelPåSammeVilkår 4.0 Internasjonal Lisens - </a>. - </span> - </footer> - </body> -</html> -</html> + <div id="program"></div> +{% endblock content %} diff --git a/templates/event/new.html.tera b/templates/event/new.html.tera index 15e5334..9a2c2da 100644 --- a/templates/event/new.html.tera +++ b/templates/event/new.html.tera @@ -1,10 +1,7 @@ -<!DOCTYPE html> -<html> - <head> - <title>Create the event</title> - </head> - <body> - <h1>Create event</h1> +{% extends "base" %} +{% block title %}Create new event — {% endblock title %} +{% block contents %} + <h1>Create new event</h1> <form name="new_event_form" action="create" method="POST"> <label for="name">Name: </label><input name="name" type="text"> @@ -12,5 +9,4 @@ <textarea name="description"></textarea> <input type="submit" value="Save"> </form> - </body> -</html> +{% endblock contents %} |