X-Git-Url: http://git.euphorik.ch/?p=recipes.git;a=blobdiff_plain;f=backend%2Fsrc%2Fdata%2Fdb.rs;fp=backend%2Fsrc%2Fdata%2Fdb.rs;h=0358d20ab9b1ed05a022568cafc35dbe5290fab8;hp=10b6f31d7b5870ff0a707190c80dcef136b20dc8;hb=cc2e5b6893b582b4b5c4e7a93e914a189f6a959b;hpb=adcf4a5a5d982489a7e91d4988401eb4512839a3 diff --git a/backend/src/data/db.rs b/backend/src/data/db.rs index 10b6f31..0358d20 100644 --- a/backend/src/data/db.rs +++ b/backend/src/data/db.rs @@ -12,10 +12,11 @@ use r2d2_sqlite::SqliteConnectionManager; use rand::distributions::{Alphanumeric, DistString}; use rusqlite::{named_params, params, OptionalExtension, Params}; -use crate::hash::{hash, verify_password}; -use crate::model; -use crate::user::*; -use crate::{consts, user}; +use crate::{ + hash::{hash, verify_password}, + model, + consts, +}; const CURRENT_DB_VERSION: u32 = 1; @@ -221,11 +222,12 @@ impl Connection { pub fn get_recipe(&self, id: i64) -> Result { let con = self.get()?; con.query_row( - "SELECT [id], [title], [description] FROM [Recipe] WHERE [id] = ?1", + "SELECT [id], [user_id], [title], [description] FROM [Recipe] WHERE [id] = ?1", [id], |row| { Ok(model::Recipe::new( row.get("id")?, + row.get("user_id")?, row.get("title")?, row.get("description")?, )) @@ -234,10 +236,10 @@ impl Connection { .map_err(DBError::from) } - pub fn get_user_login_info(&self, token: &str) -> Result { + pub fn get_user_login_info(&self, token: &str) -> Result { let con = self.get()?; con.query_row("SELECT [last_login_datetime], [ip], [user_agent] FROM [UserLoginToken] WHERE [token] = ?1", [token], |r| { - Ok(UserLoginInfo { + Ok(model::UserLoginInfo { last_login_datetime: r.get("last_login_datetime")?, ip: r.get("ip")?, user_agent: r.get("user_agent")?, @@ -245,13 +247,14 @@ impl Connection { }).map_err(DBError::from) } - pub fn load_user(&self, user_id: i64) -> Result { + pub fn load_user(&self, user_id: i64) -> Result { let con = self.get()?; con.query_row( "SELECT [email] FROM [User] WHERE [id] = ?1", [user_id], |r| { - Ok(User { + Ok(model::User { + id: user_id, email: r.get("email")?, }) }, @@ -290,13 +293,23 @@ impl Connection { } let token = generate_token(); let hashed_password = hash(password).map_err(|e| DBError::from_dyn_error(e))?; - tx.execute("UPDATE [User] SET [validation_token] = ?2, [creation_datetime] = ?3, [password] = ?4 WHERE [id] = ?1", params![id, token, datetime, hashed_password])?; + tx.execute( + "UPDATE [User] + SET [validation_token] = ?2, [creation_datetime] = ?3, [password] = ?4 + WHERE [id] = ?1", + params![id, token, datetime, hashed_password], + )?; token } None => { let token = generate_token(); let hashed_password = hash(password).map_err(|e| DBError::from_dyn_error(e))?; - tx.execute("INSERT INTO [User] ([email], [validation_token], [creation_datetime], [password]) VALUES (?1, ?2, ?3, ?4)", params![email, token, datetime, hashed_password])?; + tx.execute( + "INSERT INTO [User] + ([email], [validation_token], [creation_datetime], [password]) + VALUES (?1, ?2, ?3, ?4)", + params![email, token, datetime, hashed_password], + )?; token } }; @@ -400,7 +413,12 @@ impl Connection { .optional()? { Some((login_id, user_id)) => { - tx.execute("UPDATE [UserLoginToken] SET [last_login_datetime] = ?2, [ip] = ?3, [user_agent] = ?4 WHERE [id] = ?1", params![login_id, Utc::now(), ip, user_agent])?; + tx.execute( + "UPDATE [UserLoginToken] + SET [last_login_datetime] = ?2, [ip] = ?3, [user_agent] = ?4 + WHERE [id] = ?1", + params![login_id, Utc::now(), ip, user_agent], + )?; tx.commit()?; Ok(AuthenticationResult::Ok(user_id)) } @@ -435,21 +453,27 @@ impl Connection { let con = self.get()?; // Verify if an empty recipe already exists. Returns its id if one exists. - match con.query_row( - "SELECT [Recipe].[id] FROM [Recipe] + match con + .query_row( + "SELECT [Recipe].[id] FROM [Recipe] INNER JOIN [Image] ON [Image].[recipe_id] = [Recipe].[id] INNER JOIN [Group] ON [Group].[recipe_id] = [Recipe].[id] - WHERE [Recipe].[user_id] = ?1 AND [Recipe].[estimate_time] = NULL AND [Recipe].[description] = NULL", - [user_id], - |r| { - Ok(r.get::<&str, i64>("id")?) - } - ).optional()? { + WHERE [Recipe].[user_id] = ?1 + AND [Recipe].[estimate_time] = NULL + AND [Recipe].[description] = NULL", + [user_id], + |r| Ok(r.get::<&str, i64>("id")?), + ) + .optional()? + { Some(recipe_id) => Ok(recipe_id), None => { - con.execute("INSERT INTO [Recipe] ([user_id], [title]) VALUES (?1, '')", [user_id])?; + con.execute( + "INSERT INTO [Recipe] ([user_id], [title]) VALUES (?1, '')", + [user_id], + )?; Ok(con.last_insert_rowid()) - }, + } } } @@ -495,7 +519,12 @@ impl Connection { user_agent: &str, ) -> Result { let token = generate_token(); - tx.execute("INSERT INTO [UserLoginToken] ([user_id], [last_login_datetime], [token], [ip], [user_agent]) VALUES (?1, ?2, ?3, ?4, ?5)", params![user_id, Utc::now(), token, ip, user_agent])?; + tx.execute( + "INSERT INTO [UserLoginToken] + ([user_id], [last_login_datetime], [token], [ip], [user_agent]) + VALUES (?1, ?2, ?3, ?4, ?5)", + params![user_id, Utc::now(), token, ip, user_agent], + )?; Ok(token) } } @@ -542,7 +571,8 @@ mod tests { fn sign_up_to_an_already_existing_user() -> Result<()> { let connection = Connection::new_in_memory()?; connection.execute_sql(" - INSERT INTO [User] ([id], [email], [name], [password], [creation_datetime], [validation_token]) + INSERT INTO + [User] ([id], [email], [name], [password], [creation_datetime], [validation_token]) VALUES ( 1, 'paul@atreides.com', @@ -583,7 +613,8 @@ mod tests { let connection = Connection::new_in_memory()?; let token = generate_token(); connection.execute_sql(" - INSERT INTO [User] ([id], [email], [name], [password], [creation_datetime], [validation_token]) + INSERT INTO + [User] ([id], [email], [name], [password], [creation_datetime], [validation_token]) VALUES ( 1, 'paul@atreides.com', @@ -794,7 +825,9 @@ mod tests { let connection = Connection::new_in_memory()?; connection.execute_sql( - "INSERT INTO [User] ([id], [email], [name], [password], [creation_datetime], [validation_token]) VALUES (?1, ?2, ?3, ?4, ?5, ?6)", + "INSERT INTO [User] + ([id], [email], [name], [password], [creation_datetime], [validation_token]) + VALUES (?1, ?2, ?3, ?4, ?5, ?6)", params![ 1, "paul@atreides.com",