From 07ebf7233e96442717101cd277a0be1e51d4177b Mon Sep 17 00:00:00 2001 From: projectmoon Date: Thu, 3 Dec 2020 23:39:55 +0000 Subject: [PATCH] Load a (non existing) character list on the home page --- src/db.rs | 10 +++++++++- src/models.rs | 2 +- src/routes/root.rs | 23 +++++------------------ 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/db.rs b/src/db.rs index e089065..8dc4678 100644 --- a/src/db.rs +++ b/src/db.rs @@ -4,6 +4,14 @@ use diesel::prelude::*; #[database("tenebrous_db")] pub(crate) struct TenebrousDbConn(diesel::SqliteConnection); +pub(crate) fn load_character_list( + conn: TenebrousDbConn, + for_user_id: i32, +) -> QueryResult> { + use crate::schema::characters::dsl::*; + characters.filter(user_id.eq(for_user_id)).load(&*conn) +} + pub(crate) fn load_character( conn: TenebrousDbConn, character_id: i32, @@ -13,7 +21,7 @@ pub(crate) fn load_character( characters .filter(id.eq(character_id)) .limit(1) - .first::(&*conn) + .first(&*conn) .optional() } diff --git a/src/models.rs b/src/models.rs index 39dd0c8..d127fa9 100644 --- a/src/models.rs +++ b/src/models.rs @@ -7,7 +7,7 @@ pub mod characters; #[derive(Eq, PartialEq, Serialize, Debug)] pub struct User { - pub id: usize, + pub id: i32, pub username: String, } diff --git a/src/routes/root.rs b/src/routes/root.rs index e85a2a3..5b93a2f 100644 --- a/src/routes/root.rs +++ b/src/routes/root.rs @@ -1,4 +1,6 @@ +use crate::db; use crate::db::TenebrousDbConn; +use crate::errors::Error; use crate::models::{characters::CharacterEntry, User}; use rocket::response::Redirect; use rocket_contrib::templates::Template; @@ -8,31 +10,16 @@ pub fn routes() -> Vec { } #[get("/")] -fn user_index(user: User, conn: TenebrousDbConn) -> Template { +fn user_index(user: User, conn: TenebrousDbConn) -> Result { use crate::routes::characters::TemplateContext; - let characters = vec![ - CharacterEntry { - id: 1, - user_id: 1, - name: "Bob".to_string(), - viewable: true, - data: Some(vec![]), - }, - CharacterEntry { - id: 2, - user_id: 1, - name: "Alice".to_string(), - viewable: true, - data: Some(vec![]), - }, - ]; + let characters = db::load_character_list(conn, user.id)?; let context = TemplateContext { characters: characters, user: user, }; - Template::render("index", &context) + Ok(Template::render("index", &context)) } #[get("/", rank = 2)]