feat(db): improved layout for accounts table and defined the projects table

This commit is contained in:
antifallobst 2023-08-18 00:16:33 +02:00
parent 31a8c80eca
commit 9d4cd2e146
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
2 changed files with 65 additions and 11 deletions

View File

@ -14,6 +14,9 @@ pub struct Account {
pub password: String,
pub joined: sqlx_chrono::NaiveDateTime,
pub verified: bool,
pub follows: Option<Vec<i64>>,
pub followers: Option<Vec<i64>>,
pub permissions: i64,
}
impl Account {
@ -33,7 +36,7 @@ impl Account {
let joined = sqlx_chrono::Utc::now().naive_utc();
sqlx::query!(
r#"INSERT INTO Accounts (username, email, salt, password, joined, verified) VALUES ($1, $2, $3, $4, $5, false);"#,
r#"INSERT INTO Accounts (username, email, salt, password, joined, verified, permissions) VALUES ($1, $2, $3, $4, $5, false, 0);"#,
username,
email,
salt.to_string(),
@ -54,7 +57,18 @@ impl Account {
pub async fn from_username(pool: &PgPool, username: &String) -> Result<Option<Self>> {
match sqlx::query_as!(
Self,
r#"SELECT id, username, email, salt, password, joined, verified as "verified!: bool" FROM Accounts WHERE username = $1;"#,
r#"SELECT
id,
username,
email,
salt,
password,
joined,
verified as "verified!: bool",
follows,
followers,
permissions
FROM Accounts WHERE username = $1;"#,
username
)
.fetch_one(pool)
@ -69,7 +83,18 @@ impl Account {
pub async fn from_id(pool: &PgPool, id: i64) -> Result<Option<Self>> {
match sqlx::query_as!(
Self,
r#"SELECT id, username, email, salt, password, joined, verified as "verified!: bool" FROM Accounts WHERE id = $1;"#,
r#"SELECT
id,
username,
email,
salt,
password,
joined,
verified as "verified!: bool",
follows,
followers,
permissions
FROM Accounts WHERE id = $1;"#,
id
)
.fetch_one(pool)
@ -84,7 +109,18 @@ impl Account {
pub async fn from_email(pool: &PgPool, email: &String) -> Result<Option<Self>> {
match sqlx::query_as!(
Self,
r#"SELECT id, username, email, salt, password, joined, verified as "verified!: bool" FROM Accounts WHERE email = $1;"#,
r#"SELECT
id,
username,
email,
salt,
password,
joined,
verified as "verified!: bool",
follows,
followers,
permissions
FROM Accounts WHERE email = $1;"#,
email
)
.fetch_one(pool)

View File

@ -15,13 +15,16 @@ pub async fn start(port: u16, pool: PgPool) -> Result<()> {
sqlx::query!(
r#"
CREATE TABLE IF NOT EXISTS Accounts (
id SERIAL8 NOT NULL,
username VARCHAR(32) NOT NULL,
email TEXT NOT NULL,
salt VARCHAR(22) NOT NULL,
password VARCHAR(96) NOT NULL,
joined TIMESTAMP NOT NULL,
verified BOOLEAN NOT NULL,
id SERIAL8 NOT NULL,
username VARCHAR(32) NOT NULL,
email TEXT NOT NULL,
salt VARCHAR(22) NOT NULL,
password VARCHAR(96) NOT NULL,
joined TIMESTAMP NOT NULL,
verified BOOLEAN NOT NULL,
follows BIGINT[],
followers BIGINT[],
permissions BIGINT NOT NULL,
PRIMARY KEY(id)
);
"#
@ -53,6 +56,21 @@ pub async fn start(port: u16, pool: PgPool) -> Result<()> {
.execute(&pool)
.await?;
sqlx::query!(
r#"
CREATE TABLE IF NOT EXISTS Projects (
id SERIAL8 NOT NULL,
name VARCHAR(32) NOT NULL,
desription TEXT,
created TIMESTAMP NOT NULL,
members BIGINT[][] NOT NULL,
PRIMARY KEY(id)
);
"#
)
.execute(&pool)
.await?;
let _ = HttpServer::new(move || {
App::new()
.service(account::calls::register)