From 9d4cd2e146462149334e966f2515438c9e619448 Mon Sep 17 00:00:00 2001 From: antifallobst Date: Fri, 18 Aug 2023 00:16:33 +0200 Subject: [PATCH] feat(db): improved layout for accounts table and defined the projects table --- src/accounts.rs | 44 ++++++++++++++++++++++++++++++++++++++++---- src/api/mod.rs | 32 +++++++++++++++++++++++++------- 2 files changed, 65 insertions(+), 11 deletions(-) diff --git a/src/accounts.rs b/src/accounts.rs index 7b3731a..af0aac1 100644 --- a/src/accounts.rs +++ b/src/accounts.rs @@ -14,6 +14,9 @@ pub struct Account { pub password: String, pub joined: sqlx_chrono::NaiveDateTime, pub verified: bool, + pub follows: Option>, + pub followers: Option>, + 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> { 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> { 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> { 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) diff --git a/src/api/mod.rs b/src/api/mod.rs index 3f886b9..dfe271c 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -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)