summaryrefslogtreecommitdiff
path: root/src/models.rs
blob: 3d7eb240b90ac55403db484c7b756434dc64738a (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
use crate::schema::config;
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_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(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>,
}