import { Component, OnInit } from '@angular/core'; import { Cart } from 'src/app/model/cart.model'; import { CartService } from 'src/app/service/cart.service'; @Component({ selector: 'app-cart', templateUrl: './cart.component.html', styleUrls: ['./cart.component.css'] }) export class CartComponent implements OnInit { cartNo:number = 0; cartItems:Cart[]; uid:string = "1"; totalPrice:number = 0; constructor(private cartService:CartService) { } ngOnInit(): void { this.cartService.getCartByUser(this.uid).subscribe(data1 => { this.cartNo = data1.length; this.cartItems=data1; console.log(this.cartItems) this.cartItems.forEach((i,index) => { this.totalPrice = this.totalPrice + i.price; }) }) } increaseQty(cid:number,quantity:number,price:number) { console.log("Increase Quantity") let qty:number = quantity + 1; let amount:number = price*qty; let cart:Cart = { quantity : qty, price : amount } console.log(qty,amount); this.cartItems.forEach((i,index) => { if (i.cart_id == cid) { this.cartItems[index].quantity = qty; this.cartItems[index].price = amount; } }) this.cartService.editCartItem(cart,cid).subscribe(data => { }) this.cartItems.forEach((i,index) => { this.totalPrice = this.totalPrice + i.price; }) } decreaseQty(cid:number,quantity:number,price:number) { console.log("Decrease Quantity") let amount:number = price/quantity; let qty:number = quantity - 1; let cart:Cart = { quantity : qty, price : amount } console.log(quantity,qty,price,amount); this.cartItems.forEach((i,index) => { if (i.cart_id == cid) { this.cartItems[index].quantity = qty; this.cartItems[index].price = amount; } }) this.cartService.editCartItem(cart,cid).subscribe(data => { }) this.cartItems.forEach((i,index) => { this.totalPrice = this.totalPrice + i.price; }) } }