summaryrefslogtreecommitdiffstats
path: root/server/src/db/user_view.rs
blob: 3d78ae1a0aa47a84c9e35c85895938f5330c70cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use super::*;

table! {
  user_view (id) {
    id -> Int4,
    name -> Varchar,
    fedi_name -> Varchar,
    admin -> Bool,
    banned -> Bool,
    published -> Timestamp,
    number_of_posts -> BigInt,
    post_score -> BigInt,
    number_of_comments -> BigInt,
    comment_score -> BigInt,
  }
}

#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize,QueryableByName,Clone)]
#[table_name="user_view"]
pub struct UserView {
  pub id: i32,
  pub name: String,
  pub fedi_name: String,
  pub admin: bool,
  pub banned: bool,
  pub published: chrono::NaiveDateTime,
  pub number_of_posts: i64,
  pub post_score: i64,
  pub number_of_comments: i64,
  pub comment_score: i64,
}

impl UserView {
  pub fn read(conn: &PgConnection, from_user_id: i32) -> Result<Self, Error> {
    use super::user_view::user_view::dsl::*;

    user_view.find(from_user_id)
    .first::<Self>(conn)
  }

  pub fn admins(conn: &PgConnection) -> Result<Vec<Self>, Error> {
    use super::user_view::user_view::dsl::*;
    user_view.filter(admin.eq(true))
    .load::<Self>(conn)
  }

  pub fn banned(conn: &PgConnection) -> Result<Vec<Self>, Error> {
    use super::user_view::user_view::dsl::*;
    user_view.filter(banned.eq(true))
    .load::<Self>(conn)
  }
}