From 5cfdb0eb26e4c3c2b087378c8b882c0eff0409af Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 16 Dec 2020 20:01:13 +0530 Subject: add cart endpoints, rework testing files --- src/handlers/cart_items.rs | 111 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 src/handlers/cart_items.rs (limited to 'src/handlers') 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 @@ +use crate::models::{AddCartItem, CartItem, Customer, Product}; +use crate::schema::product::dsl as prod; +use crate::schema::{cart_items::dsl::*, customer::dsl::*}; +use crate::TPool; + +use actix_identity::Identity; +use actix_web::{web, HttpResponse, Responder}; +use diesel::prelude::*; +use log::{error, info}; +use serde::Deserialize; + +#[derive(Deserialize, Debug)] +pub struct AddToCart { + product_id: i32, +} + +pub async fn add_to_cart( + cookie: Identity, + item_details: web::Json, + pool: web::Data, +) -> impl Responder { + info!("Add to cart hit: {:?}", item_details.product_id); + let conn = pool.get().unwrap(); + if let Some(uname) = cookie.identity() { + let selected_user = customer + .filter(username.eq(&uname)) + .limit(1) + .first::(&conn) + .expect("Couldn't connect to DB"); + let new_cart_item = AddCartItem { + cart_id: selected_user.id, + product_id: item_details.product_id, + }; + diesel::insert_into(cart_items) + .values(new_cart_item) + .execute(&conn) + .expect("Coundn't connect to DB"); + HttpResponse::Ok().body("Inserted successfully!") + } else { + error!("Unauthorized add to cart action!"); + return HttpResponse::Unauthorized() + .body("Need to be logged in to add to cart!"); + } +} + +#[derive(Deserialize, Debug)] +pub struct RemoveFromCart { + product_id: i32, +} + +pub async fn remove_from_cart( + cookie: Identity, + item_details: web::Json, + pool: web::Data, +) -> impl Responder { + info!("Remove from cart hit: {:?}", item_details.product_id); + let conn = pool.get().unwrap(); + if let Some(uname) = cookie.identity() { + let selected_user = customer + .filter(username.eq(&uname)) + .limit(1) + .first::(&conn) + .expect("Couldn't connect to DB"); + + diesel::delete( + cart_items + .filter(cart_id.eq(selected_user.id)) + .filter(product_id.eq(item_details.product_id)), + ) + .execute(&conn) + .expect("Coundn't connect to DB"); + HttpResponse::Ok().body("Removed successfully!") + } else { + error!("Unauthorized add to cart action!"); + return HttpResponse::Unauthorized() + .body("Need to be logged in to add to cart!"); + } +} + +pub async fn get_user_cart_items( + cookie: Identity, + pool: web::Data, +) -> impl Responder { + let conn = pool.get().unwrap(); + if let Some(uname) = cookie.identity() { + let selected_user = customer + .filter(username.eq(&uname)) + .limit(1) + .first::(&conn) + .expect("Couldn't connect to DB"); + let user_cart_items = cart_items + .filter(cart_id.eq(selected_user.id)) + .load::(&conn) + .expect("Couldn't connect to DB"); + let cart_products = user_cart_items + .into_iter() + .map(|item| { + prod::product + .filter(prod::id.eq(item.product_id)) + .limit(1) + .first::(&conn) + .expect("Couldn't connect to db") + }) + .collect::>(); + return HttpResponse::Ok() + .body(serde_json::to_string(&cart_products).unwrap()); + } else { + return HttpResponse::Unauthorized() + .body("Need to be logged in to add to cart!"); + } +} -- cgit v1.2.3