Guest User

Untitled

a guest
Oct 18th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. impl DbState {
  2.  
  3. pub fn new(dbname: &str) -> DbState {
  4. println!(">>> setup!");
  5. DbState { conn: SqliteConnection::establish(dbname).unwrap() }
  6. // TODO: create all the tables
  7. }
  8.  
  9. pub fn create_user(&self, email_fld: &str) -> (User, String) {
  10. let mut test_user = User::default();
  11. test_user.username = String::from("john");
  12. test_user.email = String::from(email_fld);
  13. test_user.password = String::from("7654321");
  14.  
  15. diesel::insert_into(users)
  16. .values((
  17. username.eq(&test_user.username),
  18. password.eq(&test_user.password),
  19. email.eq(&test_user.email),
  20. )).execute(&self.conn)
  21. .expect("Test user could not be created.");
  22.  
  23. // FIXME: boo cloning boo
  24. (test_user.clone(), test_user.password)
  25. }
  26.  
  27. pub fn clean_db(&self) {
  28. println!(">>> dropping!");
  29. diesel::delete(users).execute(&self.conn)
  30. .expect("Cannot delete users");
  31. }
  32. }
  33.  
  34. impl Drop for DbState {
  35. fn drop(&mut self) {
  36. self.clean_db();
  37. }
  38. }
  39.  
  40. fn main() {
  41. let state = DbState::new();
  42. let (test_user, _) = state.create_user("hey@domain.con");
  43. println!("test user with id: {}", test_user.id);
  44. }
Add Comment
Please, Sign In to add comment