Load a (non existing) character list on the home page

This commit is contained in:
jeff 2020-12-03 23:39:55 +00:00
parent 499b1748ce
commit f97035ef9a
3 changed files with 15 additions and 20 deletions

View File

@ -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<Vec<CharacterEntry>> {
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::<CharacterEntry>(&*conn)
.first(&*conn)
.optional()
}

View File

@ -7,7 +7,7 @@ pub mod characters;
#[derive(Eq, PartialEq, Serialize, Debug)]
pub struct User {
pub id: usize,
pub id: i32,
pub username: String,
}

View File

@ -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<rocket::Route> {
}
#[get("/")]
fn user_index(user: User, conn: TenebrousDbConn) -> Template {
fn user_index(user: User, conn: TenebrousDbConn) -> Result<Template, Error> {
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)]