mirror of
https://github.com/actix/examples
synced 2025-02-02 17:39:05 +01:00
63 lines
1.5 KiB
Rust
63 lines
1.5 KiB
Rust
#![allow(clippy::extra_unused_lifetimes)]
|
|
|
|
use chrono::{NaiveDateTime, TimeDelta, Utc};
|
|
use diesel::{r2d2::ConnectionManager, PgConnection};
|
|
use serde::{Deserialize, Serialize};
|
|
use uuid::Uuid;
|
|
|
|
use super::schema::*;
|
|
|
|
// type alias to use in multiple places
|
|
pub type Pool = r2d2::Pool<ConnectionManager<PgConnection>>;
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Queryable, Insertable)]
|
|
#[diesel(table_name = users)]
|
|
pub struct User {
|
|
pub email: String,
|
|
pub hash: String,
|
|
pub created_at: NaiveDateTime,
|
|
}
|
|
|
|
impl User {
|
|
pub fn from_details<S: Into<String>, T: Into<String>>(email: S, pwd: T) -> Self {
|
|
User {
|
|
email: email.into(),
|
|
hash: pwd.into(),
|
|
created_at: chrono::Local::now().naive_local(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Queryable, Insertable)]
|
|
#[diesel(table_name = invitations)]
|
|
pub struct Invitation {
|
|
pub id: Uuid,
|
|
pub email: String,
|
|
pub expires_at: NaiveDateTime,
|
|
}
|
|
|
|
// any type that implements Into<String> can be used to create Invitation
|
|
impl<T> From<T> for Invitation
|
|
where
|
|
T: Into<String>,
|
|
{
|
|
fn from(email: T) -> Self {
|
|
Invitation {
|
|
id: Uuid::new_v4(),
|
|
email: email.into(),
|
|
expires_at: (Utc::now() + TimeDelta::try_hours(24).unwrap()).naive_utc(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
pub struct SlimUser {
|
|
pub email: String,
|
|
}
|
|
|
|
impl From<User> for SlimUser {
|
|
fn from(user: User) -> Self {
|
|
SlimUser { email: user.email }
|
|
}
|
|
}
|