Quellcode durchsuchen

Update user model

Lee Morgan vor 1 Monat
Ursprung
Commit
80ce51bee3
4 geänderte Dateien mit 12 neuen und 8 gelöschten Zeilen
  1. 1 1
      sql/init.sql
  2. 2 1
      sql/users.sql
  3. 1 1
      src/controllers/user/create.rs
  4. 8 5
      src/models/user.rs

+ 1 - 1
sql/init.sql

@@ -1,4 +1,4 @@
-CREATE DATABASE IF NOT EXISTS chatrpg;
+CREATE DATABASE chatrpg;
 
 \c chatrpg
 

+ 2 - 1
sql/users.sql

@@ -1,9 +1,10 @@
 CREATE TABLE IF NOT EXISTS users(
     id UUID PRIMARY KEY,
 
-    token UUID NOT NULL,
+    session_token UUID NOT NULL,
     name TEXT NOT NULL,
     email TEXT NOT NULL UNIQUE,
     pass_hash TEXT NOT NULL,
+    tokens BIGINT NOT NULL,
     created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
 )

+ 1 - 1
src/controllers/user/create.rs

@@ -33,7 +33,7 @@ pub async fn route(
     let pass_hash = hash_password(&body.password)?;
     let user = User::new(body.name, body.email, pass_hash);
     user.insert_one(&db).await?;
-    Ok(HttpResponse::Ok().json({"success": true}))
+    Ok(HttpResponse::Ok().json(json!({"success": true})))
 }
 
 fn is_bot(url: &Option<String>, time: &Option<String>) -> bool {

+ 8 - 5
src/models/user.rs

@@ -6,10 +6,11 @@ use crate::http_error::HttpError;
 
 pub struct User {
     id: Uuid,
-    token: Uuid,
+    session_token: Uuid,
     name: String,
     email: String,
     pass_hash: String,
+    tokens: i64,
     created_at: DateTime<Utc>
 }
 
@@ -24,24 +25,26 @@ impl User {
     pub fn new(name: String, email: String, pass_hash: String) -> Self {
         Self {
             id: Uuid::now_v7(),
-            token: Uuid::new_v4(),
+            session_token: Uuid::new_v4(),
             name: name,
             email: email.to_lowercase(),
             pass_hash: pass_hash,
+            tokens: 0,
             created_at: Utc::now()
         }
     }
 
     pub async fn insert_one(&self, db: &PgPool) -> Result<(), HttpError> {
         let result = sqlx::query(
-            "INSERT into users(id, token, name, email, pass_hash, created_at)
-            VALUES($1, $2, $3, $4, $5, $6)"
+            "INSERT into users(id, session_token, name, email, pass_hash, tokens, created_at)
+            VALUES($1, $2, $3, $4, $5, $6, $7)"
         )
             .bind(&self.id)
-            .bind(&self.token)
+            .bind(&self.session_token)
             .bind(&self.name)
             .bind(&self.email)
             .bind(&self.pass_hash)
+            .bind(&self.tokens)
             .bind(&self.created_at)
             .execute(db)
             .await;