Add GET /api/historique

This commit is contained in:
flyingscorpio@clevo 2023-01-08 22:50:37 +01:00
parent 783a83ef05
commit 56881725aa
4 changed files with 27 additions and 3 deletions

View file

@ -59,6 +59,13 @@ pub fn add_favorite(title_mirabelid: i32) -> Result<Title, DatabaseError> {
}
}
/// Reads the history table and returns History items.
pub fn get_history() -> Result<Vec<History>, DatabaseError> {
let connection = &mut crud::establish_connection()?;
crud::get_history(connection)
}
/// Inserts a history in the local database from a request.
pub fn save_request_to_history(request: &Request) -> Result<(), DatabaseError> {
let connection = &mut crud::establish_connection()?;
@ -73,7 +80,7 @@ pub fn save_request_to_history(request: &Request) -> Result<(), DatabaseError> {
Ok(())
}
/// Searches for Favorites with a 'titre' and returns the found Title instances
/// 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()?;

View file

@ -60,6 +60,14 @@ pub(super) fn create_title(
Ok(result)
}
/// Retrieve all items from the history table
pub(super) fn get_history(connection: &mut PgConnection) -> Result<Vec<History>, DatabaseError> {
let results = history::table
.load::<History>(connection)?;
Ok(results)
}
/// Searches the title table for a 'mirabelid', and returns a vector of Title items.
pub(super) fn search_title_from_mirabelid(
connection: &mut PgConnection,

View file

@ -156,7 +156,16 @@ fn handle_get_request(request: Request, mut sender: Mouth) {
}
Err(err) => sender.send_internal_error(format!("{}", err)),
},
"historique" => todo!(),
"historique" => match database::get_history() {
Ok(result) => {
if result.is_empty() {
sender.send_not_found(result);
} else {
sender.send_ok(result);
}
}
Err(err) => sender.send_internal_error(format!("{}", err)),
},
_ => sender.send_bad_request(""),
}
}

View file

@ -22,7 +22,7 @@ pub struct InsertFavorite {
}
/// History struct, mapped to the SQL history table.
#[derive(Queryable, Debug)]
#[derive(Queryable, Debug, Serialize)]
pub struct History {
pub id: i32,
pub method: String,