separed the db logic in the file "controllers.rs"
parent
de6949a3d1
commit
96a6e0d639
|
@ -1 +1,54 @@
|
|||
#[path = "models.rs"]
|
||||
pub mod models;
|
||||
#[path = "schema.rs"]
|
||||
pub mod schema;
|
||||
|
||||
use crate::diesel::Connection;
|
||||
use crate::diesel::ExpressionMethods;
|
||||
use crate::diesel::QueryDsl;
|
||||
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;
|
||||
|
||||
fn establish_connection() -> PgConnection {
|
||||
dotenv().ok();
|
||||
|
||||
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL not setted");
|
||||
|
||||
PgConnection::establish(&database_url).expect(&format!("Error connecting to {}", database_url))
|
||||
}
|
||||
|
||||
pub fn get_posts(page: i64) -> (Vec<Post>, i64) {
|
||||
use schema::posts::dsl::*;
|
||||
|
||||
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::<Post>(&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<Post, Error> {
|
||||
use schema::posts::dsl::*;
|
||||
|
||||
let connection = establish_connection();
|
||||
let post = posts.find(id_number).first(&connection);
|
||||
|
||||
post
|
||||
}
|
||||
|
|
52
src/main.rs
52
src/main.rs
|
@ -1,6 +1,7 @@
|
|||
#![feature(proc_macro_hygiene, decl_macro)]
|
||||
|
||||
pub mod admin;
|
||||
pub mod controllers;
|
||||
pub mod misc;
|
||||
pub mod models;
|
||||
pub mod schema;
|
||||
|
@ -14,51 +15,17 @@ extern crate dotenv;
|
|||
extern crate rocket_contrib;
|
||||
extern crate tera;
|
||||
|
||||
use self::diesel::prelude::*;
|
||||
use self::models::*;
|
||||
use comrak::{markdown_to_html, ComrakOptions};
|
||||
use diesel::pg::PgConnection;
|
||||
use diesel::result::Error;
|
||||
use dotenv::dotenv;
|
||||
use controllers::{get_post, get_posts, MAX_POSTS_PER_PAGE};
|
||||
use misc::get_context;
|
||||
use rocket::Request;
|
||||
use rocket_contrib::serve::StaticFiles;
|
||||
use rocket_contrib::templates::Template;
|
||||
use std::env;
|
||||
use std::vec::Vec;
|
||||
|
||||
const MAX_POSTS_PER_PAGE: i64 = 20;
|
||||
|
||||
fn establish_connection() -> PgConnection {
|
||||
dotenv().ok();
|
||||
|
||||
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL not setted");
|
||||
PgConnection::establish(&database_url).expect(&format!("Error connecting to {}", database_url))
|
||||
}
|
||||
|
||||
fn get_posts(page: i64) -> (Vec<Post>, i64) {
|
||||
use schema::posts::dsl::*;
|
||||
|
||||
let connection = establish_connection();
|
||||
let visible_posts = posts
|
||||
.filter(published.eq(true))
|
||||
.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
|
||||
.filter(published.eq(true))
|
||||
.count()
|
||||
.get_result(&connection)
|
||||
.expect("Error counting the posts");
|
||||
|
||||
(visible_posts, number_of_posts)
|
||||
}
|
||||
|
||||
#[get("/?<page>")]
|
||||
fn index(page: Option<i8>) -> Template {
|
||||
let page = page.unwrap_or_else(|| 1);
|
||||
let page = page.unwrap_or(1);
|
||||
|
||||
let mut context = get_context();
|
||||
|
||||
|
@ -72,21 +39,12 @@ fn index(page: Option<i8>) -> Template {
|
|||
Template::render("index", context)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
#[get("/post/<title>")]
|
||||
fn show_post(title: String) -> Template {
|
||||
let mut context = get_context();
|
||||
|
||||
let title_splited: Vec<&str> = title.split('-').collect();
|
||||
let id = title_splited.last().unwrap().parse().unwrap_or_else(|_| -1);
|
||||
let id = title_splited.last().unwrap().parse().unwrap_or(-1);
|
||||
|
||||
match get_post(id) {
|
||||
Ok(mut post) => {
|
||||
|
@ -96,7 +54,7 @@ fn show_post(title: String) -> Template {
|
|||
context.insert("post", &post);
|
||||
Template::render("post", context)
|
||||
}
|
||||
Err(_e) => {
|
||||
Err(_) => {
|
||||
let uri = format!("/post/{}", title_splited.join("-"));
|
||||
context.insert("url", &uri);
|
||||
Template::render("404", context)
|
||||
|
|
Loading…
Reference in New Issue