Skip to content

Commit

Permalink
optimise cart service
Browse files Browse the repository at this point in the history
  • Loading branch information
wnawa committed Jan 18, 2024
1 parent d2e1811 commit 73e2d0a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 41 deletions.
4 changes: 3 additions & 1 deletion src/app/cart-page/cart-page.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,10 @@ <h2 class="sidebar-title">Recent Posts</h2>
class="input-text qty text"
title="Qty"
value="{{ cartItem.quantity }}"
min="0"
min="1"
step="1"


/>
<input
type="button"
Expand Down
14 changes: 4 additions & 10 deletions src/app/cart-page/cart-page.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,20 @@ import { CommonModule } from '@angular/common';
export class CartPageComponent {
cart!: Cart;

// cartItemId=1;
constructor(private cartservice: CartService) {
this.setCart();
}

setCart() {
this.cart = this.cartservice.getCart();
}
addQuantitiy(cartItemId: number) {
this.cart = this.cartservice.addQuantitiy(cartItemId);
// updateQuantitiy(cartItemId: number,qty) {
// this.cart = this.cartservice.updateQuantitiy(cartItemId,qty);

}
// }
deleteCartItemById(cartItemId: number){
this.cart = this.cartservice.deleteCartItemById(cartItemId);

}
// addQuantitiy(cartItemId: number) {
// console.log(cartItemId);
// this.cart.items.find((cartItem) => {
// if (cartItem.id == cartItemId) cartItem.quantity++;
// });
// }

}
2 changes: 1 addition & 1 deletion src/app/productdetails/productdetails.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class ProductdetailsComponent {
}

addCartItem(product: any) {
this.cartservice.addToCartWithQuantity(product, this.quantity);
this.cartservice.addToCart(product, this.quantity);
}
slideConfigLogos = {
accessibility: true,
Expand Down
47 changes: 18 additions & 29 deletions src/app/services/cart/cart.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,39 @@ import CartItem from '../../shared/models/CartItem';
export class CartService {
private cart: Cart = new Cart();
constructor() {}
//Add new Product to cart
addToCart(product: Product) {
const existCartItem = this.cart.items.find(
(cartitem) => cartitem.product.id == product.id
);
existCartItem
? existCartItem.quantity++
: this.cart.items.push(new CartItem(product));
console.log(this.cart);
//Function READ Cart Items
getCart(): Cart {
return this.cart;
}
//Add new Product to cart with quantity for single product page
addToCartWithQuantity(product: Product,quantity:number) {

//Add to cart CREATE new Product or update quantity
addToCart(product: Product, quantity: number
=1) {
const existCartItem = this.cart.items.find(
(cartitem) => cartitem.product.id == product.id
);
existCartItem
? existCartItem.quantity+=quantity
: this.cart.items.push(this.createCartItemQuantity(product,quantity));
//quantity?this.cart.
console.log(this.cart);
? (existCartItem.quantity += quantity)
: this.cart.items.push(this.createCartItem(product, quantity));

}

// Creat cart item for single product page
createCartItemQuantity(product: Product,quantity:number) {
let cartItem= new CartItem(product);
cartItem.quantity=quantity;
// Create cart item for single product page
createCartItem(product: Product, quantity: number) {
let cartItem = new CartItem(product);
cartItem.quantity = quantity;
return cartItem;
}

//Function display Cart Items
getCart(): Cart {
return this.cart;
}

//update cart Quantity
addQuantitiy(cartItemId: number): Cart {
console.log(cartItemId);
//UPDATE cart Quantity
updateQuantitiy(cartItemId: number,Qty:number): Cart {
this.cart.items.find((cartItem) => {
if (cartItem.id == cartItemId) cartItem.quantity++;
if (cartItem.id == cartItemId) cartItem.quantity=Qty;
});
return this.cart;
}

//delete cart item
//DELETE cart item
deleteCartItemById(cartItemId: number): Cart {
var removeIndex = this.cart.items
.map((cartItem) => cartItem.id)
Expand Down

0 comments on commit 73e2d0a

Please sign in to comment.