added a mod to posts page
parent
54fb4d93b8
commit
b57c71842d
|
@ -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,32 +21,39 @@ 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 schema::posts::dsl::*;
|
use crate::controllers::*;
|
||||||
|
use models::Post;
|
||||||
|
|
||||||
let connection = establish_connection();
|
pub const MAX_POSTS_PER_PAGE: u64 = 20;
|
||||||
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::<Post>(&connection)
|
|
||||||
.expect("Error loading posts");
|
|
||||||
|
|
||||||
let number_of_posts: i64 = posts
|
pub fn get_posts(page: u64) -> (Vec<Post>, i64) {
|
||||||
.filter(published.eq(true))
|
use schema::posts::dsl::*;
|
||||||
.count()
|
|
||||||
.get_result(&connection)
|
|
||||||
.expect("Error counting the posts");
|
|
||||||
|
|
||||||
(visible_posts, number_of_posts)
|
let connection = establish_connection();
|
||||||
}
|
let visible_posts = posts
|
||||||
|
.filter(published.eq(true))
|
||||||
pub fn get_post(id_number: i32) -> Result<Post, Error> {
|
.order(created_at.desc())
|
||||||
use schema::posts::dsl::*;
|
.limit(MAX_POSTS_PER_PAGE as i64)
|
||||||
|
.offset((MAX_POSTS_PER_PAGE * (page - 1)) as i64)
|
||||||
let connection = establish_connection();
|
.load::<Post>(&connection)
|
||||||
let post = posts.find(id_number).first(&connection);
|
.expect("Error loading posts");
|
||||||
|
|
||||||
post
|
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<Post, Error> {
|
||||||
|
use schema::posts::dsl::*;
|
||||||
|
|
||||||
|
let connection = establish_connection();
|
||||||
|
let post = posts.find(id_number).first(&connection);
|
||||||
|
|
||||||
|
post
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
12
src/main.rs
12
src/main.rs
|
@ -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;
|
||||||
|
|
Loading…
Reference in New Issue