From b57c71842d683f6273ddbc1b8a8fc4cfa5cf65a9 Mon Sep 17 00:00:00 2001 From: kirbylife Date: Sun, 15 Dec 2019 18:34:27 -0600 Subject: [PATCH] added a mod to posts page --- src/controllers.rs | 61 +++++++++++++++++++++++++--------------------- src/main.rs | 12 ++++----- 2 files changed, 39 insertions(+), 34 deletions(-) diff --git a/src/controllers.rs b/src/controllers.rs index b3a3296..cb29ddb 100644 --- a/src/controllers.rs +++ b/src/controllers.rs @@ -10,10 +10,8 @@ use crate::diesel::RunQueryDsl; use diesel::pg::PgConnection; use diesel::result::Error; use dotenv::dotenv; -use models::Post; use std::env; - -pub const MAX_POSTS_PER_PAGE: i64 = 20; +use std::vec::Vec; fn establish_connection() -> PgConnection { dotenv().ok(); @@ -23,32 +21,39 @@ fn establish_connection() -> PgConnection { PgConnection::establish(&database_url).expect(&format!("Error connecting to {}", database_url)) } -pub fn get_posts(page: i64) -> (Vec, i64) { - use schema::posts::dsl::*; +pub mod posts { + use crate::controllers::*; + use models::Post; - let connection = establish_connection(); - let visible_posts = posts - .filter(published.eq(true)) - .order(created_at.desc()) - .limit(MAX_POSTS_PER_PAGE) - .offset(MAX_POSTS_PER_PAGE * (page - 1)) - .load::(&connection) - .expect("Error loading posts"); + pub const MAX_POSTS_PER_PAGE: u64 = 20; - let number_of_posts: i64 = posts - .filter(published.eq(true)) - .count() - .get_result(&connection) - .expect("Error counting the posts"); + pub fn get_posts(page: u64) -> (Vec, i64) { + use schema::posts::dsl::*; - (visible_posts, number_of_posts) -} - -pub fn get_post(id_number: i32) -> Result { - use schema::posts::dsl::*; - - let connection = establish_connection(); - let post = posts.find(id_number).first(&connection); - - post + let connection = establish_connection(); + let visible_posts = posts + .filter(published.eq(true)) + .order(created_at.desc()) + .limit(MAX_POSTS_PER_PAGE as i64) + .offset((MAX_POSTS_PER_PAGE * (page - 1)) as i64) + .load::(&connection) + .expect("Error loading posts"); + + let number_of_posts: i64 = posts + .filter(published.eq(true)) + .count() + .get_result(&connection) + .expect("Error counting the posts"); + + (visible_posts, number_of_posts) + } + + pub fn get_post(id_number: i32) -> Result { + use schema::posts::dsl::*; + + let connection = establish_connection(); + let post = posts.find(id_number).first(&connection); + + post + } } diff --git a/src/main.rs b/src/main.rs index 2bcd7be..545897a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ extern crate rocket_contrib; extern crate tera; use comrak::{markdown_to_html, ComrakOptions}; -use controllers::{get_post, get_posts, MAX_POSTS_PER_PAGE}; +use controllers::posts; use misc::get_context; use rocket::response::NamedFile; use rocket::Request; @@ -26,14 +26,14 @@ use std::path::Path; use std::vec::Vec; #[get("/?")] -fn index(page: Option) -> Template { - let page = page.unwrap_or(1); +fn index(page: Option) -> Template { + let page: u64 = page.unwrap_or(1); let mut context = get_context(); - let (posts, n_posts) = get_posts(page as i64); + let (posts, n_posts) = posts::get_posts(page); - let total_pages = (n_posts as f64 / MAX_POSTS_PER_PAGE as f64).ceil() as i64; + let total_pages = (n_posts as f64 / posts::MAX_POSTS_PER_PAGE as f64).ceil() as i64; context.insert("posts", &posts); context.insert("total_pages", &total_pages); @@ -48,7 +48,7 @@ fn show_post(title: String) -> Template { let title_splited: Vec<&str> = title.split('-').collect(); let id = title_splited.last().unwrap().parse().unwrap_or(-1); - match get_post(id) { + match posts::get_post(id) { Ok(mut post) => { let content = markdown_to_html(&post.content, &ComrakOptions::default()); post.content = content;