summaryrefslogtreecommitdiff
path: root/src/models.rs
blob: 4a853692afb4dfb2295b5ea6c22c7767d047cf13 (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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use crate::schema::config;
use crate::schema::entries;
use crate::schema::raw_data;
use crate::schema::strava_tokens;
use crate::schema::tasks;
use crate::schema::users;
use chrono::DateTime;
use chrono::Utc;
use diesel::deserialize;
use diesel::deserialize::FromSql;
use diesel::pg::Pg;
use diesel::serialize;
use diesel::serialize::Output;
use diesel::serialize::ToSql;
use diesel::sql_types;
use serde::Deserialize;
use serde::Serialize;
use serde_json::Value;
use std::fmt;
use std::io::Write;

#[derive(PartialEq, Debug, Clone, Copy, AsExpression, FromSqlRow)]
#[sql_type = "sql_types::Text"]
pub enum TaskState {
    NEW = 0,
    SUCCESSFUL,
    FAILED,
}

impl ToSql<sql_types::Text, Pg> for TaskState {
    fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
        let t = match *self {
            TaskState::NEW => "new".to_string(),
            TaskState::SUCCESSFUL => "success".to_string(),
            TaskState::FAILED => "failed".to_string(),
        };
        <String as ToSql<sql_types::Text, Pg>>::to_sql(&t, out)
    }
}

impl FromSql<sql_types::Text, Pg> for TaskState {
    fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
        let s = <String as FromSql<sql_types::Text, Pg>>::from_sql(bytes)?;
        match s.as_str() {
            "new" => Ok(TaskState::NEW),
            "success" => Ok(TaskState::SUCCESSFUL),
            "failed" => Ok(TaskState::FAILED),
            &_ => Err("Unrecognized task state".into()),
        }
    }
}

#[derive(Insertable)]
#[table_name = "tasks"]
pub struct NewTask<'a> {
    pub start_at: DateTime<Utc>,
    pub state: TaskState,
    pub username: &'a str,
    pub payload: &'a Value,
}

#[derive(Queryable, Debug, Clone)]
pub struct Task {
    pub id: i64,
    pub state: TaskState,
    pub start_at: DateTime<Utc>,
    pub username: String,
    pub payload: Value,
}

#[derive(Insertable, Queryable)]
#[table_name = "config"]
pub struct Config {
    pub strava_client_secret: String,
    pub strava_client_id: String,
    pub rocket_secret_key: String,
    pub singleton: bool,
}

#[derive(Insertable)]
#[table_name = "users"]
pub struct NewUser<'a> {
    pub username: &'a str,
    pub password: &'a str,
}

#[derive(Queryable, Clone)]
pub struct User {
    pub username: String,
    pub password: String,
}

impl fmt::Debug for User {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "User {{ username: {}, password: <secret> }}",
            self.username
        )
    }
}

#[derive(AsChangeset, Insertable, Queryable)]
#[table_name = "strava_tokens"]
pub struct StravaToken {
    pub username: String,
    pub refresh_token: String,
    pub access_token: String,
    pub expires_at: DateTime<Utc>,
}

#[derive(PartialEq, Debug, Clone, Copy, AsExpression, FromSqlRow, Serialize, Deserialize)]
#[sql_type = "sql_types::Text"]
pub enum DataType {
    StravaActivity = 0,
}

impl ToSql<sql_types::Text, Pg> for DataType {
    fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
        let t = match *self {
            DataType::StravaActivity => "s:activi".to_string(),
        };
        <String as ToSql<sql_types::Text, Pg>>::to_sql(&t, out)
    }
}

impl FromSql<sql_types::Text, Pg> for DataType {
    fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
        let s = <String as FromSql<sql_types::Text, Pg>>::from_sql(bytes)?;
        match s.as_str() {
            "s:activi" => Ok(DataType::StravaActivity),
            &_ => Err("Unrecognized data type".into()),
        }
    }
}

#[derive(Insertable, Queryable, Debug, Serialize, Deserialize, Clone)]
#[table_name = "raw_data"]
pub struct RawDataKey {
    pub data_type: DataType,
    pub id: i64,
    pub username: String,
}

#[derive(Insertable, Queryable, Identifiable, Debug, Serialize, Deserialize, Clone)]
#[table_name = "raw_data"]
pub struct RawData {
    pub data_type: DataType,
    pub id: i64,
    pub username: String,
    pub payload: Value,
    pub entry_type: Option<String>,
    pub entry_id: Option<i64>,
}

#[derive(Insertable, Debug, Serialize, Deserialize, Clone)]
#[table_name = "entries"]
pub struct NewEntry<'a> {
    pub username: &'a str,
    pub entry_type: &'a str,
    pub timestamp: Option<DateTime<Utc>>,
    pub payload: Value,
}

#[derive(Queryable, Debug, Serialize, Deserialize, Clone)]
pub struct Entry {
    pub username: String,
    pub entry_type: String,
    pub id: i64,
    pub timestamp: Option<DateTime<Utc>>,
    pub payload: Value,
}