This commit is contained in:
flyingscorpio@clevo 2023-01-07 10:46:39 +01:00
parent 925ac6310c
commit 4a09f7a3b4
2 changed files with 20 additions and 11 deletions

View file

@ -143,12 +143,14 @@ fn handle_post_request(request: Request, mut sender: Mouth) {
}
};
match database::add_favorite(mirabelid) {
Ok(result) => sender.send_ok(
format!("Favori inséré avec succès : {}", result.titre),
),
Ok(result) => {
sender.send_ok(format!("Favori inséré avec succès : {}", result.titre))
}
Err(err) => match err {
database::DatabaseError::NotFound => sender.send_not_found(format!("{}", err)),
database::DatabaseError::FoundDuplicate => sender.send_bad_request(format!("{}", err).as_str()),
database::DatabaseError::FoundDuplicate => {
sender.send_bad_request(format!("{}", err).as_str())
}
_ => sender.send_internal_error(format!("{}", err)),
},
}

View file

@ -43,7 +43,10 @@ impl<'a> Mouth<'a> {
}
/// Send OK response to a client.
pub fn send_ok<T>(&mut self, data: T) where T: Serialize {
pub fn send_ok<T>(&mut self, data: T)
where
T: Serialize,
{
self.send_data(Status::OK, serde_json::json!(data));
}
@ -58,19 +61,23 @@ impl<'a> Mouth<'a> {
} else {
data
};
self.send_data(
Status::BadRequest,
data,
);
self.send_data(Status::BadRequest, data);
}
/// Send InternalError response to a client.
pub fn send_internal_error<T>(&mut self, data: T) where T: Serialize {
pub fn send_internal_error<T>(&mut self, data: T)
where
T: Serialize,
{
self.send_data(Status::InternalError, serde_json::json!(data));
}
/// Send NotFound response to a client.
pub fn send_not_found<T>(&mut self, data: T) where T: Serialize {
pub fn send_not_found<T>(&mut self, data: T)
where
T: Serialize,
{
self.send_data(Status::NotFound, serde_json::json!(data));
}
}