added a mod to posts page

pull/1/head
kirbylife 2019-12-15 18:34:27 -06:00
parent 54fb4d93b8
commit b57c71842d
2 changed files with 39 additions and 34 deletions

View File

@ -10,10 +10,8 @@ use crate::diesel::RunQueryDsl;
use diesel::pg::PgConnection; use diesel::pg::PgConnection;
use diesel::result::Error; use diesel::result::Error;
use dotenv::dotenv; use dotenv::dotenv;
use models::Post;
use std::env; use std::env;
use std::vec::Vec;
pub const MAX_POSTS_PER_PAGE: i64 = 20;
fn establish_connection() -> PgConnection { fn establish_connection() -> PgConnection {
dotenv().ok(); dotenv().ok();
@ -23,15 +21,21 @@ fn establish_connection() -> PgConnection {
PgConnection::establish(&database_url).expect(&format!("Error connecting to {}", database_url)) PgConnection::establish(&database_url).expect(&format!("Error connecting to {}", database_url))
} }
pub fn get_posts(page: i64) -> (Vec<Post>, i64) { pub mod posts {
use crate::controllers::*;
use models::Post;
pub const MAX_POSTS_PER_PAGE: u64 = 20;
pub fn get_posts(page: u64) -> (Vec<Post>, i64) {
use schema::posts::dsl::*; use schema::posts::dsl::*;
let connection = establish_connection(); let connection = establish_connection();
let visible_posts = posts let visible_posts = posts
.filter(published.eq(true)) .filter(published.eq(true))
.order(created_at.desc()) .order(created_at.desc())
.limit(MAX_POSTS_PER_PAGE) .limit(MAX_POSTS_PER_PAGE as i64)
.offset(MAX_POSTS_PER_PAGE * (page - 1)) .offset((MAX_POSTS_PER_PAGE * (page - 1)) as i64)
.load::<Post>(&connection) .load::<Post>(&connection)
.expect("Error loading posts"); .expect("Error loading posts");
@ -42,13 +46,14 @@ pub fn get_posts(page: i64) -> (Vec<Post>, i64) {
.expect("Error counting the posts"); .expect("Error counting the posts");
(visible_posts, number_of_posts) (visible_posts, number_of_posts)
} }
pub fn get_post(id_number: i32) -> Result<Post, Error> { pub fn get_post(id_number: i32) -> Result<Post, Error> {
use schema::posts::dsl::*; use schema::posts::dsl::*;
let connection = establish_connection(); let connection = establish_connection();
let post = posts.find(id_number).first(&connection); let post = posts.find(id_number).first(&connection);
post post
}
} }

View File

@ -16,7 +16,7 @@ extern crate rocket_contrib;
extern crate tera; extern crate tera;
use comrak::{markdown_to_html, ComrakOptions}; use comrak::{markdown_to_html, ComrakOptions};
use controllers::{get_post, get_posts, MAX_POSTS_PER_PAGE}; use controllers::posts;
use misc::get_context; use misc::get_context;
use rocket::response::NamedFile; use rocket::response::NamedFile;
use rocket::Request; use rocket::Request;
@ -26,14 +26,14 @@ use std::path::Path;
use std::vec::Vec; use std::vec::Vec;
#[get("/?<page>")] #[get("/?<page>")]
fn index(page: Option<i8>) -> Template { fn index(page: Option<u64>) -> Template {
let page = page.unwrap_or(1); let page: u64 = page.unwrap_or(1);
let mut context = get_context(); 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("posts", &posts);
context.insert("total_pages", &total_pages); 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 title_splited: Vec<&str> = title.split('-').collect();
let id = title_splited.last().unwrap().parse().unwrap_or(-1); let id = title_splited.last().unwrap().parse().unwrap_or(-1);
match get_post(id) { match posts::get_post(id) {
Ok(mut post) => { Ok(mut post) => {
let content = markdown_to_html(&post.content, &ComrakOptions::default()); let content = markdown_to_html(&post.content, &ComrakOptions::default());
post.content = content; post.content = content;