Return Title when inserting favorite

This commit is contained in:
flyingscorpio@clevo 2023-01-06 17:31:31 +01:00
parent b8a0190d91
commit e5b28029c9
3 changed files with 11 additions and 9 deletions

View file

@ -35,20 +35,22 @@ pub fn add_all_titles<'a>(
}
/// Inserts a favorite in the local database, if a matching Title exists.
pub fn insert_favorite(title_mirabelid: i32) -> Result<(), DatabaseError> {
/// Returns the matching Title if successful, otherwise an error.
pub fn insert_favorite(title_mirabelid: i32) -> Result<Title, 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)
// search for a matching title, and return error if not found
let matching_title = crud::search_title_from_mirabelid(connection, title_mirabelid)?;
if matching_title.is_empty() {
return 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(())
crud::create_favorite(connection, new_favorite)?;
return Ok(matching_title[0].clone());
} else {
// else return an error
Err(DatabaseError::FoundDuplicate)

View file

@ -129,7 +129,7 @@ fn handle_request(request: Request, mut sender: Mouth) {
},
};
match database::insert_favorite(mirabelid) {
Ok(result) => sender.send_data(Status::OK, serde_json::json!(result)),
Ok(result) => sender.send_data(Status::OK, serde_json::json!(format!("Favori inséré avec succès : {}", result.titre))),
Err(err) => match err {
database::DatabaseError::NotFound => sender.send_data(Status::NotFound, serde_json::json!(format!("{}", err))),
database::DatabaseError::FoundDuplicate => sender.send_data(Status::BadRequest, serde_json::json!(format!("{}", err))),

View file

@ -21,7 +21,7 @@ pub struct InsertFavorite {
}
/// Title struct, mapped to the SQL title table.
#[derive(Queryable, Debug, Serialize)]
#[derive(Queryable, Debug, Serialize, Clone)]
pub struct Title {
pub id: i32,
pub mirabelid: i32,