diff options
-rw-r--r-- | src/handlers/cart_items.rs | 111 | ||||
-rw-r--r-- | tests/product/chair.json (renamed from tests/json_data/chair.json) | 0 | ||||
-rw-r--r-- | tests/product/dumb_sofa.json (renamed from tests/json_data/dumb_sofa.json) | 0 | ||||
-rw-r--r-- | tests/product/smart_sofa.json (renamed from tests/json_data/smart_sofa.json) | 0 | ||||
-rw-r--r-- | tests/requests.txt | 17 | ||||
-rw-r--r-- | tests/users/add_akshay.json | 8 | ||||
-rw-r--r-- | tests/users/add_user.json (renamed from tests/json_data/add_user.json) | 3 |
7 files changed, 138 insertions, 1 deletions
diff --git a/src/handlers/cart_items.rs b/src/handlers/cart_items.rs new file mode 100644 index 0000000..62cab75 --- /dev/null +++ b/src/handlers/cart_items.rs | |||
@@ -0,0 +1,111 @@ | |||
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 | #[derive(Deserialize, Debug)] | ||
13 | pub struct AddToCart { | ||
14 | product_id: i32, | ||
15 | } | ||
16 | |||
17 | pub async fn add_to_cart( | ||
18 | cookie: Identity, | ||
19 | item_details: web::Json<AddToCart>, | ||
20 | pool: web::Data<TPool>, | ||
21 | ) -> impl Responder { | ||
22 | info!("Add to cart hit: {:?}", item_details.product_id); | ||
23 | let conn = pool.get().unwrap(); | ||
24 | if let Some(uname) = cookie.identity() { | ||
25 | let selected_user = customer | ||
26 | .filter(username.eq(&uname)) | ||
27 | .limit(1) | ||
28 | .first::<Customer>(&conn) | ||
29 | .expect("Couldn't connect to DB"); | ||
30 | let new_cart_item = AddCartItem { | ||
31 | cart_id: selected_user.id, | ||
32 | product_id: item_details.product_id, | ||
33 | }; | ||
34 | diesel::insert_into(cart_items) | ||
35 | .values(new_cart_item) | ||
36 | .execute(&conn) | ||
37 | .expect("Coundn't connect to DB"); | ||
38 | HttpResponse::Ok().body("Inserted successfully!") | ||
39 | } else { | ||
40 | error!("Unauthorized add to cart action!"); | ||
41 | return HttpResponse::Unauthorized() | ||
42 | .body("Need to be logged in to add to cart!"); | ||
43 | } | ||
44 | } | ||
45 | |||
46 | #[derive(Deserialize, Debug)] | ||
47 | pub struct RemoveFromCart { | ||
48 | product_id: i32, | ||
49 | } | ||
50 | |||
51 | pub async fn remove_from_cart( | ||
52 | cookie: Identity, | ||
53 | item_details: web::Json<RemoveFromCart>, | ||
54 | pool: web::Data<TPool>, | ||
55 | ) -> impl Responder { | ||
56 | info!("Remove from cart hit: {:?}", item_details.product_id); | ||
57 | let conn = pool.get().unwrap(); | ||
58 | if let Some(uname) = cookie.identity() { | ||
59 | let selected_user = customer | ||
60 | .filter(username.eq(&uname)) | ||
61 | .limit(1) | ||
62 | .first::<Customer>(&conn) | ||
63 | .expect("Couldn't connect to DB"); | ||
64 | |||
65 | diesel::delete( | ||
66 | cart_items | ||
67 | .filter(cart_id.eq(selected_user.id)) | ||
68 | .filter(product_id.eq(item_details.product_id)), | ||
69 | ) | ||
70 | .execute(&conn) | ||
71 | .expect("Coundn't connect to DB"); | ||
72 | HttpResponse::Ok().body("Removed successfully!") | ||
73 | } else { | ||
74 | error!("Unauthorized add to cart action!"); | ||
75 | return HttpResponse::Unauthorized() | ||
76 | .body("Need to be logged in to add to cart!"); | ||
77 | } | ||
78 | } | ||
79 | |||
80 | pub async fn get_user_cart_items( | ||
81 | cookie: Identity, | ||
82 | pool: web::Data<TPool>, | ||
83 | ) -> impl Responder { | ||
84 | let conn = pool.get().unwrap(); | ||
85 | if let Some(uname) = cookie.identity() { | ||
86 | let selected_user = customer | ||
87 | .filter(username.eq(&uname)) | ||
88 | .limit(1) | ||
89 | .first::<Customer>(&conn) | ||
90 | .expect("Couldn't connect to DB"); | ||
91 | let user_cart_items = cart_items | ||
92 | .filter(cart_id.eq(selected_user.id)) | ||
93 | .load::<CartItem>(&conn) | ||
94 | .expect("Couldn't connect to DB"); | ||
95 | let cart_products = user_cart_items | ||
96 | .into_iter() | ||
97 | .map(|item| { | ||
98 | prod::product | ||
99 | .filter(prod::id.eq(item.product_id)) | ||
100 | .limit(1) | ||
101 | .first::<Product>(&conn) | ||
102 | .expect("Couldn't connect to db") | ||
103 | }) | ||
104 | .collect::<Vec<_>>(); | ||
105 | return HttpResponse::Ok() | ||
106 | .body(serde_json::to_string(&cart_products).unwrap()); | ||
107 | } else { | ||
108 | return HttpResponse::Unauthorized() | ||
109 | .body("Need to be logged in to add to cart!"); | ||
110 | } | ||
111 | } | ||
diff --git a/tests/json_data/chair.json b/tests/product/chair.json index bb13c8b..bb13c8b 100644 --- a/tests/json_data/chair.json +++ b/tests/product/chair.json | |||
diff --git a/tests/json_data/dumb_sofa.json b/tests/product/dumb_sofa.json index 2b09a58..2b09a58 100644 --- a/tests/json_data/dumb_sofa.json +++ b/tests/product/dumb_sofa.json | |||
diff --git a/tests/json_data/smart_sofa.json b/tests/product/smart_sofa.json index 5d7f7d4..5d7f7d4 100644 --- a/tests/json_data/smart_sofa.json +++ b/tests/product/smart_sofa.json | |||
diff --git a/tests/requests.txt b/tests/requests.txt new file mode 100644 index 0000000..7e38905 --- /dev/null +++ b/tests/requests.txt | |||
@@ -0,0 +1,17 @@ | |||
1 | http POST :7878/user/login username=akshay password=password | ||
2 | |||
3 | http POST :7878/user/login username=akshay password=nigga | ||
4 | |||
5 | http POST :7878/user/change_password Cookie:user-login=mKT3PRPSKJp/AdsfPCXg3GICmQW2wViUwUKPpsXKp+70ug== old_password=nigga new_password=nigga | ||
6 | |||
7 | http :7878/user/change_password username=akshay password=password | ||
8 | |||
9 | http :7878/product/catalog | ||
10 | |||
11 | http :7878/product/1 | ||
12 | |||
13 | http POST :7878/cart/add Cookie:user-login=mKT3PRPSKJp/AdsfPCXg3GICmQW2wViUwUKPpsXKp+70ug== product_id:=1 | ||
14 | |||
15 | http :7878/cart/items Cookie:user-login=mKT3PRPSKJp/AdsfPCXg3GICmQW2wViUwUKPpsXKp+70ug== | ||
16 | |||
17 | http POST :7878/cart/remove Cookie: product_id:=1 | ||
diff --git a/tests/users/add_akshay.json b/tests/users/add_akshay.json new file mode 100644 index 0000000..1d6c405 --- /dev/null +++ b/tests/users/add_akshay.json | |||
@@ -0,0 +1,8 @@ | |||
1 | |||
2 | { | ||
3 | "username": "Akshay", | ||
4 | "password": "akshay", | ||
5 | "phone_number": "123454234", | ||
6 | "email_id": "[email protected]", | ||
7 | "address": "Rv College of Engineering, Mysuru Road, 560087, Near Metro station" | ||
8 | } | ||
diff --git a/tests/json_data/add_user.json b/tests/users/add_user.json index c7b2c15..1f481e9 100644 --- a/tests/json_data/add_user.json +++ b/tests/users/add_user.json | |||
@@ -2,5 +2,6 @@ | |||
2 | "username": "Ramu Kaka", | 2 | "username": "Ramu Kaka", |
3 | "password": "ramu123", | 3 | "password": "ramu123", |
4 | "phone_number": "123454234", | 4 | "phone_number": "123454234", |
5 | "email_id": "[email protected]" | 5 | "email_id": "[email protected]", |
6 | "address": "RA-602, Purva Riviera" | ||
6 | } | 7 | } |