Add inserting to history at every request
This commit is contained in:
parent
75d94d3d4f
commit
b5becee084
3 changed files with 38 additions and 5 deletions
|
@ -7,7 +7,8 @@ mod errors;
|
|||
|
||||
use diesel::prelude::*;
|
||||
|
||||
use crate::models::{Favorite, InsertFavorite, InsertTitle, Title};
|
||||
use crate::ear::Request;
|
||||
use crate::models::{Favorite, History, InsertFavorite, InsertHistory, InsertTitle, Title};
|
||||
use crate::schema::title;
|
||||
use crate::schema::title::dsl::*;
|
||||
pub use errors::DatabaseError;
|
||||
|
@ -58,6 +59,20 @@ pub fn add_favorite(title_mirabelid: i32) -> Result<Title, DatabaseError> {
|
|||
}
|
||||
}
|
||||
|
||||
/// 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()?;
|
||||
|
||||
let new_history = InsertHistory {
|
||||
method: &request.method,
|
||||
section: &request.section,
|
||||
query: &request.requested_data,
|
||||
};
|
||||
crud::create_history(connection, new_history)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 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()?;
|
||||
|
|
|
@ -6,9 +6,11 @@ use diesel::pg::PgConnection;
|
|||
use diesel::prelude::*;
|
||||
use dotenvy::dotenv;
|
||||
|
||||
use crate::models::{Favorite, InsertFavorite, InsertTitle, Title};
|
||||
use crate::models::{Favorite, History, InsertFavorite, InsertHistory, InsertTitle, Title};
|
||||
use crate::schema::favorite;
|
||||
use crate::schema::favorite::dsl::*;
|
||||
use crate::schema::history;
|
||||
use crate::schema::history::dsl::*;
|
||||
use crate::schema::title;
|
||||
use crate::schema::title::dsl::*;
|
||||
|
||||
|
@ -34,6 +36,18 @@ pub(super) fn create_favorite(
|
|||
Ok(result)
|
||||
}
|
||||
|
||||
/// Inserts a new history in the history table and returns the inserted item.
|
||||
pub(super) fn create_history(
|
||||
connection: &mut PgConnection,
|
||||
new_history: InsertHistory,
|
||||
) -> QueryResult<History> {
|
||||
let result = diesel::insert_into(history::table)
|
||||
.values(new_history)
|
||||
.get_result(connection)?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Inserts a new title in the title table and returns the inserted item.
|
||||
pub(super) fn create_title(
|
||||
connection: &mut PgConnection,
|
||||
|
|
10
src/ear.rs
10
src/ear.rs
|
@ -17,9 +17,9 @@ pub struct Ear {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct Request {
|
||||
method: String,
|
||||
section: String,
|
||||
requested_data: String,
|
||||
pub method: String,
|
||||
pub section: String,
|
||||
pub requested_data: String,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -70,6 +70,7 @@ impl Ear {
|
|||
/// Receives and parses a request from `receive_request` to dermine the function to call.
|
||||
/// We are expecting the request to be like 'GET /api/titre/jean'
|
||||
fn parse_uri(full_request: &str) -> Result<Request, ReqParseError> {
|
||||
// TODO: turn method to an enum, and handle cases explicitly instead of unwrapping
|
||||
let method: String = full_request.split_whitespace().nth(0).unwrap().to_string();
|
||||
let uri = full_request.split_whitespace().nth(1).unwrap();
|
||||
let uri = decode(uri).expect("UTF-8").to_string();
|
||||
|
@ -96,6 +97,9 @@ fn parse_uri(full_request: &str) -> Result<Request, ReqParseError> {
|
|||
|
||||
/// Handles a given well formed request
|
||||
fn handle_request(request: Request, mut sender: Mouth) {
|
||||
database::save_request_to_history(&request);
|
||||
|
||||
// TODO: turn method to an enum to handle cases explicitly
|
||||
match request.method.as_str() {
|
||||
"GET" => handle_get_request(request, sender),
|
||||
"POST" => handle_post_request(request, sender),
|
||||
|
|
Loading…
Reference in a new issue