crablog/site/src/db.rs

102 lines
2.8 KiB
Rust
Raw Normal View History

2020-10-22 00:17:52 +02:00
mod models;
mod schema;
use diesel::prelude::*;
use diesel::sqlite::SqliteConnection;
use models::*;
use std::env;
2020-10-22 00:17:52 +02:00
2020-10-26 12:17:52 +01:00
/// Returns an SqliteConnection if connection successful.
2020-10-22 00:17:52 +02:00
fn establish_connection() -> SqliteConnection {
let db_path = env::var("ROOT_PATH").unwrap() + "/db.sqlite3";
2020-10-22 00:17:52 +02:00
SqliteConnection::establish(&db_path)
2020-10-22 01:38:43 +02:00
.unwrap_or_else(|_| panic!("Error, connection to {} failed.", &db_path))
2020-10-22 00:17:52 +02:00
}
2020-10-26 12:17:52 +01:00
/// Returns all posts
pub fn get_all_posts() -> std::vec::Vec<Post> {
use schema::posts::dsl::*;
let connection = establish_connection();
posts
.filter(published.eq(true))
.order(id.desc())
.load::<Post>(&connection)
.expect("Error, couldn't load posts.")
}
/// Returns the last five posts.
pub fn get_last_five_posts() -> std::vec::Vec<Post> {
2020-10-25 23:23:32 +01:00
use schema::posts::dsl::*;
2020-10-22 00:17:52 +02:00
let connection = establish_connection();
posts
.filter(published.eq(true))
2020-10-22 01:38:43 +02:00
.order(id.desc())
.limit(5)
2020-10-22 00:17:52 +02:00
.load::<Post>(&connection)
2020-10-22 01:38:43 +02:00
.expect("Error, couldn't load posts.")
2020-10-22 00:17:52 +02:00
}
2020-10-26 12:17:52 +01:00
/// Returns the post with the given ID.
2020-10-25 23:23:32 +01:00
pub fn get_post_by_id(post_id: i32) -> Post {
use schema::posts::dsl::*;
2020-10-22 00:17:52 +02:00
let connection = establish_connection();
posts
2020-10-25 23:23:32 +01:00
.find(post_id)
2020-10-22 00:17:52 +02:00
.get_result(&connection)
2020-10-25 23:23:32 +01:00
.expect("Error, couldn't find post.")
2020-10-22 00:17:52 +02:00
}
2020-10-26 12:17:52 +01:00
/// Creates a post and publishes it.
2020-10-25 23:23:32 +01:00
pub fn create_post(title: &str, body: &str) {
2020-10-22 00:17:52 +02:00
use chrono::prelude::*;
use schema::posts;
let connection = establish_connection();
let new_post = NewPost {
title,
body,
published: &true,
publish_date: &Utc::now().naive_utc(),
};
diesel::insert_into(posts::table)
.values(&new_post)
.execute(&connection)
2020-10-22 01:38:43 +02:00
.unwrap_or_else(|_| panic!("Error, couldn't insert new Post."));
2020-10-22 00:17:52 +02:00
}
2020-10-25 23:23:32 +01:00
2020-10-26 12:17:52 +01:00
/// Updates a post with the new title and body.
2020-10-25 23:23:32 +01:00
pub fn edit_post_by_id(post_id: i32, new_title: &str, new_body: &str) {
use schema::posts::dsl::*;
let connection = establish_connection();
diesel::update(posts)
.filter(id.eq(post_id))
.set((title.eq(new_title), body.eq(new_body)))
.execute(&connection)
.expect("Error, couldn't update post.");
}
2020-10-26 12:17:52 +01:00
/// Deletes a post by id.
2020-10-25 23:23:32 +01:00
pub fn delete_post_by_id(post_id: i32) {
use schema::posts::dsl::*;
let connection = establish_connection();
diesel::delete(posts.filter(id.eq(post_id)))
.execute(&connection)
.expect("Error, couldn't update post.");
}
2020-10-26 12:17:52 +01:00
/// Sets the published bool of a post to false.
2020-10-25 23:23:32 +01:00
pub fn hide_post_by_id(post_id: i32) {
use schema::posts::dsl::*;
let connection = establish_connection();
diesel::update(posts)
.filter(id.eq(post_id))
.set(published.eq(false))
.execute(&connection)
.expect("Error, couldn't update post.");
}