Get Orders working. Kindof
This commit is contained in:
@@ -59,7 +59,7 @@ server is backed by PostgreSQL database.
|
|||||||
“orders”: [
|
“orders”: [
|
||||||
{
|
{
|
||||||
“id”: 1,
|
“id”: 1,
|
||||||
“email”: “test@example.com”,
|
“email”: “test@example.com”,
|
||||||
“line_items”: [
|
“line_items”: [
|
||||||
{
|
{
|
||||||
“quantity”: 5,
|
“quantity”: 5,
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ async fn main() -> std::io::Result<()> {
|
|||||||
env::var("HOST").expect("Host not set in .env file"),
|
env::var("HOST").expect("Host not set in .env file"),
|
||||||
env::var("PORT").expect("Port not set in .env file"));
|
env::var("PORT").expect("Port not set in .env file"));
|
||||||
info!("Starting Server");
|
info!("Starting Server");
|
||||||
HttpServer::new(||
|
HttpServer::new(move ||
|
||||||
App::new()
|
App::new()
|
||||||
.data(db_pool::get_connection())
|
.data(db_pool::get_connection())
|
||||||
.wrap(actix_web::middleware::Logger::default())
|
.wrap(actix_web::middleware::Logger::default())
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use diesel::{insert_into, RunQueryDsl};
|
use diesel::{ExpressionMethods, insert_into, QueryDsl, RunQueryDsl};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json;
|
use serde_json;
|
||||||
|
|
||||||
@@ -22,6 +22,7 @@ pub struct NewOrder {
|
|||||||
pub customer_email: String,
|
pub customer_email: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Stores selected (queried) `order_details` table row.
|
/// Stores selected (queried) `order_details` table row.
|
||||||
/// Can also be used to insert data into `order_details` table
|
/// Can also be used to insert data into `order_details` table
|
||||||
#[derive(Serialize, Deserialize, Queryable, Insertable, Debug)]
|
#[derive(Serialize, Deserialize, Queryable, Insertable, Debug)]
|
||||||
@@ -32,10 +33,61 @@ pub struct OrderDetail {
|
|||||||
pub quantity: i32,
|
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<EachOrderFormatted>
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct EachOrderFormatted {
|
||||||
|
pub id: i32,
|
||||||
|
pub email: String,
|
||||||
|
pub line_items: Vec<EachItemFormatted>
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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 {
|
impl Order {
|
||||||
pub fn find_all() -> Result<OrderDetail, ApiError> {
|
pub fn find_all() -> Result<Vec<AllOrders>, ApiError> {
|
||||||
// let conn = db_pool::get_connection()?;
|
let conn = db_pool::get_connection()?;
|
||||||
Ok(OrderDetail { quantity: 1, productid: 2, orderid: 3 })
|
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::<AllOrders>(&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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user