aboutsummaryrefslogtreecommitdiff
path: root/backend/src/handlers/cart_items.rs
diff options
context:
space:
mode:
Diffstat (limited to 'backend/src/handlers/cart_items.rs')
-rw-r--r--backend/src/handlers/cart_items.rs131
1 files changed, 114 insertions, 17 deletions
diff --git a/backend/src/handlers/cart_items.rs b/backend/src/handlers/cart_items.rs
index 25baaeb..e17f4c4 100644
--- a/backend/src/handlers/cart_items.rs
+++ b/backend/src/handlers/cart_items.rs
@@ -7,7 +7,7 @@ use actix_identity::Identity;
7use actix_web::{web, HttpResponse, Responder}; 7use actix_web::{web, HttpResponse, Responder};
8use diesel::prelude::*; 8use diesel::prelude::*;
9use log::{error, info}; 9use log::{error, info};
10use serde::Deserialize; 10use serde::Serialize;
11 11
12pub async fn add_to_cart( 12pub async fn add_to_cart(
13 cookie: Identity, 13 cookie: Identity,
@@ -27,16 +27,41 @@ pub async fn add_to_cart(
27 let new_cart_item = AddCartItem { 27 let new_cart_item = AddCartItem {
28 cart_id: selected_user.id, 28 cart_id: selected_user.id,
29 product_id: item_details, 29 product_id: item_details,
30 quantity: Some(1),
30 }; 31 };
31 info!( 32 info!(
32 "cart id: {:?}, product id {:?}", 33 "cart id: {:?}, product id {:?}",
33 selected_user.id, item_details 34 selected_user.id, item_details
34 ); 35 );
35 diesel::insert_into(cart_items) 36 let current_entry = cart_items
36 .values((cart_id.eq(selected_user.id), product_id.eq(item_details))) 37 .filter(cart_id.eq(selected_user.id))
37 .execute(&conn) 38 .filter(product_id.eq(item_details))
38 .expect("Coundn't connect to DB"); 39 .limit(1)
39 HttpResponse::Ok().body("Inserted successfully!") 40 .first::<CartItem>(&conn);
41 match current_entry {
42 Ok(v) => {
43 info!("Item already present in cart, increasing quantity.");
44 let old_quantity = v.quantity.unwrap_or(1);
45 diesel::update(
46 cart_items
47 .filter(cart_id.eq(selected_user.id))
48 .filter(product_id.eq(item_details)),
49 )
50 .set(quantity.eq(old_quantity + 1))
51 .execute(&conn)
52 .unwrap();
53 return HttpResponse::Ok()
54 .body("Updated quantity successfully!");
55 }
56 Err(_) => {
57 info!("Item not present, adding to cart.");
58 diesel::insert_into(cart_items)
59 .values(new_cart_item)
60 .execute(&conn)
61 .expect("Couldn't connect to DB");
62 HttpResponse::Ok().body("Inserted successfully!")
63 }
64 }
40 } else { 65 } else {
41 error!("Unauthorized add to cart action!"); 66 error!("Unauthorized add to cart action!");
42 return HttpResponse::Unauthorized() 67 return HttpResponse::Unauthorized()
@@ -58,15 +83,44 @@ pub async fn remove_from_cart(
58 .limit(1) 83 .limit(1)
59 .first::<Customer>(&conn) 84 .first::<Customer>(&conn)
60 .expect("Couldn't connect to DB"); 85 .expect("Couldn't connect to DB");
61 86 let current_entry = cart_items
62 diesel::delete( 87 .filter(cart_id.eq(selected_user.id))
63 cart_items 88 .filter(product_id.eq(item_details))
64 .filter(cart_id.eq(selected_user.id)) 89 .limit(1)
65 .filter(product_id.eq(item_details)), 90 .first::<CartItem>(&conn);
66 ) 91 match current_entry {
67 .execute(&conn) 92 Ok(v) => {
68 .expect("Coundn't connect to DB"); 93 info!("Item already present in cart, increasing quantity.");
69 HttpResponse::Ok().body("Removed successfully!") 94 let old_quantity = v.quantity.unwrap_or(1);
95 if old_quantity == 1 {
96 diesel::delete(
97 cart_items
98 .filter(cart_id.eq(selected_user.id))
99 .filter(product_id.eq(item_details)),
100 )
101 .execute(&conn)
102 .expect("Coundn't connect to DB");
103 } else {
104 diesel::update(
105 cart_items
106 .filter(cart_id.eq(selected_user.id))
107 .filter(product_id.eq(item_details)),
108 )
109 .set(quantity.eq(old_quantity - 1))
110 .execute(&conn)
111 .unwrap();
112 return HttpResponse::Ok()
113 .body("Updated quantity successfully!");
114 }
115 return HttpResponse::Ok()
116 .body("Updated quantity successfully!");
117 }
118 Err(_) => {
119 info!("Item not present.");
120 return HttpResponse::InternalServerError()
121 .body("Item not found!");
122 }
123 }
70 } else { 124 } else {
71 error!("Unauthorized add to cart action!"); 125 error!("Unauthorized add to cart action!");
72 return HttpResponse::Unauthorized() 126 return HttpResponse::Unauthorized()
@@ -74,6 +128,12 @@ pub async fn remove_from_cart(
74 } 128 }
75} 129}
76 130
131#[derive(Serialize)]
132struct UserCartItem {
133 product_item: Product,
134 quantity: i32,
135}
136
77pub async fn get_user_cart_items( 137pub async fn get_user_cart_items(
78 cookie: Identity, 138 cookie: Identity,
79 pool: web::Data<TPool>, 139 pool: web::Data<TPool>,
@@ -92,11 +152,15 @@ pub async fn get_user_cart_items(
92 let cart_products = user_cart_items 152 let cart_products = user_cart_items
93 .into_iter() 153 .into_iter()
94 .map(|item| { 154 .map(|item| {
95 prod::product 155 let p = prod::product
96 .filter(prod::id.eq(item.product_id)) 156 .filter(prod::id.eq(item.product_id))
97 .limit(1) 157 .limit(1)
98 .first::<Product>(&conn) 158 .first::<Product>(&conn)
99 .expect("Couldn't connect to db") 159 .expect("Couldn't connect to db");
160 UserCartItem {
161 product_item: p,
162 quantity: item.quantity.unwrap_or(1),
163 }
100 }) 164 })
101 .collect::<Vec<_>>(); 165 .collect::<Vec<_>>();
102 return HttpResponse::Ok().json(&cart_products); 166 return HttpResponse::Ok().json(&cart_products);
@@ -105,3 +169,36 @@ pub async fn get_user_cart_items(
105 .body("Need to be logged in to add to cart!"); 169 .body("Need to be logged in to add to cart!");
106 } 170 }
107} 171}
172
173pub async fn get_user_cart_total(
174 cookie: Identity,
175 pool: web::Data<TPool>,
176) -> impl Responder {
177 let conn = pool.get().unwrap();
178 if let Some(uname) = cookie.identity() {
179 let selected_user = customer
180 .filter(username.eq(&uname))
181 .limit(1)
182 .first::<Customer>(&conn)
183 .expect("Couldn't connect to DB");
184 let user_cart_items = cart_items
185 .filter(cart_id.eq(selected_user.id))
186 .load::<CartItem>(&conn)
187 .expect("Couldn't connect to DB");
188 let cart_total: f32 = user_cart_items
189 .into_iter()
190 .map(|item| {
191 let p = prod::product
192 .filter(prod::id.eq(item.product_id))
193 .limit(1)
194 .first::<Product>(&conn)
195 .expect("Couldn't connect to db");
196 return p.price * item.quantity.unwrap_or(1) as f32;
197 })
198 .sum();
199 return HttpResponse::Ok().json(&cart_total);
200 } else {
201 return HttpResponse::Unauthorized()
202 .body("Need to be logged in to add to cart!");
203 }
204}