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