Sign up form and other stuff
[recipes.git] / backend / src / db.rs
index 28bf62c..bae57d0 100644 (file)
@@ -1,4 +1,4 @@
-use std::{fmt::Display, fs::{self, File}, path::Path, io::Read};\r
+use std::{fmt, fs::{self, File}, path::Path, io::Read};\r
 \r
 use itertools::Itertools;\r
 use chrono::{prelude::*, Duration};\r
@@ -22,6 +22,14 @@ pub enum DBError {
     Other(String),\r
 }\r
 \r
+impl fmt::Display for DBError {\r
+    fn fmt(&self, f: &mut fmt::Formatter) -> std::result::Result<(), fmt::Error> {\r
+        write!(f, "{:?}", self)\r
+    }\r
+}\r
+\r
+impl std::error::Error for DBError { }\r
+\r
 impl From<rusqlite::Error> for DBError  {\r
     fn from(error: rusqlite::Error) -> Self {\r
         DBError::SqliteError(error)\r
@@ -95,7 +103,7 @@ impl Connection {
         Self::create_connection(SqliteConnectionManager::file(file))\r
     }\r
 \r
-    fn create_connection(manager: SqliteConnectionManager) -> Result<Connection> {;\r
+    fn create_connection(manager: SqliteConnectionManager) -> Result<Connection> {\r
         let pool = r2d2::Pool::new(manager).unwrap();\r
         let connection = Connection { pool };\r
         connection.create_or_update()?;\r
@@ -206,11 +214,11 @@ impl Connection {
     }\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
+    pub fn sign_up(&self, email: &str, password: &str) -> Result<SignUpResult> {\r
+        self.sign_up_with_given_time(email, password, Utc::now())\r
     }\r
 \r
-    fn sign_up_with_given_time(&self, password: &str, email: &str, datetime: DateTime<Utc>) -> Result<SignUpResult> {\r
+    fn sign_up_with_given_time(&self, email: &str, password: &str, datetime: DateTime<Utc>) -> Result<SignUpResult> {\r
         let mut con = self.pool.get()?;\r
         let tx = con.transaction()?;\r
         let token =\r
@@ -313,7 +321,7 @@ impl Connection {
     }\r
 \r
     /// Execute a given SQL file.\r
-    pub fn execute_file<P: AsRef<Path> + Display>(&self, file: P) -> Result<()> {\r
+    pub fn execute_file<P: AsRef<Path> + fmt::Display>(&self, file: P) -> Result<()> {\r
         let con = self.pool.get()?;\r
         let sql = load_sql_file(file)?;\r
         con.execute_batch(&sql).map_err(DBError::from)\r
@@ -334,7 +342,7 @@ impl Connection {
     }\r
 }\r
 \r
-fn load_sql_file<P: AsRef<Path> + Display>(sql_file: P) -> Result<String> {\r
+fn load_sql_file<P: AsRef<Path> + fmt::Display>(sql_file: P) -> Result<String> {\r
     let mut file = File::open(&sql_file).map_err(|err| DBError::Other(format!("Cannot open SQL file ({}): {}", &sql_file, err.to_string())))?;\r
     let mut sql = String::new();\r
     file.read_to_string(&mut sql).map_err(|err| DBError::Other(format!("Cannot read SQL file ({}) : {}", &sql_file, err.to_string())))?;\r
@@ -352,7 +360,7 @@ mod tests {
     #[test]\r
     fn sign_up() -> Result<()> {\r
         let connection = Connection::new_in_memory()?;\r
-        match connection.sign_up("12345", "paul@test.org")? {\r
+        match connection.sign_up("paul@test.org", "12345")? {\r
             SignUpResult::UserCreatedWaitingForValidation(_) => (), // Nominal case.\r
             other => panic!("{:?}", other),\r
         }\r
@@ -372,7 +380,7 @@ mod tests {
                     0,\r
                     NULL\r
                 );", [])?;\r
-        match connection.sign_up("12345", "paul@test.org")? {\r
+        match connection.sign_up("paul@test.org", "12345")? {\r
             SignUpResult::UserAlreadyExists => (), // Nominal case.\r
             other => panic!("{:?}", other),\r
         }\r
@@ -393,7 +401,7 @@ mod tests {
                     0,\r
                     :token\r
                 );", named_params! { ":token": token })?;\r
-        match connection.sign_up("12345", "paul@test.org")? {\r
+        match connection.sign_up("paul@test.org", "12345")? {\r
             SignUpResult::UserCreatedWaitingForValidation(_) => (), // Nominal case.\r
             other => panic!("{:?}", other),\r
         }\r
@@ -404,7 +412,7 @@ mod tests {
     fn sign_up_then_send_validation_at_time() -> Result<()> {\r
         let connection = Connection::new_in_memory()?;\r
         let validation_token =\r
-            match connection.sign_up("12345", "paul@test.org")? {\r
+            match connection.sign_up("paul@test.org", "12345")? {\r
                 SignUpResult::UserCreatedWaitingForValidation(token) => token, // Nominal case.\r
                 other => panic!("{:?}", other),\r
             };\r
@@ -419,7 +427,7 @@ mod tests {
     fn sign_up_then_send_validation_too_late() -> Result<()> {\r
         let connection = Connection::new_in_memory()?;\r
         let validation_token =\r
-            match connection.sign_up_with_given_time("12345", "paul@test.org", Utc::now() - Duration::days(1))? {\r
+            match connection.sign_up_with_given_time("paul@test.org", "12345", Utc::now() - Duration::days(1))? {\r
                 SignUpResult::UserCreatedWaitingForValidation(token) => token, // Nominal case.\r
                 other => panic!("{:?}", other),\r
             };\r
@@ -434,7 +442,7 @@ mod tests {
     fn sign_up_then_send_validation_with_bad_token() -> Result<()> {\r
         let connection = Connection::new_in_memory()?;\r
         let _validation_token =\r
-            match connection.sign_up("12345", "paul@test.org")? {\r
+            match connection.sign_up("paul@test.org", "12345")? {\r
                 SignUpResult::UserCreatedWaitingForValidation(token) => token, // Nominal case.\r
                 other => panic!("{:?}", other),\r
             };\r
@@ -450,12 +458,12 @@ mod tests {
     fn sign_up_then_send_validation_then_sign_in() -> Result<()> {\r
         let connection = Connection::new_in_memory()?;\r
 \r
-        let password = "12345";\r
         let email = "paul@test.org";\r
+        let password = "12345";\r
 \r
         // Sign up.\r
         let validation_token =\r
-            match connection.sign_up(password, email)? {\r
+            match connection.sign_up(email, password)? {\r
                 SignUpResult::UserCreatedWaitingForValidation(token) => token, // Nominal case.\r
                 other => panic!("{:?}", other),\r
             };\r
@@ -479,12 +487,12 @@ mod tests {
     fn sign_up_then_send_validation_then_authentication() -> Result<()> {\r
         let connection = Connection::new_in_memory()?;\r
 \r
-        let password = "12345";\r
         let email = "paul@test.org";\r
+        let password = "12345";\r
 \r
         // Sign up.\r
         let validation_token =\r
-            match connection.sign_up(password, email)? {\r
+            match connection.sign_up(email, password)? {\r
                 SignUpResult::UserCreatedWaitingForValidation(token) => token, // Nominal case.\r
                 other => panic!("{:?}", other),\r
             };\r
@@ -519,12 +527,12 @@ mod tests {
     fn sign_up_then_send_validation_then_sign_out_then_sign_in() -> Result<()> {\r
         let connection = Connection::new_in_memory()?;\r
 \r
-        let password = "12345";\r
         let email = "paul@test.org";\r
+        let password = "12345";\r
 \r
         // Sign up.\r
         let validation_token =\r
-            match connection.sign_up(password, email)? {\r
+            match connection.sign_up(email, password)? {\r
                 SignUpResult::UserCreatedWaitingForValidation(token) => token, // Nominal case.\r
                 other => panic!("{:?}", other),\r
             };\r