#[macro_use] extern crate rocket; #[macro_use] extern crate diesel; mod cors; mod kanta; mod pagination; use crate::diesel::QueryDsl; use crate::diesel::TextExpressionMethods; use crate::pagination::SortingAndPaging; use cors::CORS; use rocket::serde::json::Json as InOut; // use rocket::serde::msgpack::MsgPack as InOut; #[derive(FromForm)] struct ProductFilter { prefix: Option, sort_by: Option, sort_direction: Option, page_num: Option, page_size: Option, } #[get("/products?")] async fn filter_products( filter: ProductFilter, db: kanta::Db, ) -> Option>> { info!("prefix: {:?}", Some(filter.prefix.clone())); db.run(move |conn| { let mut query = kanta::products::table.into_boxed(); if let Some(i) = filter.prefix { query = query.filter(kanta::products::title.like(format!("%{}%", i))); } query .paginate(filter.page_num.unwrap_or(0)) .per_page(filter.page_size.unwrap_or(100)) .sort( filter.sort_by.unwrap_or("created_at".to_string()), filter.sort_direction.unwrap_or("".to_string()), ) .load_and_count_items::(conn) }) .await .map(InOut) .ok() } #[get("/")] async fn index() -> String { "Server is running".to_string() } #[launch] fn rocket() -> _ { kanta::mount_at( rocket::build() .attach(CORS) .mount("/", routes![index, filter_products]), "/", ) }