1 use crate::settings::Settings;
3 use diesel::result::Error;
5 use serde::{Deserialize, Serialize};
8 pub mod code_migrations;
12 pub mod community_view;
14 pub mod moderator_views;
15 pub mod password_reset_request;
18 pub mod private_message;
19 pub mod private_message_view;
24 pub mod user_mention_view;
28 fn create(conn: &PgConnection, form: &T) -> Result<Self, Error>
31 fn read(conn: &PgConnection, id: i32) -> Result<Self, Error>
34 fn update(conn: &PgConnection, id: i32, form: &T) -> Result<Self, Error>
37 fn delete(conn: &PgConnection, id: i32) -> Result<usize, Error>
42 pub trait Followable<T> {
43 fn follow(conn: &PgConnection, form: &T) -> Result<Self, Error>
46 fn ignore(conn: &PgConnection, form: &T) -> Result<usize, Error>
51 pub trait Joinable<T> {
52 fn join(conn: &PgConnection, form: &T) -> Result<Self, Error>
55 fn leave(conn: &PgConnection, form: &T) -> Result<usize, Error>
60 pub trait Likeable<T> {
61 fn read(conn: &PgConnection, id: i32) -> Result<Vec<Self>, Error>
64 fn like(conn: &PgConnection, form: &T) -> Result<Self, Error>
67 fn remove(conn: &PgConnection, form: &T) -> Result<usize, Error>
72 pub trait Bannable<T> {
73 fn ban(conn: &PgConnection, form: &T) -> Result<Self, Error>
76 fn unban(conn: &PgConnection, form: &T) -> Result<usize, Error>
81 pub trait Saveable<T> {
82 fn save(conn: &PgConnection, form: &T) -> Result<Self, Error>
85 fn unsave(conn: &PgConnection, form: &T) -> Result<usize, Error>
90 pub trait Readable<T> {
91 fn mark_as_read(conn: &PgConnection, form: &T) -> Result<Self, Error>
94 fn mark_as_unread(conn: &PgConnection, form: &T) -> Result<usize, Error>
99 pub trait MaybeOptional<T> {
100 fn get_optional(self) -> Option<T>;
103 impl<T> MaybeOptional<T> for T {
104 fn get_optional(self) -> Option<T> {
109 impl<T> MaybeOptional<T> for Option<T> {
110 fn get_optional(self) -> Option<T> {
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))
120 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
131 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
132 pub enum ListingType {
138 #[derive(EnumString, ToString, Debug, Serialize, Deserialize)]
139 pub enum SearchType {
148 pub fn fuzzy_search(q: &str) -> String {
149 let replaced = q.replace(" ", "%");
150 format!("%{}%", replaced)
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);
161 use super::fuzzy_search;
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());