]> Untitled Git - lemmy.git/blob - server/src/db/mod.rs
Merge branch 'admin_settings' into dev
[lemmy.git] / server / src / db / mod.rs
1 use crate::settings::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 password_reset_request;
15 pub mod post;
16 pub mod post_view;
17 pub mod private_message;
18 pub mod private_message_view;
19 pub mod site;
20 pub mod site_view;
21 pub mod user;
22 pub mod user_mention;
23 pub mod user_mention_view;
24 pub mod user_view;
25
26 pub trait Crud<T> {
27   fn create(conn: &PgConnection, form: &T) -> Result<Self, Error>
28   where
29     Self: Sized;
30   fn read(conn: &PgConnection, id: i32) -> Result<Self, Error>
31   where
32     Self: Sized;
33   fn update(conn: &PgConnection, id: i32, form: &T) -> Result<Self, Error>
34   where
35     Self: Sized;
36   fn delete(conn: &PgConnection, id: i32) -> Result<usize, Error>
37   where
38     Self: Sized;
39 }
40
41 pub trait Followable<T> {
42   fn follow(conn: &PgConnection, form: &T) -> Result<Self, Error>
43   where
44     Self: Sized;
45   fn ignore(conn: &PgConnection, form: &T) -> Result<usize, Error>
46   where
47     Self: Sized;
48 }
49
50 pub trait Joinable<T> {
51   fn join(conn: &PgConnection, form: &T) -> Result<Self, Error>
52   where
53     Self: Sized;
54   fn leave(conn: &PgConnection, form: &T) -> Result<usize, Error>
55   where
56     Self: Sized;
57 }
58
59 pub trait Likeable<T> {
60   fn read(conn: &PgConnection, id: i32) -> Result<Vec<Self>, Error>
61   where
62     Self: Sized;
63   fn like(conn: &PgConnection, form: &T) -> Result<Self, Error>
64   where
65     Self: Sized;
66   fn remove(conn: &PgConnection, form: &T) -> Result<usize, Error>
67   where
68     Self: Sized;
69 }
70
71 pub trait Bannable<T> {
72   fn ban(conn: &PgConnection, form: &T) -> Result<Self, Error>
73   where
74     Self: Sized;
75   fn unban(conn: &PgConnection, form: &T) -> Result<usize, Error>
76   where
77     Self: Sized;
78 }
79
80 pub trait Saveable<T> {
81   fn save(conn: &PgConnection, form: &T) -> Result<Self, Error>
82   where
83     Self: Sized;
84   fn unsave(conn: &PgConnection, form: &T) -> Result<usize, Error>
85   where
86     Self: Sized;
87 }
88
89 pub trait Readable<T> {
90   fn mark_as_read(conn: &PgConnection, form: &T) -> Result<Self, Error>
91   where
92     Self: Sized;
93   fn mark_as_unread(conn: &PgConnection, form: &T) -> Result<usize, Error>
94   where
95     Self: Sized;
96 }
97
98 pub trait MaybeOptional<T> {
99   fn get_optional(self) -> Option<T>;
100 }
101
102 impl<T> MaybeOptional<T> for T {
103   fn get_optional(self) -> Option<T> {
104     Some(self)
105   }
106 }
107
108 impl<T> MaybeOptional<T> for Option<T> {
109   fn get_optional(self) -> Option<T> {
110     self
111   }
112 }
113
114 pub fn establish_unpooled_connection() -> PgConnection {
115   let db_url = Settings::get().get_database_url();
116   PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url))
117 }
118
119 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
120 pub enum SortType {
121   Hot,
122   New,
123   TopDay,
124   TopWeek,
125   TopMonth,
126   TopYear,
127   TopAll,
128 }
129
130 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
131 pub enum ListingType {
132   All,
133   Subscribed,
134   Community,
135 }
136
137 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
138 pub enum SearchType {
139   All,
140   Comments,
141   Posts,
142   Communities,
143   Users,
144   Url,
145 }
146
147 pub fn fuzzy_search(q: &str) -> String {
148   let replaced = q.replace(" ", "%");
149   format!("%{}%", replaced)
150 }
151
152 pub fn limit_and_offset(page: Option<i64>, limit: Option<i64>) -> (i64, i64) {
153   let page = page.unwrap_or(1);
154   let limit = limit.unwrap_or(10);
155   let offset = limit * (page - 1);
156   (limit, offset)
157 }
158 #[cfg(test)]
159 mod tests {
160   use super::fuzzy_search;
161   #[test]
162   fn test_fuzzy_search() {
163     let test = "This is a fuzzy search";
164     assert_eq!(fuzzy_search(test), "%This%is%a%fuzzy%search%".to_string());
165   }
166 }