aboutsummaryrefslogtreecommitdiff
path: root/src/db/models.rs
blob: c07a12a761f109454d696e849db164cd1bd3a18d (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
use chrono::naive::NaiveDateTime;
use diesel::prelude::*;

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

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

impl AssociatedData for TextMeme {
    type Associated = (Option<Image>, Option<Audio>);

    fn associated_data(&self, conn: &PgConnection) -> Result<Self::Associated> {
        let image = self.image_id.map(|x: i32| images::table.find(x).first(conn)).transpose()?;
        let audio = self.audio_id.map(|x: i32| audio::table.find(x).first(conn)).transpose()?;

        Ok((image, audio))
    }
}


#[derive(Insertable, Queryable, Identifiable, PartialEq, AsChangeset, Debug)]
#[table_name="image_memes"]
pub struct ImageMeme {
    pub id: i32,
    pub title: String,
    pub image_id: i32,
    pub metadata_id: i32,
}

impl AssociatedData for ImageMeme {
    type Associated = Image;

    fn associated_data(&self, conn: &PgConnection) -> Result<Self::Associated> {
        images::table.find(self.image_id).first(conn).map_err(Error::from)
    }
}


#[derive(Insertable, Queryable, Identifiable, PartialEq, AsChangeset, Debug)]
#[table_name="audio_memes"]
pub struct AudioMeme {
    pub id: i32,
    pub title: String,
    pub audio_id: i32,
    pub metadata_id: i32,
}

impl AssociatedData for AudioMeme {
    type Associated = Audio;

    fn associated_data(&self, conn: &PgConnection) -> Result<Self::Associated> {
        audio::table.find(self.audio_id).first(conn).map_err(Error::from)
    }
}


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

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

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

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