add users table

This commit is contained in:
anna 2022-12-07 07:34:45 +01:00
parent 29fc6395a6
commit 50dab32391
Signed by: fef
GPG key ID: EC22E476DC2D3D84
8 changed files with 136 additions and 0 deletions

View 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);

View file

@ -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
View file

@ -0,0 +1,3 @@
pub mod pg;
pub use pg::PgUserDataSource;

54
src/data/user/pg.rs Normal file
View 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)
}
}

View file

@ -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
View 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
}
}

View file

@ -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
View 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
}
}