Permit adding a new favorite
This commit is contained in:
parent
820c848a99
commit
8509a32cbf
3 changed files with 68 additions and 4 deletions
|
@ -7,7 +7,7 @@ mod errors;
|
|||
|
||||
use diesel::prelude::*;
|
||||
|
||||
use crate::models::{InsertTitle, Title};
|
||||
use crate::models::{InsertFavorite, InsertTitle, Title};
|
||||
use crate::schema::title;
|
||||
use crate::schema::title::dsl::*;
|
||||
use errors::DatabaseError;
|
||||
|
@ -34,9 +34,31 @@ pub fn add_all_titles<'a>(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Inserts a favorite in the local database, if a matching Title exists.
|
||||
pub fn insert_favorite(title_mirabelid: i32) -> Result<(), DatabaseError> {
|
||||
let connection = &mut crud::establish_connection()?;
|
||||
|
||||
// search for a matching title
|
||||
if crud::search_title_from_mirabelid(connection, title_mirabelid)?.is_empty() {
|
||||
// no matching title, return an error
|
||||
Err(DatabaseError::NotFound)
|
||||
} else {
|
||||
// title exists: check if the favorite already exists and insert it
|
||||
if crud::search_favorite_from_mirabelid(connection, title_mirabelid)?.is_empty() {
|
||||
// no favorite found, insert it
|
||||
let new_favorite = InsertFavorite { title_mirabelid };
|
||||
crud::create_favorite(connection, new_favorite);
|
||||
Ok(())
|
||||
} else {
|
||||
// else return an error
|
||||
Err(DatabaseError::FoundDuplicate)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Searches for a titre with a keyword and returns the found Title instances.
|
||||
pub fn search_titre(keyword: &str) -> Result<Vec<Title>, DatabaseError> {
|
||||
let connection = &mut crud::establish_connection()?;
|
||||
|
||||
crud::search_titre(connection, keyword)
|
||||
crud::search_titre_from_keyword(connection, keyword)
|
||||
}
|
||||
|
|
|
@ -6,7 +6,9 @@ use diesel::pg::PgConnection;
|
|||
use diesel::prelude::*;
|
||||
use dotenvy::dotenv;
|
||||
|
||||
use crate::models::{InsertTitle, Title};
|
||||
use crate::models::{Favorite, InsertFavorite, InsertTitle, Title};
|
||||
use crate::schema::favorite;
|
||||
use crate::schema::favorite::dsl::*;
|
||||
use crate::schema::title;
|
||||
use crate::schema::title::dsl::*;
|
||||
|
||||
|
@ -20,6 +22,18 @@ pub(super) fn establish_connection() -> Result<PgConnection, DatabaseError> {
|
|||
Ok(PgConnection::establish(&database_url)?)
|
||||
}
|
||||
|
||||
/// Inserts a new favorite in the favorite table and returns the inserted item.
|
||||
pub(super) fn create_favorite(
|
||||
connection: &mut PgConnection,
|
||||
new_favorite: InsertFavorite,
|
||||
) -> QueryResult<Favorite> {
|
||||
let result = diesel::insert_into(favorite::table)
|
||||
.values(new_favorite)
|
||||
.get_result(connection)?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Inserts a new title in the title table and returns the inserted item.
|
||||
pub(super) fn create_title(
|
||||
connection: &mut PgConnection,
|
||||
|
@ -32,8 +46,32 @@ pub(super) fn create_title(
|
|||
Ok(result)
|
||||
}
|
||||
|
||||
/// Searches the title table for a 'mirabelid', and returns a vector of Title items.
|
||||
pub(super) fn search_title_from_mirabelid(
|
||||
connection: &mut PgConnection,
|
||||
search_mirabelid: i32,
|
||||
) -> Result<Vec<Title>, DatabaseError> {
|
||||
let results = title::table
|
||||
.filter(mirabelid.eq(search_mirabelid))
|
||||
.load::<Title>(connection)?;
|
||||
|
||||
Ok(results)
|
||||
}
|
||||
|
||||
/// Searches the favorite table for a 'mirabelid', and returns a vector of Favorite items.
|
||||
pub(super) fn search_favorite_from_mirabelid(
|
||||
connection: &mut PgConnection,
|
||||
search_mirabelid: i32,
|
||||
) -> Result<Vec<Favorite>, DatabaseError> {
|
||||
let results = favorite::table
|
||||
.filter(title_mirabelid.eq(search_mirabelid))
|
||||
.load::<Favorite>(connection)?;
|
||||
|
||||
Ok(results)
|
||||
}
|
||||
|
||||
/// Searches the title table for a 'titre', and returns a vector of Title items.
|
||||
pub(super) fn search_titre(
|
||||
pub(super) fn search_titre_from_keyword(
|
||||
connection: &mut PgConnection,
|
||||
keyword: &str,
|
||||
) -> Result<Vec<Title>, DatabaseError> {
|
||||
|
|
|
@ -10,6 +10,8 @@ pub enum DatabaseError {
|
|||
DieselConnectionError(diesel::ConnectionError),
|
||||
DieselResultError(diesel::result::Error),
|
||||
EnvVarError(env::VarError),
|
||||
NotFound,
|
||||
FoundDuplicate,
|
||||
}
|
||||
|
||||
/// The Display trait is needed to print these errors.
|
||||
|
@ -19,6 +21,8 @@ impl Display for DatabaseError {
|
|||
DatabaseError::DieselConnectionError(err) => write!(f, "{}", err),
|
||||
DatabaseError::DieselResultError(err) => write!(f, "{}", err),
|
||||
DatabaseError::EnvVarError(err) => write!(f, "{}", err),
|
||||
DatabaseError::NotFound => write!(f, "Item was not found"),
|
||||
DatabaseError::FoundDuplicate => write!(f, "Item is already present"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue