3 use diesel::result::Error;
5 use serde::{Deserialize, Serialize};
14 pub mod community_view;
17 pub mod moderator_views;
20 fn create(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
21 fn read(conn: &PgConnection, id: i32) -> Result<Self, Error> where Self: Sized;
22 fn update(conn: &PgConnection, id: i32, form: &T) -> Result<Self, Error> where Self: Sized;
23 fn delete(conn: &PgConnection, id: i32) -> Result<usize, Error> where Self: Sized;
26 pub trait Followable<T> {
27 fn follow(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
28 fn ignore(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
31 pub trait Joinable<T> {
32 fn join(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
33 fn leave(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
36 pub trait Likeable<T> {
37 fn read(conn: &PgConnection, id: i32) -> Result<Vec<Self>, Error> where Self: Sized;
38 fn like(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
39 fn remove(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
42 pub trait Bannable<T> {
43 fn ban(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
44 fn unban(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
47 pub trait Saveable<T> {
48 fn save(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
49 fn unsave(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
52 pub trait Readable<T> {
53 fn mark_as_read(conn: &PgConnection, form: &T) -> Result<Self, Error> where Self: Sized;
54 fn mark_as_unread(conn: &PgConnection, form: &T) -> Result<usize, Error> where Self: Sized;
57 pub fn establish_connection() -> PgConnection {
58 let db_url = Settings::get().db_url;
59 PgConnection::establish(&db_url)
60 .expect(&format!("Error connecting to {}", db_url))
63 #[derive(EnumString,ToString,Debug, Serialize, Deserialize)]
65 Hot, New, TopDay, TopWeek, TopMonth, TopYear, TopAll
68 #[derive(EnumString,ToString,Debug, Serialize, Deserialize)]
73 pub fn fuzzy_search(q: &str) -> String {
74 let replaced = q.replace(" ", "%");
75 format!("%{}%", replaced)
78 pub fn limit_and_offset(page: Option<i64>, limit: Option<i64>) -> (i64, i64) {
79 let page = page.unwrap_or(1);
80 let limit = limit.unwrap_or(10);
81 let offset = limit * (page - 1);
86 use super::fuzzy_search;
87 #[test] fn test_fuzzy_search() {
88 let test = "This is a fuzzy search";
89 assert_eq!(fuzzy_search(test), "%This%is%a%fuzzy%search%".to_string());