Skip to content

Commit

Permalink
use MysqlConnection instead of DBConnection in database models
Browse files Browse the repository at this point in the history
  • Loading branch information
Tazoeur committed Mar 23, 2020
1 parent 8c6771d commit 27eb3f5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
26 changes: 13 additions & 13 deletions src/database/models/address.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::database::schema::addresses;
use crate::database::schema::addresses::dsl::addresses as table;
use crate::database::DBConnection;
use diesel::prelude::*;
use diesel::MysqlConnection;
use either::*;

#[derive(Identifiable, Queryable, Associations, Serialize, Deserialize, Clone, Debug)]
Expand All @@ -19,18 +19,18 @@ pub struct Address {
impl Address {
/* ------------------------------- STATIC ------------------------------ */

// from :: (DBConnection, Integer) -> Option<Address>
pub fn from(conn: &DBConnection, id: &u32) -> Option<Self> {
table.find(id).first::<Address>(&**conn).ok()
// from :: (MysqlConnection, Integer) -> Option<Address>
pub fn from(conn: &MysqlConnection, id: &u32) -> Option<Self> {
table.find(id).first::<Address>(conn).ok()
}

// all :: (DBConnection, Integer) -> Option<Address>
pub fn all(conn: &DBConnection) -> Vec<Self> {
table.load(&**conn).unwrap_or(vec![])
// all :: (MysqlConnection, Integer) -> Option<Address>
pub fn all(conn: &MysqlConnection) -> Vec<Self> {
table.load(conn).unwrap_or(vec![])
}

/// Get the address record that fits the `minima` given.
pub fn select_minima(conn: &DBConnection, minima: &AddressMinima) -> Option<Self> {
pub fn select_minima(conn: &MysqlConnection, minima: &AddressMinima) -> Option<Self> {
let filtered = table.filter(
addresses::street
.eq(&minima.street)
Expand All @@ -45,27 +45,27 @@ impl Address {
match &minima.box_number {
None => filtered
.filter(addresses::box_number.is_null())
.first::<Address>(&**conn)
.first::<Address>(conn)
.ok(),
Some(box_n) => filtered
.filter(
addresses::box_number
.is_not_null()
.and(addresses::box_number.eq(box_n)),
)
.first::<Address>(&**conn)
.first::<Address>(conn)
.ok(),
}
}

// insert_minima :: (DBConnection, AddressMinima) -> Either<Address, Address>
pub fn insert_minima(conn: &DBConnection, minima: &AddressMinima) -> Either<Self, Self> {
// insert_minima :: (MysqlConnection, AddressMinima) -> Either<Address, Address>
pub fn insert_minima(conn: &MysqlConnection, minima: &AddressMinima) -> Either<Self, Self> {
if let Some(past) = Address::select_minima(conn, minima) {
Left(past)
} else {
diesel::insert_into(table)
.values(minima)
.execute(&**conn)
.execute(conn)
.expect("Failed address insertion");
Right(
Address::select_minima(conn, minima)
Expand Down
38 changes: 19 additions & 19 deletions src/database/models/user.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::address::Address;
use crate::database::schema::users;
use crate::database::schema::users::dsl::users as table;
use crate::database::DBConnection;
use chrono::NaiveDateTime;
use diesel::prelude::*;
use diesel::MysqlConnection;
use either::*;
use regex::Regex;
use rocket_contrib::json::JsonValue;
Expand Down Expand Up @@ -33,26 +33,26 @@ impl User {
/* ------------------------------- STATIC ------------------------------ */

// from :: (DBConnection, Integer) -> Option<User>
pub fn from(conn: &DBConnection, id: &u32) -> Option<Self> {
table.find(id).first::<User>(&**conn).ok()
pub fn from(conn: &MysqlConnection, id: &u32) -> Option<Self> {
table.find(id).first::<User>(conn).ok()
}

// all :: (DBConnection) -> Vec<User>
pub fn all(conn: &DBConnection) -> Vec<Self> {
table.load(&**conn).unwrap_or(vec![])
// all :: (MysqlConnection) -> Vec<User>
pub fn all(conn: &MysqlConnection) -> Vec<Self> {
table.load(conn).unwrap_or(vec![])
}

// by_email :: (DBConnection, String) -> Option<User>
pub fn by_email(conn: &DBConnection, email: &str) -> Option<User> {
if let Ok(user) = table.filter(users::email.eq(email)).first(&**conn) {
// by_email :: (MysqlConnection, String) -> Option<User>
pub fn by_email(conn: &MysqlConnection, email: &str) -> Option<User> {
if let Ok(user) = table.filter(users::email.eq(email)).first(conn) {
Some(user)
} else {
None
}
}

// is_available_email :: (DBConnection, String) -> Boolean
pub fn is_available_email(conn: &DBConnection, email: &str) -> bool {
// is_available_email :: (MysqlConnection, String) -> Boolean
pub fn is_available_email(conn: &MysqlConnection, email: &str) -> bool {
User::by_email(conn, email).is_none()
}

Expand All @@ -62,24 +62,24 @@ impl User {
re.is_match(email)
}

// select_minima :: (DBConnection, UserMinima) -> Option<User>
pub fn select_minima(conn: &DBConnection, minima: &UserMinima) -> Option<Self> {
// select_minima :: (MysqlConnection, UserMinima) -> Option<User>
pub fn select_minima(conn: &MysqlConnection, minima: &UserMinima) -> Option<Self> {
table
.filter(users::email.eq(minima.email.clone()))
.first::<User>(&**conn)
.first::<User>(conn)
.ok()
}

// insert_minima :: (DBConnection, UserMinima) -> Either<User, User>
pub fn insert_minima(conn: &DBConnection, minima: &UserMinima) -> Either<Self, Self> {
// insert_minima :: (MysqlConnection, UserMinima) -> Either<User, User>
pub fn insert_minima(conn: &MysqlConnection, minima: &UserMinima) -> Either<Self, Self> {
if let Some(past) = User::select_minima(conn, minima) {
Left(past)
} else {
let mut inserted = minima.clone();
inserted.password = bcrypt::hash(&minima.password, 8).expect("Unable to hash password");
diesel::insert_into(table)
.values(inserted)
.execute(&**conn)
.execute(conn)
.expect("Error inserting address");
Right(
User::select_minima(conn, minima)
Expand All @@ -98,11 +98,11 @@ impl User {
bcrypt::verify(password, &self.password).expect("Fatal: BCrypt error")
}

pub fn activate(&self, conn: &DBConnection) {
pub fn activate(&self, conn: &MysqlConnection) {
use crate::database::schema::users::dsl::*;
diesel::update(users.filter(id.eq(self.id)))
.set((active.eq(true), token.eq(None: Option<String>)))
.execute(&**conn)
.execute(conn)
.expect(&format!("Error updating user #{}", self.id));
}

Expand Down

0 comments on commit 27eb3f5

Please sign in to comment.