Split handle_request into two subfunctions for get and post
This commit is contained in:
parent
f00ccab096
commit
52b54bfb84
1 changed files with 77 additions and 67 deletions
144
src/ear.rs
144
src/ear.rs
|
@ -100,73 +100,83 @@ 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));
|
||||
}
|
||||
}
|
||||
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."
|
||||
),
|
||||
);
|
||||
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)),
|
||||
),
|
||||
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)),
|
||||
),
|
||||
},
|
||||
}
|
||||
}
|
||||
_ => sender.send_data(
|
||||
Status::BadRequest,
|
||||
serde_json::json!("La requête est mal formée, veuillez lire la documentation."),
|
||||
),
|
||||
},
|
||||
"GET" => handle_get_request(request, sender),
|
||||
"POST" => handle_post_request(request, sender),
|
||||
_ => sender.send_data(
|
||||
Status::BadRequest,
|
||||
serde_json::json!("La requête est mal formée, veuillez lire la documentation."),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// Handles a given GET request
|
||||
fn handle_get_request(request: Request, mut sender: Mouth) {
|
||||
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" => 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."),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// Handles a given POST request
|
||||
fn handle_post_request(request: Request, mut sender: Mouth) {
|
||||
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."
|
||||
),
|
||||
);
|
||||
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)),
|
||||
),
|
||||
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))),
|
||||
},
|
||||
}
|
||||
}
|
||||
_ => sender.send_data(
|
||||
Status::BadRequest,
|
||||
serde_json::json!("La requête est mal formée, veuillez lire la documentation."),
|
||||
|
|
Loading…
Reference in a new issue