Update and Migrate Diesel to 2.0
Almost have tag support working, still figuring out how to get photo tags.
This commit is contained in:
@@ -2,9 +2,9 @@ use bcrypt::{hash, verify, DEFAULT_COST};
|
||||
use diesel::prelude::*;
|
||||
use diesel::sqlite::SqliteConnection;
|
||||
use std::{
|
||||
ops::Deref,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
use std::ops::DerefMut;
|
||||
|
||||
use crate::database::models::{Favorite, InsertFavorite, InsertUser, User};
|
||||
|
||||
@@ -12,9 +12,9 @@ pub mod models;
|
||||
pub mod schema;
|
||||
|
||||
pub trait UserDao {
|
||||
fn create_user(&self, user: &str, password: &str) -> Option<User>;
|
||||
fn get_user(&self, user: &str, password: &str) -> Option<User>;
|
||||
fn user_exists(&self, user: &str) -> bool;
|
||||
fn create_user(&mut self, user: &str, password: &str) -> Option<User>;
|
||||
fn get_user(&mut self, user: &str, password: &str) -> Option<User>;
|
||||
fn user_exists(&mut self, user: &str) -> bool;
|
||||
}
|
||||
|
||||
pub struct SqliteUserDao {
|
||||
@@ -31,7 +31,7 @@ impl SqliteUserDao {
|
||||
|
||||
impl UserDao for SqliteUserDao {
|
||||
// TODO: Should probably use Result here
|
||||
fn create_user(&self, user: &str, pass: &str) -> std::option::Option<User> {
|
||||
fn create_user(&mut self, user: &str, pass: &str) -> Option<User> {
|
||||
use schema::users::dsl::*;
|
||||
|
||||
let hashed = hash(pass, DEFAULT_COST);
|
||||
@@ -41,12 +41,12 @@ impl UserDao for SqliteUserDao {
|
||||
username: user,
|
||||
password: &hash,
|
||||
})
|
||||
.execute(&self.connection)
|
||||
.execute(&mut self.connection)
|
||||
.unwrap();
|
||||
|
||||
users
|
||||
.filter(username.eq(username))
|
||||
.load::<User>(&self.connection)
|
||||
.load::<User>(&mut self.connection)
|
||||
.unwrap()
|
||||
.first()
|
||||
.cloned()
|
||||
@@ -55,12 +55,12 @@ impl UserDao for SqliteUserDao {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_user(&self, user: &str, pass: &str) -> Option<User> {
|
||||
fn get_user(&mut self, user: &str, pass: &str) -> Option<User> {
|
||||
use schema::users::dsl::*;
|
||||
|
||||
match users
|
||||
.filter(username.eq(user))
|
||||
.load::<User>(&self.connection)
|
||||
.load::<User>(&mut self.connection)
|
||||
.unwrap_or_default()
|
||||
.first()
|
||||
{
|
||||
@@ -69,12 +69,12 @@ impl UserDao for SqliteUserDao {
|
||||
}
|
||||
}
|
||||
|
||||
fn user_exists(&self, user: &str) -> bool {
|
||||
fn user_exists(&mut self, user: &str) -> bool {
|
||||
use schema::users::dsl::*;
|
||||
|
||||
users
|
||||
.filter(username.eq(user))
|
||||
.load::<User>(&self.connection)
|
||||
.load::<User>(&mut self.connection)
|
||||
.unwrap_or_default()
|
||||
.first()
|
||||
.is_some()
|
||||
@@ -109,9 +109,9 @@ pub enum DbErrorKind {
|
||||
}
|
||||
|
||||
pub trait FavoriteDao: Sync + Send {
|
||||
fn add_favorite(&self, user_id: i32, favorite_path: &str) -> Result<usize, DbError>;
|
||||
fn remove_favorite(&self, user_id: i32, favorite_path: String);
|
||||
fn get_favorites(&self, user_id: i32) -> Result<Vec<Favorite>, DbError>;
|
||||
fn add_favorite(&mut self, user_id: i32, favorite_path: &str) -> Result<usize, DbError>;
|
||||
fn remove_favorite(&mut self, user_id: i32, favorite_path: String);
|
||||
fn get_favorites(&mut self, user_id: i32) -> Result<Vec<Favorite>, DbError>;
|
||||
}
|
||||
|
||||
pub struct SqliteFavoriteDao {
|
||||
@@ -127,15 +127,14 @@ impl SqliteFavoriteDao {
|
||||
}
|
||||
|
||||
impl FavoriteDao for SqliteFavoriteDao {
|
||||
fn add_favorite(&self, user_id: i32, favorite_path: &str) -> Result<usize, DbError> {
|
||||
fn add_favorite(&mut self, user_id: i32, favorite_path: &str) -> Result<usize, DbError> {
|
||||
use schema::favorites::dsl::*;
|
||||
|
||||
let connection = self.connection.lock().unwrap();
|
||||
let connection = connection.deref();
|
||||
let mut connection = self.connection.lock().expect("Unable to get FavoriteDao");
|
||||
|
||||
if favorites
|
||||
.filter(userid.eq(user_id).and(path.eq(&favorite_path)))
|
||||
.first::<Favorite>(connection)
|
||||
.first::<Favorite>(connection.deref_mut())
|
||||
.is_err()
|
||||
{
|
||||
diesel::insert_into(favorites)
|
||||
@@ -143,28 +142,28 @@ impl FavoriteDao for SqliteFavoriteDao {
|
||||
userid: &user_id,
|
||||
path: favorite_path,
|
||||
})
|
||||
.execute(connection)
|
||||
.execute(connection.deref_mut())
|
||||
.map_err(|_| DbError::new(DbErrorKind::InsertError))
|
||||
} else {
|
||||
Err(DbError::exists())
|
||||
}
|
||||
}
|
||||
|
||||
fn remove_favorite(&self, user_id: i32, favorite_path: String) {
|
||||
fn remove_favorite(&mut self, user_id: i32, favorite_path: String) {
|
||||
use schema::favorites::dsl::*;
|
||||
|
||||
diesel::delete(favorites)
|
||||
.filter(userid.eq(user_id).and(path.eq(favorite_path)))
|
||||
.execute(self.connection.lock().unwrap().deref())
|
||||
.execute(self.connection.lock().unwrap().deref_mut())
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn get_favorites(&self, user_id: i32) -> Result<Vec<Favorite>, DbError> {
|
||||
fn get_favorites(&mut self, user_id: i32) -> Result<Vec<Favorite>, DbError> {
|
||||
use schema::favorites::dsl::*;
|
||||
|
||||
favorites
|
||||
.filter(userid.eq(user_id))
|
||||
.load::<Favorite>(self.connection.lock().unwrap().deref())
|
||||
.load::<Favorite>(self.connection.lock().unwrap().deref_mut())
|
||||
.map_err(|_| DbError::new(DbErrorKind::QueryError))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user