Add GET favoris
This commit is contained in:
parent
0f15c38cd3
commit
f00ccab096
2 changed files with 72 additions and 27 deletions
|
@ -7,7 +7,7 @@ mod errors;
|
|||
|
||||
use diesel::prelude::*;
|
||||
|
||||
use crate::models::{InsertFavorite, InsertTitle, Title};
|
||||
use crate::models::{Favorite, InsertFavorite, InsertTitle, Title};
|
||||
use crate::schema::title;
|
||||
use crate::schema::title::dsl::*;
|
||||
pub use errors::DatabaseError;
|
||||
|
@ -58,6 +58,27 @@ pub fn add_favorite(title_mirabelid: i32) -> Result<Title, DatabaseError> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Searches for Favorites with a 'titre' and returns the found Title instances
|
||||
pub fn search_favorites_from_titre(keyword: &str) -> Result<Vec<Title>, DatabaseError> {
|
||||
let connection = &mut crud::establish_connection()?;
|
||||
|
||||
// Find titles matching the keyword
|
||||
let matching_titles = crud::search_title_from_titre(connection, keyword)?;
|
||||
if matching_titles.is_empty() {
|
||||
return Ok(matching_titles);
|
||||
}
|
||||
|
||||
// Keep only the titles that have a favorite
|
||||
let mut favorite_titles: Vec<Title> = vec![];
|
||||
for matching_title in matching_titles {
|
||||
if !crud::search_favorite_from_mirabelid(connection, matching_title.mirabelid)?.is_empty() {
|
||||
favorite_titles.push(matching_title);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(favorite_titles)
|
||||
}
|
||||
|
||||
/// Searches for a Title with a 'titre' and returns the found Title instances.
|
||||
pub fn search_title_from_titre(keyword: &str) -> Result<Vec<Title>, DatabaseError> {
|
||||
let connection = &mut crud::establish_connection()?;
|
||||
|
|
76
src/ear.rs
76
src/ear.rs
|
@ -100,42 +100,66 @@ fn parse_uri(full_request: &str) -> Result<Request, ReqParseError> {
|
|||
/// Handles a given well formed request
|
||||
fn handle_request(request: Request, mut sender: Mouth) {
|
||||
match request.method.as_str() {
|
||||
"GET" => match request.section.as_str() {
|
||||
"titre" => match database::search_title_from_titre(&request.requested_data) {
|
||||
Ok(result) => {
|
||||
if result.len() == 0 {
|
||||
sender.send_data(Status::NotFound, serde_json::json!(result));
|
||||
} else {
|
||||
sender.send_data(Status::OK, serde_json::json!(result));
|
||||
"GET" => {
|
||||
match request.section.as_str() {
|
||||
"titre" => match database::search_title_from_titre(&request.requested_data) {
|
||||
Ok(result) => {
|
||||
if result.len() == 0 {
|
||||
sender.send_data(Status::NotFound, serde_json::json!(result));
|
||||
} else {
|
||||
sender.send_data(Status::OK, serde_json::json!(result));
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
sender.send_data(Status::InternalError, serde_json::json!(format!("{}", err)))
|
||||
}
|
||||
},
|
||||
"favoris" => todo!(),
|
||||
"historique" => todo!(),
|
||||
_ => sender.send_data(
|
||||
Status::BadRequest,
|
||||
serde_json::json!("La requête est mal formée, veuillez lire la documentation."),
|
||||
),
|
||||
},
|
||||
Err(err) => sender
|
||||
.send_data(Status::InternalError, serde_json::json!(format!("{}", err))),
|
||||
},
|
||||
"favoris" => match database::search_favorites_from_titre(&request.requested_data) {
|
||||
Ok(result) => {
|
||||
if result.len() == 0 {
|
||||
sender.send_data(Status::NotFound, serde_json::json!(result));
|
||||
} else {
|
||||
sender.send_data(Status::OK, serde_json::json!(result));
|
||||
}
|
||||
}
|
||||
Err(err) => sender
|
||||
.send_data(Status::InternalError, serde_json::json!(format!("{}", err))),
|
||||
},
|
||||
"historique" => todo!(),
|
||||
_ => sender.send_data(
|
||||
Status::BadRequest,
|
||||
serde_json::json!("La requête est mal formée, veuillez lire la documentation."),
|
||||
),
|
||||
}
|
||||
}
|
||||
"POST" => match request.section.as_str() {
|
||||
"favoris" => {
|
||||
let mirabelid: i32 = match request.requested_data.trim().parse() {
|
||||
Ok(val) => val,
|
||||
Err(_) => {
|
||||
sender.send_data(Status::BadRequest, serde_json::json!("La requête est mal formée, veuillez lire la documentation."));
|
||||
sender.send_data(
|
||||
Status::BadRequest,
|
||||
serde_json::json!(
|
||||
"La requête est mal formée, veuillez lire la documentation."
|
||||
),
|
||||
);
|
||||
return;
|
||||
},
|
||||
}
|
||||
};
|
||||
match database::add_favorite(mirabelid) {
|
||||
Ok(result) => sender.send_data(Status::OK, serde_json::json!(format!("Favori inséré avec succès : {}", result.titre))),
|
||||
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))),
|
||||
_ => sender.send_data(Status::InternalError, serde_json::json!(format!("{}", 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))),
|
||||
_ => sender.send_data(
|
||||
Status::InternalError,
|
||||
serde_json::json!(format!("{}", err)),
|
||||
),
|
||||
},
|
||||
}
|
||||
}
|
||||
_ => sender.send_data(
|
||||
|
|
Loading…
Reference in a new issue