diff options
Diffstat (limited to 'backend/src/handlers/transaction.rs')
-rw-r--r-- | backend/src/handlers/transaction.rs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/backend/src/handlers/transaction.rs b/backend/src/handlers/transaction.rs new file mode 100644 index 0000000..1e87312 --- /dev/null +++ b/backend/src/handlers/transaction.rs | |||
@@ -0,0 +1,74 @@ | |||
1 | use crate::models::{AddTransaction, CartItem, Customer, Product, Transaction}; | ||
2 | use crate::schema::cart_items::dsl::*; | ||
3 | use crate::schema::customer::dsl::*; | ||
4 | use crate::schema::product::dsl as prod; | ||
5 | use crate::schema::transaction::dsl::*; | ||
6 | use crate::TPool; | ||
7 | |||
8 | use actix_identity::Identity; | ||
9 | use actix_web::{web, HttpResponse, Responder}; | ||
10 | use diesel::prelude::*; | ||
11 | use log::{error, info}; | ||
12 | |||
13 | pub async fn checkout_cart( | ||
14 | pool: web::Data<TPool>, | ||
15 | pmt_kind: String, | ||
16 | cookie: Identity, | ||
17 | ) -> impl Responder { | ||
18 | let conn = pool.get().unwrap(); | ||
19 | info!("Checkout cart for user: {:?}", cookie.identity()); | ||
20 | if let Some(uname) = cookie.identity() { | ||
21 | let selected_user = customer | ||
22 | .filter(username.eq(&uname)) | ||
23 | .limit(1) | ||
24 | .first::<Customer>(&conn) | ||
25 | .expect("Couldn't connect to DB"); | ||
26 | let user_cart_items = cart_items | ||
27 | .filter(cart_id.eq(selected_user.id)) | ||
28 | .load::<CartItem>(&conn) | ||
29 | .expect("Couldn't connect to DB"); | ||
30 | let cart_total = user_cart_items.into_iter().fold(0., |acc, item| { | ||
31 | let item_price = prod::product | ||
32 | .filter(prod::id.eq(item.product_id)) | ||
33 | .limit(1) | ||
34 | .first::<Product>(&conn) | ||
35 | .unwrap() | ||
36 | .price; | ||
37 | acc + item.quantity.unwrap_or(1) as f32 * item_price | ||
38 | }); | ||
39 | let transaction_entry = AddTransaction { | ||
40 | customer_id: Some(selected_user.id), | ||
41 | amount: cart_total, | ||
42 | payment_type: pmt_kind, | ||
43 | }; | ||
44 | diesel::insert_into(transaction) | ||
45 | .values(transaction_entry) | ||
46 | .execute(&conn) | ||
47 | .expect("Coundn't connect to DB"); | ||
48 | return HttpResponse::Ok().body("Transaction performed successfully"); | ||
49 | } else { | ||
50 | return HttpResponse::Unauthorized().body("Login first"); | ||
51 | } | ||
52 | } | ||
53 | |||
54 | pub async fn list_transactions( | ||
55 | pool: web::Data<TPool>, | ||
56 | cookie: Identity, | ||
57 | ) -> impl Responder { | ||
58 | let conn = pool.get().unwrap(); | ||
59 | if let Some(uname) = cookie.identity() { | ||
60 | let selected_user = customer | ||
61 | .filter(username.eq(&uname)) | ||
62 | .limit(1) | ||
63 | .first::<Customer>(&conn) | ||
64 | .expect("Couldn't connect to DB"); | ||
65 | let user_transactions = transaction | ||
66 | .filter(customer_id.eq(selected_user.id)) | ||
67 | .load::<Transaction>(&conn) | ||
68 | .expect("Couldn't connect to DB"); | ||
69 | return HttpResponse::Ok().json(&user_transactions); | ||
70 | } else { | ||
71 | return HttpResponse::Unauthorized() | ||
72 | .body("Need to be logged in to add to cart!"); | ||
73 | } | ||
74 | } | ||