]> Untitled Git - lemmy.git/blob - server/src/db/mod.rs
Spanish translations
[lemmy.git] / server / src / db / mod.rs
1 use crate::Settings;
2 use diesel::dsl::*;
3 use diesel::result::Error;
4 use diesel::*;
5 use serde::{Deserialize, Serialize};
6
7 pub mod category;
8 pub mod comment;
9 pub mod comment_view;
10 pub mod community;
11 pub mod community_view;
12 pub mod moderator;
13 pub mod moderator_views;
14 pub mod post;
15 pub mod post_view;
16 pub mod user;
17 pub mod user_view;
18
19 pub trait Crud<T> {
20   fn create(conn: &PgConnection, form: &T) -> Result<Self, Error>
21   where
22     Self: Sized;
23   fn read(conn: &PgConnection, id: i32) -> Result<Self, Error>
24   where
25     Self: Sized;
26   fn update(conn: &PgConnection, id: i32, form: &T) -> Result<Self, Error>
27   where
28     Self: Sized;
29   fn delete(conn: &PgConnection, id: i32) -> Result<usize, Error>
30   where
31     Self: Sized;
32 }
33
34 pub trait Followable<T> {
35   fn follow(conn: &PgConnection, form: &T) -> Result<Self, Error>
36   where
37     Self: Sized;
38   fn ignore(conn: &PgConnection, form: &T) -> Result<usize, Error>
39   where
40     Self: Sized;
41 }
42
43 pub trait Joinable<T> {
44   fn join(conn: &PgConnection, form: &T) -> Result<Self, Error>
45   where
46     Self: Sized;
47   fn leave(conn: &PgConnection, form: &T) -> Result<usize, Error>
48   where
49     Self: Sized;
50 }
51
52 pub trait Likeable<T> {
53   fn read(conn: &PgConnection, id: i32) -> Result<Vec<Self>, Error>
54   where
55     Self: Sized;
56   fn like(conn: &PgConnection, form: &T) -> Result<Self, Error>
57   where
58     Self: Sized;
59   fn remove(conn: &PgConnection, form: &T) -> Result<usize, Error>
60   where
61     Self: Sized;
62 }
63
64 pub trait Bannable<T> {
65   fn ban(conn: &PgConnection, form: &T) -> Result<Self, Error>
66   where
67     Self: Sized;
68   fn unban(conn: &PgConnection, form: &T) -> Result<usize, Error>
69   where
70     Self: Sized;
71 }
72
73 pub trait Saveable<T> {
74   fn save(conn: &PgConnection, form: &T) -> Result<Self, Error>
75   where
76     Self: Sized;
77   fn unsave(conn: &PgConnection, form: &T) -> Result<usize, Error>
78   where
79     Self: Sized;
80 }
81
82 pub trait Readable<T> {
83   fn mark_as_read(conn: &PgConnection, form: &T) -> Result<Self, Error>
84   where
85     Self: Sized;
86   fn mark_as_unread(conn: &PgConnection, form: &T) -> Result<usize, Error>
87   where
88     Self: Sized;
89 }
90
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))
94 }
95
96 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
97 pub enum SortType {
98   Hot,
99   New,
100   TopDay,
101   TopWeek,
102   TopMonth,
103   TopYear,
104   TopAll,
105 }
106
107 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
108 pub enum SearchType {
109   All,
110   Comments,
111   Posts,
112   Communities,
113   Users,
114   Url,
115 }
116
117 pub fn fuzzy_search(q: &str) -> String {
118   let replaced = q.replace(" ", "%");
119   format!("%{}%", replaced)
120 }
121
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);
126   (limit, offset)
127 }
128 #[cfg(test)]
129 mod tests {
130   use super::fuzzy_search;
131   #[test]
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());
135   }
136 }