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 ++++++++++++++++++++++++++++++++++++++++ tests/json_data/add_user.json | 6 --- tests/json_data/chair.json | 6 --- tests/json_data/dumb_sofa.json | 4 -- tests/json_data/smart_sofa.json | 6 --- tests/product/chair.json | 6 +++ tests/product/dumb_sofa.json | 4 ++ tests/product/smart_sofa.json | 6 +++ tests/requests.txt | 17 ++++++ tests/users/add_akshay.json | 8 +++ tests/users/add_user.json | 7 +++ 11 files changed, 159 insertions(+), 22 deletions(-) create mode 100644 src/handlers/cart_items.rs delete mode 100644 tests/json_data/add_user.json delete mode 100644 tests/json_data/chair.json delete mode 100644 tests/json_data/dumb_sofa.json delete mode 100644 tests/json_data/smart_sofa.json create mode 100644 tests/product/chair.json create mode 100644 tests/product/dumb_sofa.json create mode 100644 tests/product/smart_sofa.json create mode 100644 tests/requests.txt create mode 100644 tests/users/add_akshay.json create mode 100644 tests/users/add_user.json 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!"); + } +} diff --git a/tests/json_data/add_user.json b/tests/json_data/add_user.json deleted file mode 100644 index c7b2c15..0000000 --- a/tests/json_data/add_user.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "username": "Ramu Kaka", - "password": "ramu123", - "phone_number": "123454234", - "email_id": "ramu@kaka.com" -} diff --git a/tests/json_data/chair.json b/tests/json_data/chair.json deleted file mode 100644 index bb13c8b..0000000 --- a/tests/json_data/chair.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "name": "Urban Ladder Teak Chair", - "kind": "Chair", - "price": 3500, - "description": "Sleek, modern chair for offices and workplaces" -} diff --git a/tests/json_data/dumb_sofa.json b/tests/json_data/dumb_sofa.json deleted file mode 100644 index 2b09a58..0000000 --- a/tests/json_data/dumb_sofa.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "name": "Home Town Leather Sofa", - "price": 5400 -} diff --git a/tests/json_data/smart_sofa.json b/tests/json_data/smart_sofa.json deleted file mode 100644 index 5d7f7d4..0000000 --- a/tests/json_data/smart_sofa.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "name": "Home Town Leather Sofa", - "kind": "Sofa", - "description": "Comfortable, authentic leather, wooden frame sofa, for living rooms", - "price": 5400 -} diff --git a/tests/product/chair.json b/tests/product/chair.json new file mode 100644 index 0000000..bb13c8b --- /dev/null +++ b/tests/product/chair.json @@ -0,0 +1,6 @@ +{ + "name": "Urban Ladder Teak Chair", + "kind": "Chair", + "price": 3500, + "description": "Sleek, modern chair for offices and workplaces" +} diff --git a/tests/product/dumb_sofa.json b/tests/product/dumb_sofa.json new file mode 100644 index 0000000..2b09a58 --- /dev/null +++ b/tests/product/dumb_sofa.json @@ -0,0 +1,4 @@ +{ + "name": "Home Town Leather Sofa", + "price": 5400 +} diff --git a/tests/product/smart_sofa.json b/tests/product/smart_sofa.json new file mode 100644 index 0000000..5d7f7d4 --- /dev/null +++ b/tests/product/smart_sofa.json @@ -0,0 +1,6 @@ +{ + "name": "Home Town Leather Sofa", + "kind": "Sofa", + "description": "Comfortable, authentic leather, wooden frame sofa, for living rooms", + "price": 5400 +} 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 @@ +http POST :7878/user/login username=akshay password=password + +http POST :7878/user/login username=akshay password=nigga + +http POST :7878/user/change_password Cookie:user-login=mKT3PRPSKJp/AdsfPCXg3GICmQW2wViUwUKPpsXKp+70ug== old_password=nigga new_password=nigga + +http :7878/user/change_password username=akshay password=password + +http :7878/product/catalog + +http :7878/product/1 + +http POST :7878/cart/add Cookie:user-login=mKT3PRPSKJp/AdsfPCXg3GICmQW2wViUwUKPpsXKp+70ug== product_id:=1 + +http :7878/cart/items Cookie:user-login=mKT3PRPSKJp/AdsfPCXg3GICmQW2wViUwUKPpsXKp+70ug== + +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 @@ + +{ + "username": "Akshay", + "password": "akshay", + "phone_number": "123454234", + "email_id": "aks@hay.com", + "address": "Rv College of Engineering, Mysuru Road, 560087, Near Metro station" +} diff --git a/tests/users/add_user.json b/tests/users/add_user.json new file mode 100644 index 0000000..1f481e9 --- /dev/null +++ b/tests/users/add_user.json @@ -0,0 +1,7 @@ +{ + "username": "Ramu Kaka", + "password": "ramu123", + "phone_number": "123454234", + "email_id": "ramu@kaka.com", + "address": "RA-602, Purva Riviera" +} -- cgit v1.2.3