fish_lib/models/
user.rs

1use crate::traits::model::Model;
2use chrono::{DateTime, Utc};
3use chrono_tz::Tz;
4use diesel::prelude::*;
5use serde::{Deserialize, Serialize};
6use std::str::FromStr;
7
8#[derive(
9    Debug, Default, Serialize, Deserialize, Clone, PartialEq, Queryable, Selectable, AsChangeset,
10)]
11#[diesel(table_name = crate::schema::fish_users)]
12#[diesel(check_for_backend(diesel::pg::Pg))]
13pub struct User {
14    /// Primary key of this user in the database
15    pub id: i64,
16    /// An ID which identifies this user in the external system
17    pub external_id: i64,
18    /// How much of the currency this user has
19    pub credits: i64,
20    /// When the dataset was created
21    pub created_at: DateTime<Utc>,
22    /// When the dataset was last updated
23    pub updated_at: DateTime<Utc>,
24    /// The Timezone of this user, defaults to UTC
25    pub timezone: String,
26}
27
28impl User {
29    pub fn get_timezone(&self) -> Tz {
30        Tz::from_str(&self.timezone).unwrap()
31    }
32
33    pub fn set_timezone(&mut self, timezone: Tz) {
34        self.timezone = timezone.to_string();
35    }
36
37    pub fn get_local_time(&self) -> DateTime<Tz> {
38        Utc::now().with_timezone(&self.get_timezone())
39    }
40}
41
42impl Model for User {
43    type Table = crate::schema::fish_users::table;
44    type PrimaryKeyType = i64;
45    type InsertType = NewUser;
46
47    fn table() -> Self::Table {
48        crate::schema::fish_users::table
49    }
50
51    fn id(&self) -> Self::PrimaryKeyType {
52        self.id
53    }
54}
55
56#[derive(Insertable)]
57#[diesel(table_name = crate::schema::fish_users)]
58#[diesel(check_for_backend(diesel::pg::Pg))]
59pub struct NewUser {
60    pub external_id: i64,
61}