]> Untitled Git - lemmy.git/blob - server/src/db/mod.rs
Adding emoji support.
[lemmy.git] / server / src / db / mod.rs
1 use diesel::*;
2 use diesel::dsl::*;
3 use diesel::result::Error;
4 use crate::{Settings};
5 use serde::{Deserialize, Serialize};
6
7 pub mod user;
8 pub mod community;
9 pub mod post;
10 pub mod comment;
11 pub mod post_view;
12 pub mod comment_view;
13 pub mod category;
14 pub mod community_view;
15 pub mod user_view;
16 pub mod moderator;
17 pub mod moderator_views;
18
19 pub trait Crud<T> {
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;
24 }
25
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;
29 }
30
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;
34 }
35
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;
40 }
41
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;
45 }
46
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;
50 }
51
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;
55 }
56
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))
61 }
62
63 #[derive(EnumString,ToString,Debug, Serialize, Deserialize)]
64 pub enum SortType {
65   Hot, New, TopDay, TopWeek, TopMonth, TopYear, TopAll
66 }
67
68 #[derive(EnumString,ToString,Debug, Serialize, Deserialize)]
69 pub enum SearchType {
70   All, Comments, Posts, Communities, Users, Url
71 }
72
73 pub fn fuzzy_search(q: &str) -> String {
74   let replaced = q.replace(" ", "%");
75   format!("%{}%", replaced)
76 }
77
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);
82     (limit, offset)
83 }
84 #[cfg(test)]
85 mod tests {
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());
90   }
91 }
92