mirror of
https://github.com/actix/examples
synced 2025-01-23 14:24:35 +01:00
59 lines
1.5 KiB
Rust
59 lines
1.5 KiB
Rust
|
use actix::{Actor, SyncContext};
|
||
|
use diesel::pg::PgConnection;
|
||
|
use diesel::r2d2::{ConnectionManager, Pool};
|
||
|
use chrono::{NaiveDateTime, Local};
|
||
|
use uuid::Uuid;
|
||
|
use std::convert::From;
|
||
|
|
||
|
use schema::{users, invitations};
|
||
|
|
||
|
/// This is db executor actor. can be run in parallel
|
||
|
pub struct DbExecutor(pub Pool<ConnectionManager<PgConnection>>);
|
||
|
|
||
|
// Actors communicate exclusively by exchanging messages.
|
||
|
// The sending actor can optionally wait for the response.
|
||
|
// Actors are not referenced directly, but by means of addresses.
|
||
|
// Any rust type can be an actor, it only needs to implement the Actor trait.
|
||
|
impl Actor for DbExecutor {
|
||
|
type Context = SyncContext<Self>;
|
||
|
}
|
||
|
|
||
|
#[derive(Debug, Serialize, Deserialize, Queryable, Insertable)]
|
||
|
#[table_name = "users"]
|
||
|
pub struct User {
|
||
|
pub email: String,
|
||
|
pub password: String,
|
||
|
pub created_at: NaiveDateTime,
|
||
|
}
|
||
|
|
||
|
impl User {
|
||
|
pub fn with_details(email: String, password: String) -> Self {
|
||
|
User {
|
||
|
email,
|
||
|
password,
|
||
|
created_at: Local::now().naive_local(),
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[derive(Debug, Serialize, Deserialize, Queryable, Insertable)]
|
||
|
#[table_name = "invitations"]
|
||
|
pub struct Invitation {
|
||
|
pub id: Uuid,
|
||
|
pub email: String,
|
||
|
pub expires_at: NaiveDateTime,
|
||
|
}
|
||
|
|
||
|
#[derive(Debug, Serialize, Deserialize)]
|
||
|
pub struct SlimUser {
|
||
|
pub email: String,
|
||
|
}
|
||
|
|
||
|
impl From<User> for SlimUser {
|
||
|
fn from(user: User) -> Self {
|
||
|
SlimUser {
|
||
|
email: user.email
|
||
|
}
|
||
|
}
|
||
|
}
|