added menu search

This commit is contained in:
Colin McKechney
2023-04-30 21:31:04 -04:00
parent 8ebf0c162c
commit 7c52c2dd0e
3 changed files with 66 additions and 0 deletions

61
backend/src/api/menu.rs Normal file
View File

@@ -0,0 +1,61 @@
use oracle::{Result, Connection};
use serde::{Serialize, Deserialize};
use log::{info, error};
use actix_web::{self, Responder, web::Json, HttpResponse};
use crate::config::{ORACLE_USER, ORACLE_PASS, ORACLE_CON_STR};
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct SearchTerm{
search_term: String
}
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct SearchResults {
results: Vec<SearchResult>
}
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct SearchResult {
item_name: Option<String>,
item_id: u32,
eatery_id: Option<u32>,
serving_size: Option<String>
}
pub async menu_search(term: Json<SearchTerm>) -> Json<SearchResults> {
let term = term.into_inner();
match fuzzy_search(term) {
Ok(result) => Json(result),
Err(e) => {
error!("failed to search for {}", term);
Json(vec![])
}
}
}
fn fuzzy_search(term: &str) -> Result<SearchResults> {
let conn = Connection::connect(ORACLE_USER,ORACLE_PASS,ORACLE_CON_STR)?;
let stmt = conn.statement("select * from menu_item where item_name like :term").build()?;
let rows = stmt.query_named(&[("term", &term))?;
let rows_vec = SearchResults::default();
for row_result in rows{
let row = row_result?;
rows_vec.results.push( SearchResult {
item_name: row.get(0).unwrap_or(None),
item_id: row.get(1)?,
eatery_id: row.get(2).unwrap_or(None),
serving_size: row.get(3).unwrap_or(None)
};
}
Ok(rows_vec)
}

View File

@@ -2,4 +2,5 @@ pub mod user;
pub mod eatery; pub mod eatery;
pub mod plan; pub mod plan;
pub mod week; pub mod week;
pub mod menu;

View File

@@ -67,6 +67,10 @@ async fn main() -> std::io::Result<()> {
web::resource("/week_meals") web::resource("/week_meals")
.route(web::post().to(api::week::week_meals)) .route(web::post().to(api::week::week_meals))
) )
.service(
web::resource("/week_progress/{net_id}")
.route(web::get().to(api::week::week_lookup))
)
.route("/", web::get().to(api_index)) .route("/", web::get().to(api_index))
) )
.route("/", web::get().to(index)) .route("/", web::get().to(index))