From 6f27c2f3ac7fb75e1f4b071f84f879a447712725 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 25 Aug 2021 19:45:34 +0200 Subject: Refactor event templates and add edit form. --- src/controllers/event.rs | 26 ++++++++++++++++++------ src/main.rs | 13 ++++++++++-- src/models/event.rs | 18 +++++++++++++++-- templates/base.html.tera | 29 ++++++++++++++++++++++++++ templates/event/edit.html.tera | 12 +++++++++++ templates/event/index.html.tera | 45 +++++++++-------------------------------- templates/event/new.html.tera | 14 +++++-------- 7 files changed, 103 insertions(+), 54 deletions(-) create mode 100644 templates/base.html.tera create mode 100644 templates/event/edit.html.tera 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 { #[derive(Serialize)] struct Context { - event: models::Event, + events: Vec, } - 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 = "
")] pub async fn create(db: db::Connection, form: Form) -> Result { - 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("//edit")] +pub async fn edit(db: db::Connection, eventid: i32) -> Result { + #[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 { + pub async fn get_event(db: &db::Connection, eventid: i32) -> QueryResult { + 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> { + db.run(|conn| { + use crate::schema::event::dsl::*; + event.load(conn) + }).await + } + + pub async fn create(db: &db::Connection, eventname: String, desc: String) -> QueryResult { 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 @@ + + + + + {% block title %}{% endblock title %}Festival Schedule + + + + {% block contents %}{% endblock contents %} + + + + 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 %} +

Edit {{ event.name }}

+ + + + + + + +{% 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 @@ - - - - - {{ event.name }} - - - - -

{{ event.name }}

+{% extends "base" %} +{% block content %} + {% for event in events %} +

{{ event.name }}

- {% if event.description -%} -

{{ event.description }}

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

{{ event.description }}

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

Create event

+{% extends "base" %} +{% block title %}Create new event — {% endblock title %} +{% block contents %} +

Create new event

@@ -12,5 +9,4 @@
- - +{% endblock contents %} -- cgit v1.2.3