From f67c88c25975afa91e4e237c670f02178dca082e Mon Sep 17 00:00:00 2001 From: Pratik Tripathy Date: Wed, 2 Dec 2020 01:11:42 +0530 Subject: [PATCH] Get Orders working. Kindof --- readme.md | 2 +- src/main.rs | 2 +- src/models/order.rs | 60 ++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/readme.md b/readme.md index a5f4287..3309f3e 100644 --- a/readme.md +++ b/readme.md @@ -59,7 +59,7 @@ server is backed by PostgreSQL database. “orders”: [ { “id”: 1, - “email”: “​test@example.com​”, + “email”: “test@example.com”, “line_items”: [ { “quantity”: 5, diff --git a/src/main.rs b/src/main.rs index b75db13..9633f84 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,7 +31,7 @@ async fn main() -> std::io::Result<()> { env::var("HOST").expect("Host not set in .env file"), env::var("PORT").expect("Port not set in .env file")); info!("Starting Server"); - HttpServer::new(|| + HttpServer::new(move || App::new() .data(db_pool::get_connection()) .wrap(actix_web::middleware::Logger::default()) diff --git a/src/models/order.rs b/src/models/order.rs index b81f7f5..445d620 100644 --- a/src/models/order.rs +++ b/src/models/order.rs @@ -1,4 +1,4 @@ -use diesel::{insert_into, RunQueryDsl}; +use diesel::{ExpressionMethods, insert_into, QueryDsl, RunQueryDsl}; use serde::{Deserialize, Serialize}; use serde_json; @@ -22,6 +22,7 @@ pub struct NewOrder { pub customer_email: String, } + /// Stores selected (queried) `order_details` table row. /// Can also be used to insert data into `order_details` table #[derive(Serialize, Deserialize, Queryable, Insertable, Debug)] @@ -32,10 +33,61 @@ pub struct OrderDetail { pub quantity: i32, } +#[derive(Serialize, Deserialize, Queryable)] +pub struct AllOrders { + pub id: i32, + pub email: String, + pub quantity: i32, + pub productid: i32, + pub productname: String, +} + +#[derive(Serialize, Deserialize)] +pub struct AllOrdersFormatted { + pub orders: Vec +} + +#[derive(Serialize, Deserialize)] +pub struct EachOrderFormatted { + pub id: i32, + pub email: String, + pub line_items: Vec +} + +#[derive(Serialize, Deserialize)] +pub struct EachItemFormatted { + pub quantity: i32, + pub product: ProductFormatted +} + +#[derive(Serialize, Deserialize)] +pub struct ProductFormatted { + pub id: i32, + pub name: String +} + impl Order { - pub fn find_all() -> Result { - // let conn = db_pool::get_connection()?; - Ok(OrderDetail { quantity: 1, productid: 2, orderid: 3 }) + pub fn find_all() -> Result, ApiError> { + let conn = db_pool::get_connection()?; + use crate::schema::orders::dsl::{orderid, customer_email, created_at}; + use crate::schema::order_details::dsl::{quantity}; + use crate::schema::products::dsl::*; + + let orders = match orders::table.inner_join(order_details::table.inner_join(products)) + .select((orderid, customer_email, quantity, productid, productname)) + .order_by(created_at.desc()) + .load::(&conn) { + Ok(t) => t, + Err(e) => { + error!("Error fetching all orders: {}", e); + return Err(ApiError { + message: "Error fetching all orders".to_string(), + status_code: 420 + }) + } + }; + + Ok(orders) }