add users table
This commit is contained in:
parent
29fc6395a6
commit
50dab32391
8 changed files with 136 additions and 0 deletions
9
migrations/20221206184956_create_users.sql
Normal file
9
migrations/20221206184956_create_users.sql
Normal file
|
@ -0,0 +1,9 @@
|
|||
CREATE TABLE users (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
account_id BIGINT REFERENCES accounts (id),
|
||||
email VARCHAR NOT NULL,
|
||||
password VARCHAR NOT NULL,
|
||||
activated BOOLEAN NOT NULL DEFAULT FALSE
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX index_users_on_email ON users (email);
|
|
@ -1,5 +1,7 @@
|
|||
pub mod account;
|
||||
pub mod status;
|
||||
pub mod user;
|
||||
|
||||
pub use account::*;
|
||||
pub use status::*;
|
||||
pub use user::*;
|
||||
|
|
3
src/data/user/mod.rs
Normal file
3
src/data/user/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
pub mod pg;
|
||||
|
||||
pub use pg::PgUserDataSource;
|
54
src/data/user/pg.rs
Normal file
54
src/data/user/pg.rs
Normal file
|
@ -0,0 +1,54 @@
|
|||
use serde;
|
||||
use sqlx::{Executor, PgPool};
|
||||
|
||||
use crate::core::*;
|
||||
use crate::model::user::{NewUser, User};
|
||||
|
||||
pub struct PgUserDataSource {
|
||||
pool: PgPool,
|
||||
}
|
||||
|
||||
impl PgUserDataSource {
|
||||
pub fn new(pool: PgPool) -> PgUserDataSource {
|
||||
PgUserDataSource { pool }
|
||||
}
|
||||
|
||||
pub async fn by_id<I>(&self, id: I) -> Result<User>
|
||||
where
|
||||
I: Into<Id> + Send,
|
||||
{
|
||||
let id: Id = id.into();
|
||||
let user: User = sqlx::query_as("SELECT * FROM users WHERE id = $1")
|
||||
.bind(id)
|
||||
.fetch_one(&self.pool)
|
||||
.await?;
|
||||
Ok(user)
|
||||
}
|
||||
|
||||
pub async fn by_account(&self, account_id: I) -> Result<User>
|
||||
where
|
||||
I: Into<Id> + Send,
|
||||
{
|
||||
let id: Id = account_id.into();
|
||||
let user: User = sqlx::query_as("SELECT * FROM users WHERE account_id = $1")
|
||||
.bind(id)
|
||||
.fetch_one(&self.pool)
|
||||
.await?;
|
||||
Ok(user)
|
||||
}
|
||||
|
||||
pub async fn create<T>(&self, new: T) -> Result<User>
|
||||
where
|
||||
T: Into<NewUse> + Send,
|
||||
{
|
||||
let new = new.into();
|
||||
let user: User =
|
||||
sqlx::query_as("INSERT INTO users (account_id, email, password) VALUES ($1, $2, $3)")
|
||||
.bind(new.account_id)
|
||||
.bind(new.email)
|
||||
.bind(new.password)
|
||||
.fetch_one(&self.pool)
|
||||
.await?;
|
||||
Ok(user)
|
||||
}
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
pub mod account;
|
||||
pub mod status;
|
||||
pub mod user;
|
||||
|
||||
pub use account::{Account, NewAccount};
|
||||
pub use status::{NewStatus, Status};
|
||||
pub use user::{NewUser, User};
|
||||
|
|
24
src/model/user.rs
Normal file
24
src/model/user.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::FromRow;
|
||||
|
||||
use crate::core::*;
|
||||
|
||||
#[derive(Deserialize, Serialize, FromRow)]
|
||||
pub struct User {
|
||||
pub id: Id,
|
||||
pub account_id: Id,
|
||||
pub email: String,
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
pub struct NewUser {
|
||||
pub account_id: Id,
|
||||
pub email: String,
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
impl Into<Id> for User {
|
||||
fn into(self) -> Id {
|
||||
self.id
|
||||
}
|
||||
}
|
|
@ -2,8 +2,10 @@ use sqlx::PgPool;
|
|||
|
||||
pub mod account;
|
||||
pub mod status;
|
||||
pub mod user;
|
||||
use account::AccountRepo;
|
||||
use status::StatusRepo;
|
||||
use user::UserRepo;
|
||||
|
||||
/// The central collection of all data accessible to the app.
|
||||
/// This is included in `AppState` so it is accessible everywhere.
|
||||
|
@ -11,6 +13,7 @@ use status::StatusRepo;
|
|||
pub struct AppRepo {
|
||||
pub accounts: AccountRepo,
|
||||
pub statuses: StatusRepo,
|
||||
pub users: UserRepo,
|
||||
}
|
||||
|
||||
impl AppRepo {
|
||||
|
@ -18,6 +21,7 @@ impl AppRepo {
|
|||
AppRepo {
|
||||
accounts: AccountRepo::new(db_pool.clone()),
|
||||
statuses: StatusRepo::new(db_pool.clone()),
|
||||
users: UserRepo::new(db_pool.clone()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
38
src/repo/user.rs
Normal file
38
src/repo/user.rs
Normal file
|
@ -0,0 +1,38 @@
|
|||
use sqlx::PgPool;
|
||||
|
||||
use crate::core::*;
|
||||
use crate::data::user::PgUserDataSource;
|
||||
use crate::model::user::{NewUser, User};
|
||||
|
||||
pub struct UserRepo {
|
||||
db: PgUserDataSource,
|
||||
}
|
||||
|
||||
impl UserRepo {
|
||||
pub fn new(db_pool: PgPool) -> UserRepo {
|
||||
UserRepo {
|
||||
db: PgUserDataSource::new(db_pool),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn by_id<I>(&self, id: I) -> Result<User>
|
||||
where
|
||||
I: Into<Id> + Send,
|
||||
{
|
||||
self.db.by_id(id).await
|
||||
}
|
||||
|
||||
pub async fn by_account<I>(&self, account_id: I) -> Result<User>
|
||||
where
|
||||
I: Into<Id> + Send,
|
||||
{
|
||||
self.db.by_account(account_id).await
|
||||
}
|
||||
|
||||
pub async fn create<T>(&self, new: T) -> Result<User>
|
||||
where
|
||||
T: Into<NewUser> + Send,
|
||||
{
|
||||
self.db.create(new).await
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue