diff options
author | Akshay <[email protected]> | 2020-12-16 14:31:13 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2020-12-16 14:31:13 +0000 |
commit | 5cfdb0eb26e4c3c2b087378c8b882c0eff0409af (patch) | |
tree | 4ee9b6bb7677a2e6d802261d8c23c99a119f6168 /src | |
parent | f25d37318e7873bf4f5ca58bcb850bed70ae77a6 (diff) |
add cart endpoints, rework testing files
Diffstat (limited to 'src')
-rw-r--r-- | src/handlers/cart_items.rs | 111 |
1 files changed, 111 insertions, 0 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 | } | ||