aboutsummaryrefslogtreecommitdiff
path: root/src/handlers
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-12-23 16:20:49 +0000
committerAkshay <[email protected]>2020-12-23 16:20:49 +0000
commit7c65421328552b08e64df25e224fe9d54d363e6e (patch)
treeb80f67282252dda16917151c5af19f211fa0b597 /src/handlers
parent375fc45f310476a0a49fc48054f6563c8e02e2f6 (diff)
fix add to cart endpoint
Diffstat (limited to 'src/handlers')
-rw-r--r--src/handlers/cart_items.rs31
-rw-r--r--src/handlers/users.rs1
2 files changed, 15 insertions, 17 deletions
diff --git a/src/handlers/cart_items.rs b/src/handlers/cart_items.rs
index 2ad800b..25baaeb 100644
--- a/src/handlers/cart_items.rs
+++ b/src/handlers/cart_items.rs
@@ -9,17 +9,14 @@ use diesel::prelude::*;
9use log::{error, info}; 9use log::{error, info};
10use serde::Deserialize; 10use serde::Deserialize;
11 11
12#[derive(Deserialize, Debug)]
13pub struct AddToCart {
14 product_id: i32,
15}
16
17pub async fn add_to_cart( 12pub async fn add_to_cart(
18 cookie: Identity, 13 cookie: Identity,
19 item_details: web::Json<AddToCart>, 14 item_id: String,
20 pool: web::Data<TPool>, 15 pool: web::Data<TPool>,
21) -> impl Responder { 16) -> impl Responder {
22 info!("Add to cart hit: {:?}", item_details.product_id); 17 let item_details = item_id.parse::<i32>().unwrap_or(-1);
18 info!("Add to cart hit: {:?}", item_details);
19 info!("[cart] Current user: {:?}", cookie.identity());
23 let conn = pool.get().unwrap(); 20 let conn = pool.get().unwrap();
24 if let Some(uname) = cookie.identity() { 21 if let Some(uname) = cookie.identity() {
25 let selected_user = customer 22 let selected_user = customer
@@ -29,10 +26,14 @@ pub async fn add_to_cart(
29 .expect("Couldn't connect to DB"); 26 .expect("Couldn't connect to DB");
30 let new_cart_item = AddCartItem { 27 let new_cart_item = AddCartItem {
31 cart_id: selected_user.id, 28 cart_id: selected_user.id,
32 product_id: item_details.product_id, 29 product_id: item_details,
33 }; 30 };
31 info!(
32 "cart id: {:?}, product id {:?}",
33 selected_user.id, item_details
34 );
34 diesel::insert_into(cart_items) 35 diesel::insert_into(cart_items)
35 .values(new_cart_item) 36 .values((cart_id.eq(selected_user.id), product_id.eq(item_details)))
36 .execute(&conn) 37 .execute(&conn)
37 .expect("Coundn't connect to DB"); 38 .expect("Coundn't connect to DB");
38 HttpResponse::Ok().body("Inserted successfully!") 39 HttpResponse::Ok().body("Inserted successfully!")
@@ -43,17 +44,13 @@ pub async fn add_to_cart(
43 } 44 }
44} 45}
45 46
46#[derive(Deserialize, Debug)]
47pub struct RemoveFromCart {
48 product_id: i32,
49}
50
51pub async fn remove_from_cart( 47pub async fn remove_from_cart(
52 cookie: Identity, 48 cookie: Identity,
53 item_details: web::Json<RemoveFromCart>, 49 item_id: String,
54 pool: web::Data<TPool>, 50 pool: web::Data<TPool>,
55) -> impl Responder { 51) -> impl Responder {
56 info!("Remove from cart hit: {:?}", item_details.product_id); 52 info!("Remove from cart hit: {:?}", item_id);
53 let item_details = item_id.parse::<i32>().unwrap_or(-1);
57 let conn = pool.get().unwrap(); 54 let conn = pool.get().unwrap();
58 if let Some(uname) = cookie.identity() { 55 if let Some(uname) = cookie.identity() {
59 let selected_user = customer 56 let selected_user = customer
@@ -65,7 +62,7 @@ pub async fn remove_from_cart(
65 diesel::delete( 62 diesel::delete(
66 cart_items 63 cart_items
67 .filter(cart_id.eq(selected_user.id)) 64 .filter(cart_id.eq(selected_user.id))
68 .filter(product_id.eq(item_details.product_id)), 65 .filter(product_id.eq(item_details)),
69 ) 66 )
70 .execute(&conn) 67 .execute(&conn)
71 .expect("Coundn't connect to DB"); 68 .expect("Coundn't connect to DB");
diff --git a/src/handlers/users.rs b/src/handlers/users.rs
index 73fca84..24fb591 100644
--- a/src/handlers/users.rs
+++ b/src/handlers/users.rs
@@ -13,6 +13,7 @@ pub async fn new_user(
13 pool: web::Data<TPool>, 13 pool: web::Data<TPool>,
14 item: web::Json<NewCustomer>, 14 item: web::Json<NewCustomer>,
15) -> impl Responder { 15) -> impl Responder {
16 info!("Creating ... {:?}", item.username);
16 let conn = pool.get().unwrap(); 17 let conn = pool.get().unwrap();
17 let hashed_item = NewCustomer { 18 let hashed_item = NewCustomer {
18 password: hash(&item.password, DEFAULT_COST).unwrap(), 19 password: hash(&item.password, DEFAULT_COST).unwrap(),