+ }\r
+\r
+ pub fn get_all_recipe_titles(&self) -> Result<Vec<(i32, String)>> {\r
+ let con = self.pool.get()?;\r
+ let mut stmt = con.prepare("SELECT [id], [title] FROM [Recipe] ORDER BY [title]")?;\r
+ let titles =\r
+ stmt.query_map([], |row| {\r
+ Ok((row.get(0)?, row.get(1)?))\r
+ })?.map(|r| r.unwrap()).collect_vec(); // TODO: remove unwrap.\r
+ Ok(titles)\r
+ }\r
+\r
+ /* Not used for the moment.\r
+ pub fn get_all_recipes(&self) -> Result<Vec<model::Recipe>> {\r
+ let con = self.pool.get()?;\r
+ let mut stmt = con.prepare("SELECT [id], [title] FROM [Recipe] ORDER BY [title]")?;\r
+ let recipes =\r
+ stmt.query_map([], |row| {\r
+ Ok(model::Recipe::new(row.get(0)?, row.get(1)?))\r
+ })?.map(|r| r.unwrap()).collect_vec(); // TODO: remove unwrap.\r
+ Ok(recipes)\r
+ } */\r
+\r
+ pub fn get_recipe(&self, id: i32) -> Result<model::Recipe> {\r
+ let con = self.pool.get()?;\r
+ con.query_row("SELECT [id], [title] FROM [Recipe] WHERE [id] = ?1", [id], |row| {\r
+ Ok(model::Recipe::new(row.get(0)?, row.get(1)?))\r
+ }).map_err(DBError::from)\r
+ }\r
+\r
+ ///\r
+ pub fn sign_up(&self, password: &str, email: &str) -> Result<SignUpResult> {\r
+ self.sign_up_with_given_time(password, email, Utc::now())\r
+ }\r
+\r
+ fn sign_up_with_given_time(&self, password: &str, email: &str, datetime: DateTime<Utc>) -> Result<SignUpResult> {\r
+ let mut con = self.pool.get()?;\r
+ let tx = con.transaction()?;\r
+ let token =\r
+ match tx.query_row("SELECT [id], [validation_token] FROM [User] WHERE [email] = ?1", [email], |r| {\r
+ Ok((r.get::<&str, i32>("id")?, r.get::<&str, Option<String>>("validation_token")?))\r
+ }).optional()? {\r
+ Some((id, validation_token)) => {\r
+ if validation_token.is_none() {\r
+ return Ok(SignUpResult::UserAlreadyExists)\r
+ }\r
+ let token = generate_token();\r
+ let hashed_password = hash(password).map_err(|e| DBError::from_dyn_error(e))?;\r
+ tx.execute("UPDATE [User] SET [validation_token] = ?2, [creation_datetime] = ?3, [password] = ?4 WHERE [id] = ?1", params![id, token, datetime, hashed_password])?;\r
+ token\r
+ },\r
+ None => {\r
+ let token = generate_token();\r
+ let hashed_password = hash(password).map_err(|e| DBError::from_dyn_error(e))?;\r
+ tx.execute("INSERT INTO [User] ([email], [validation_token], [creation_datetime], [password]) VALUES (?1, ?2, ?3, ?4)", params![email, token, datetime, hashed_password])?;\r
+ token\r
+ },\r
+ };\r
+ tx.commit()?;\r
+ Ok(SignUpResult::UserCreatedWaitingForValidation(token))\r
+ }\r
+\r
+ pub fn validation(&self, token: &str, validation_time: Duration) -> Result<ValidationResult> {\r
+ todo!()\r
+ }\r
+\r
+ pub fn sign_in(&self, password: &str, email: String) -> Result<SignInResult> {\r
+ todo!()\r
+ }\r
+\r
+ pub fn authentication(&self, token: &str) -> Result<AuthenticationResult> {\r
+ todo!()\r
+ }\r