diff --git a/src/db.rs b/src/db.rs index ac6be30..227fe16 100644 --- a/src/db.rs +++ b/src/db.rs @@ -8,7 +8,7 @@ use models::*; fn establish_connection() -> SqliteConnection { let db_path = "db.sqlite3"; SqliteConnection::establish(&db_path) - .unwrap_or_else(|_| panic!("Error connection to {}", &db_path)) + .unwrap_or_else(|_| panic!("Error, connection to {} failed.", &db_path)) } pub fn get_posts() -> std::vec::Vec { @@ -16,9 +16,10 @@ pub fn get_posts() -> std::vec::Vec { let connection = establish_connection(); posts .filter(published.eq(true)) + .order(id.desc()) .limit(5) .load::(&connection) - .expect("Error loading posts") + .expect("Error, couldn't load posts.") } pub fn get_post_by_id(id: i32) -> Post { @@ -27,7 +28,7 @@ pub fn get_post_by_id(id: i32) -> Post { posts .find(id) .get_result(&connection) - .expect("Couldn't find post") + .expect("Error, couldn't find post") } pub fn add_post(title: &str, body: &str) { @@ -46,5 +47,5 @@ pub fn add_post(title: &str, body: &str) { diesel::insert_into(posts::table) .values(&new_post) .execute(&connection) - .unwrap_or_else(|_| panic!("Error couldn't insert new Post!")); + .unwrap_or_else(|_| panic!("Error, couldn't insert new Post.")); } diff --git a/src/routes.rs b/src/routes.rs index ac3b9a5..5aa09ac 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -19,12 +19,12 @@ async fn blog() -> impl Responder { // one-off render blog template with context let result = Tera::one_off( &(std::fs::read_to_string("templates/blog.html") - .unwrap_or_else(|_| panic!("Couldn't load blog template.")) + .unwrap_or_else(|e| panic!("Error, couldn't load blog template.\n{}", e)) .as_str()), &context, true, ) - .unwrap_or_else(|_| panic!("Couldn't render blog template.")); + .unwrap_or_else(|e| panic!("Error, couldn't render blog template.\n{}", e)); HttpResponse::Ok().body(result) } @@ -38,11 +38,11 @@ async fn blog_permalink(web::Path(post_id): web::Path) -> impl Responder { // one-off render blog template with context let result = Tera::one_off( &(std::fs::read_to_string("templates/blog.html") - .unwrap_or_else(|_| panic!("Couldn't load blog template.")) + .unwrap_or_else(|e| panic!("Error, couldn't load blog template.\n{}", e)) .as_str()), &context, true, ) - .unwrap_or_else(|_| panic!("Couldn't render blog template.")); + .unwrap_or_else(|e| panic!("Error, couldn't render blog template.\n{}", e)); HttpResponse::Ok().body(result) }