3 use diesel::result::Error;
5 use serde::{Deserialize, Serialize};
11 pub mod community_view;
13 pub mod moderator_views;
20 fn create(conn: &PgConnection, form: &T) -> Result<Self, Error>
23 fn read(conn: &PgConnection, id: i32) -> Result<Self, Error>
26 fn update(conn: &PgConnection, id: i32, form: &T) -> Result<Self, Error>
29 fn delete(conn: &PgConnection, id: i32) -> Result<usize, Error>
34 pub trait Followable<T> {
35 fn follow(conn: &PgConnection, form: &T) -> Result<Self, Error>
38 fn ignore(conn: &PgConnection, form: &T) -> Result<usize, Error>
43 pub trait Joinable<T> {
44 fn join(conn: &PgConnection, form: &T) -> Result<Self, Error>
47 fn leave(conn: &PgConnection, form: &T) -> Result<usize, Error>
52 pub trait Likeable<T> {
53 fn read(conn: &PgConnection, id: i32) -> Result<Vec<Self>, Error>
56 fn like(conn: &PgConnection, form: &T) -> Result<Self, Error>
59 fn remove(conn: &PgConnection, form: &T) -> Result<usize, Error>
64 pub trait Bannable<T> {
65 fn ban(conn: &PgConnection, form: &T) -> Result<Self, Error>
68 fn unban(conn: &PgConnection, form: &T) -> Result<usize, Error>
73 pub trait Saveable<T> {
74 fn save(conn: &PgConnection, form: &T) -> Result<Self, Error>
77 fn unsave(conn: &PgConnection, form: &T) -> Result<usize, Error>
82 pub trait Readable<T> {
83 fn mark_as_read(conn: &PgConnection, form: &T) -> Result<Self, Error>
86 fn mark_as_unread(conn: &PgConnection, form: &T) -> Result<usize, Error>
91 pub fn establish_connection() -> PgConnection {
92 let db_url = Settings::get().db_url;
93 PgConnection::establish(&db_url).expect(&format!("Error connecting to {}", db_url))
96 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
107 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
108 pub enum SearchType {
117 pub fn fuzzy_search(q: &str) -> String {
118 let replaced = q.replace(" ", "%");
119 format!("%{}%", replaced)
122 pub fn limit_and_offset(page: Option<i64>, limit: Option<i64>) -> (i64, i64) {
123 let page = page.unwrap_or(1);
124 let limit = limit.unwrap_or(10);
125 let offset = limit * (page - 1);
130 use super::fuzzy_search;
132 fn test_fuzzy_search() {
133 let test = "This is a fuzzy search";
134 assert_eq!(fuzzy_search(test), "%This%is%a%fuzzy%search%".to_string());