diff options
Diffstat (limited to '')
| -rw-r--r-- | src/bin/server/database.rs | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/src/bin/server/database.rs b/src/bin/server/database.rs index 0fac387..1653071 100644 --- a/src/bin/server/database.rs +++ b/src/bin/server/database.rs | |||
| @@ -11,14 +11,22 @@ pub fn init_db() -> Connection { | |||
| 11 | balance INTEGER DEFAULT 1000 | 11 | balance INTEGER DEFAULT 1000 |
| 12 | )", | 12 | )", |
| 13 | [], | 13 | [], |
| 14 | ).unwrap(); | 14 | ) |
| 15 | .unwrap(); | ||
| 15 | // Add column if not exists (for migration) | 16 | // Add column if not exists (for migration) |
| 16 | let _ = conn.execute("ALTER TABLE users ADD COLUMN account_id INTEGER UNIQUE", []); | 17 | let _ = conn.execute("ALTER TABLE users ADD COLUMN account_id INTEGER UNIQUE", []); |
| 17 | let _ = conn.execute("ALTER TABLE users ADD COLUMN balance INTEGER DEFAULT 1000", []); | 18 | let _ = conn.execute( |
| 19 | "ALTER TABLE users ADD COLUMN balance INTEGER DEFAULT 1000", | ||
| 20 | [], | ||
| 21 | ); | ||
| 18 | conn | 22 | conn |
| 19 | } | 23 | } |
| 20 | 24 | ||
| 21 | pub fn add_user(conn: &Connection, username: &str, hashed_password: &str) -> Result<(), Box<dyn std::error::Error>> { | 25 | pub fn add_user( |
| 26 | conn: &Connection, | ||
| 27 | username: &str, | ||
| 28 | hashed_password: &str, | ||
| 29 | ) -> Result<(), Box<dyn std::error::Error>> { | ||
| 22 | loop { | 30 | loop { |
| 23 | let account_id: u32 = rand::random::<u32>(); | 31 | let account_id: u32 = rand::random::<u32>(); |
| 24 | match conn.execute( | 32 | match conn.execute( |
| @@ -33,7 +41,9 @@ pub fn add_user(conn: &Connection, username: &str, hashed_password: &str) -> Res | |||
| 33 | } | 41 | } |
| 34 | 42 | ||
| 35 | pub fn get_hashed_password(conn: &Connection, username: &str) -> Option<String> { | 43 | pub fn get_hashed_password(conn: &Connection, username: &str) -> Option<String> { |
| 36 | let mut stmt = conn.prepare("SELECT hashed_password FROM users WHERE username = ?1").unwrap(); | 44 | let mut stmt = conn |
| 45 | .prepare("SELECT hashed_password FROM users WHERE username = ?1") | ||
| 46 | .unwrap(); | ||
| 37 | let mut rows = stmt.query([username]).unwrap(); | 47 | let mut rows = stmt.query([username]).unwrap(); |
| 38 | if let Ok(Some(row)) = rows.next() { | 48 | if let Ok(Some(row)) = rows.next() { |
| 39 | row.get(0).ok() | 49 | row.get(0).ok() |
| @@ -43,7 +53,9 @@ pub fn get_hashed_password(conn: &Connection, username: &str) -> Option<String> | |||
| 43 | } | 53 | } |
| 44 | 54 | ||
| 45 | pub fn get_account_id(conn: &Connection, username: &str) -> Option<u32> { | 55 | pub fn get_account_id(conn: &Connection, username: &str) -> Option<u32> { |
| 46 | let mut stmt = conn.prepare("SELECT account_id FROM users WHERE username = ?1").unwrap(); | 56 | let mut stmt = conn |
| 57 | .prepare("SELECT account_id FROM users WHERE username = ?1") | ||
| 58 | .unwrap(); | ||
| 47 | let mut rows = stmt.query([username]).unwrap(); | 59 | let mut rows = stmt.query([username]).unwrap(); |
| 48 | if let Ok(Some(row)) = rows.next() { | 60 | if let Ok(Some(row)) = rows.next() { |
| 49 | Some(row.get::<_, u32>(0).ok()?) | 61 | Some(row.get::<_, u32>(0).ok()?) |
| @@ -53,7 +65,9 @@ pub fn get_account_id(conn: &Connection, username: &str) -> Option<u32> { | |||
| 53 | } | 65 | } |
| 54 | 66 | ||
| 55 | pub fn get_balance(conn: &Connection, username: &str) -> Option<i64> { | 67 | pub fn get_balance(conn: &Connection, username: &str) -> Option<i64> { |
| 56 | let mut stmt = conn.prepare("SELECT balance FROM users WHERE username = ?1").unwrap(); | 68 | let mut stmt = conn |
| 69 | .prepare("SELECT balance FROM users WHERE username = ?1") | ||
| 70 | .unwrap(); | ||
| 57 | let mut rows = stmt.query([username]).unwrap(); | 71 | let mut rows = stmt.query([username]).unwrap(); |
| 58 | if let Ok(Some(row)) = rows.next() { | 72 | if let Ok(Some(row)) = rows.next() { |
| 59 | Some(row.get::<_, i64>(0).ok()?) | 73 | Some(row.get::<_, i64>(0).ok()?) |
