summaryrefslogtreecommitdiff
path: root/src/db/models.rs
blob: 7479fff933747b06f276e456ebe95462936a6fd2 (plain) (blame)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use chrono::naive::NaiveDateTime;
use diesel::prelude::*;

use super::schema::*;
use ::{Result, Error};

#[derive(Queryable, Identifiable, PartialEq, Debug)]
#[table_name="memes"]
pub struct Meme {
    pub id: i32,
    pub title: String,
    pub content: Option<String>,
    pub image_id: Option<i32>,
    pub audio_id: Option<i32>,
    pub metadata_id: i32,
}

impl Meme {
    pub fn image(&self, conn: &PgConnection) -> Option<Result<Image>> {
        self.image_id.map(|x: i32| images::table.find(x).first(conn).map_err(Error::from))
    }

    pub fn audio(&self, conn: &PgConnection) -> Option<Result<Audio>> {
        self.audio_id.map(|x: i32| audio::table.find(x).first(conn).map_err(Error::from))
    }
}

#[derive(Insertable, PartialEq, Debug)]
#[table_name="memes"]
pub struct NewMeme {
    pub title: String,
    pub content: Option<String>,
    pub image_id: Option<i32>,
    pub audio_id: Option<i32>,
    pub metadata_id: i32,
}


#[derive(Queryable, Identifiable, PartialEq, Debug)]
#[table_name="audio"]
pub struct Audio {
    pub id: i32,
    pub data: Vec<u8>,
    pub metadata_id: i32,
}

#[derive(Insertable, PartialEq, Debug)]
#[table_name="audio"]
pub struct NewAudio {
    pub data: Vec<u8>,
    pub metadata_id: i32,
}


#[derive(Queryable, Identifiable, PartialEq, Debug)]
#[table_name="images"]
pub struct Image {
    pub id: i32,
    pub data: Vec<u8>,
    pub metadata_id: i32,
}

#[derive(Insertable, PartialEq, Debug)]
#[table_name="images"]
pub struct NewImage {
    pub data: Vec<u8>,
    pub metadata_id: i32,
}


#[derive(Queryable, Identifiable, PartialEq, Debug)]
#[table_name="metadata"]
pub struct Metadata {
    pub id: i32,
    pub created: NaiveDateTime,
    pub created_by: i64,
}

#[derive(Insertable, PartialEq, Debug)]
#[table_name="metadata"]
pub struct NewMetadata {
    pub created: NaiveDateTime,
    pub created_by: i64,
}


#[derive(Queryable, Identifiable, PartialEq, Debug)]
#[table_name="audit_records"]
pub struct AuditRecord {
    pub id: i32,
    pub updated: NaiveDateTime,
    pub updated_by: i64,
    pub metadata_id: i32,
}

#[derive(Insertable, PartialEq, Debug)]
#[table_name="audit_records"]
pub struct NewAuditRecord {
    pub updated: NaiveDateTime,
    pub updated_by: i64,
    pub metadata_id: i32,
}