Compare commits
2 Commits
499b1748ce
...
881518cc8f
Author | SHA1 | Date |
---|---|---|
jeff | 881518cc8f | |
jeff | f97035ef9a |
10
src/db.rs
10
src/db.rs
|
@ -4,6 +4,14 @@ use diesel::prelude::*;
|
||||||
#[database("tenebrous_db")]
|
#[database("tenebrous_db")]
|
||||||
pub(crate) struct TenebrousDbConn(diesel::SqliteConnection);
|
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(
|
pub(crate) fn load_character(
|
||||||
conn: TenebrousDbConn,
|
conn: TenebrousDbConn,
|
||||||
character_id: i32,
|
character_id: i32,
|
||||||
|
@ -13,7 +21,7 @@ pub(crate) fn load_character(
|
||||||
characters
|
characters
|
||||||
.filter(id.eq(character_id))
|
.filter(id.eq(character_id))
|
||||||
.limit(1)
|
.limit(1)
|
||||||
.first::<CharacterEntry>(&*conn)
|
.first(&*conn)
|
||||||
.optional()
|
.optional()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ pub mod characters;
|
||||||
|
|
||||||
#[derive(Eq, PartialEq, Serialize, Debug)]
|
#[derive(Eq, PartialEq, Serialize, Debug)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
pub id: usize,
|
pub id: i32,
|
||||||
pub username: String,
|
pub username: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
|
use crate::db;
|
||||||
use crate::db::TenebrousDbConn;
|
use crate::db::TenebrousDbConn;
|
||||||
|
use crate::errors::Error;
|
||||||
use crate::models::{characters::CharacterEntry, User};
|
use crate::models::{characters::CharacterEntry, User};
|
||||||
use rocket::response::Redirect;
|
use rocket::response::Redirect;
|
||||||
use rocket_contrib::templates::Template;
|
use rocket_contrib::templates::Template;
|
||||||
|
@ -8,31 +10,16 @@ pub fn routes() -> Vec<rocket::Route> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn user_index(user: User, conn: TenebrousDbConn) -> Template {
|
fn user_index(user: User, conn: TenebrousDbConn) -> Result<Template, Error> {
|
||||||
use crate::routes::characters::TemplateContext;
|
use crate::routes::characters::TemplateContext;
|
||||||
let characters = vec![
|
let characters = db::load_character_list(conn, user.id)?;
|
||||||
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 context = TemplateContext {
|
let context = TemplateContext {
|
||||||
characters: characters,
|
characters: characters,
|
||||||
user: user,
|
user: user,
|
||||||
};
|
};
|
||||||
|
|
||||||
Template::render("index", &context)
|
Ok(Template::render("index", &context))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/", rank = 2)]
|
#[get("/", rank = 2)]
|
||||||
|
|
|
@ -13,7 +13,5 @@
|
||||||
</li>
|
</li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<p>Try going to <a href="/hello/YourName">/hello/YourName</a></p>
|
|
||||||
</div>
|
</div>
|
||||||
{% endblock content %}
|
{% endblock content %}
|
||||||
|
|
Loading…
Reference in New Issue