diff options
Diffstat (limited to 'src/handlers/cart_items.rs')
-rw-r--r-- | src/handlers/cart_items.rs | 107 |
1 files changed, 0 insertions, 107 deletions
diff --git a/src/handlers/cart_items.rs b/src/handlers/cart_items.rs deleted file mode 100644 index 25baaeb..0000000 --- a/src/handlers/cart_items.rs +++ /dev/null | |||
@@ -1,107 +0,0 @@ | |||
1 | use crate::models::{AddCartItem, CartItem, Customer, Product}; | ||
2 | use crate::schema::product::dsl as prod; | ||
3 | use crate::schema::{cart_items::dsl::*, customer::dsl::*}; | ||
4 | use crate::TPool; | ||
5 | |||
6 | use actix_identity::Identity; | ||
7 | use actix_web::{web, HttpResponse, Responder}; | ||
8 | use diesel::prelude::*; | ||
9 | use log::{error, info}; | ||
10 | use serde::Deserialize; | ||
11 | |||
12 | pub async fn add_to_cart( | ||
13 | cookie: Identity, | ||
14 | item_id: String, | ||
15 | pool: web::Data<TPool>, | ||
16 | ) -> impl Responder { | ||
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()); | ||
20 | let conn = pool.get().unwrap(); | ||
21 | if let Some(uname) = cookie.identity() { | ||
22 | let selected_user = customer | ||
23 | .filter(username.eq(&uname)) | ||
24 | .limit(1) | ||
25 | .first::<Customer>(&conn) | ||
26 | .expect("Couldn't connect to DB"); | ||
27 | let new_cart_item = AddCartItem { | ||
28 | cart_id: selected_user.id, | ||
29 | product_id: item_details, | ||
30 | }; | ||
31 | info!( | ||
32 | "cart id: {:?}, product id {:?}", | ||
33 | selected_user.id, item_details | ||
34 | ); | ||
35 | diesel::insert_into(cart_items) | ||
36 | .values((cart_id.eq(selected_user.id), product_id.eq(item_details))) | ||
37 | .execute(&conn) | ||
38 | .expect("Coundn't connect to DB"); | ||
39 | HttpResponse::Ok().body("Inserted successfully!") | ||
40 | } else { | ||
41 | error!("Unauthorized add to cart action!"); | ||
42 | return HttpResponse::Unauthorized() | ||
43 | .body("Need to be logged in to add to cart!"); | ||
44 | } | ||
45 | } | ||
46 | |||
47 | pub async fn remove_from_cart( | ||
48 | cookie: Identity, | ||
49 | item_id: String, | ||
50 | pool: web::Data<TPool>, | ||
51 | ) -> impl Responder { | ||
52 | info!("Remove from cart hit: {:?}", item_id); | ||
53 | let item_details = item_id.parse::<i32>().unwrap_or(-1); | ||
54 | let conn = pool.get().unwrap(); | ||
55 | if let Some(uname) = cookie.identity() { | ||
56 | let selected_user = customer | ||
57 | .filter(username.eq(&uname)) | ||
58 | .limit(1) | ||
59 | .first::<Customer>(&conn) | ||
60 | .expect("Couldn't connect to DB"); | ||
61 | |||
62 | diesel::delete( | ||
63 | cart_items | ||
64 | .filter(cart_id.eq(selected_user.id)) | ||
65 | .filter(product_id.eq(item_details)), | ||
66 | ) | ||
67 | .execute(&conn) | ||
68 | .expect("Coundn't connect to DB"); | ||
69 | HttpResponse::Ok().body("Removed successfully!") | ||
70 | } else { | ||
71 | error!("Unauthorized add to cart action!"); | ||
72 | return HttpResponse::Unauthorized() | ||
73 | .body("Need to be logged in to add to cart!"); | ||
74 | } | ||
75 | } | ||
76 | |||
77 | pub async fn get_user_cart_items( | ||
78 | cookie: Identity, | ||
79 | pool: web::Data<TPool>, | ||
80 | ) -> impl Responder { | ||
81 | let conn = pool.get().unwrap(); | ||
82 | if let Some(uname) = cookie.identity() { | ||
83 | let selected_user = customer | ||
84 | .filter(username.eq(&uname)) | ||
85 | .limit(1) | ||
86 | .first::<Customer>(&conn) | ||
87 | .expect("Couldn't connect to DB"); | ||
88 | let user_cart_items = cart_items | ||
89 | .filter(cart_id.eq(selected_user.id)) | ||
90 | .load::<CartItem>(&conn) | ||
91 | .expect("Couldn't connect to DB"); | ||
92 | let cart_products = user_cart_items | ||
93 | .into_iter() | ||
94 | .map(|item| { | ||
95 | prod::product | ||
96 | .filter(prod::id.eq(item.product_id)) | ||
97 | .limit(1) | ||
98 | .first::<Product>(&conn) | ||
99 | .expect("Couldn't connect to db") | ||
100 | }) | ||
101 | .collect::<Vec<_>>(); | ||
102 | return HttpResponse::Ok().json(&cart_products); | ||
103 | } else { | ||
104 | return HttpResponse::Unauthorized() | ||
105 | .body("Need to be logged in to add to cart!"); | ||
106 | } | ||
107 | } | ||