You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
3.7 KiB
136 lines
3.7 KiB
import { HttpClient } from '@angular/common/http';
|
|
import { Injectable } from '@angular/core';
|
|
import { BehaviorSubject, Observable } from 'rxjs';
|
|
import { Cartdto } from '../model/Cartdto';
|
|
import { CartItem } from '../model/CartItem';
|
|
import { Orderdto } from '../model/Orderdto';
|
|
import { Products } from '../model/Products';
|
|
import { User } from '../model/User';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class CartService {
|
|
|
|
public cartItemList : any=[];
|
|
cartItem!:CartItem;
|
|
public cartdto! : Cartdto[];
|
|
public orderdto! : Orderdto[];
|
|
public productList = new BehaviorSubject<any>([]);
|
|
order = new BehaviorSubject<Orderdto>({});
|
|
num=new BehaviorSubject<number>(0);
|
|
num1=new BehaviorSubject<boolean>(false);
|
|
|
|
total=new BehaviorSubject<number>(0);
|
|
|
|
|
|
path : string = 'http://localhost:8001/api1';
|
|
|
|
constructor(private httpClient:HttpClient) { }
|
|
|
|
public postCart(uid:number,pid:number):Observable<CartItem[]>{
|
|
let get_api=this.path + '/cart/'+uid+'/'+pid;
|
|
this.cartItem= {};
|
|
return this.httpClient.post<CartItem[]>(get_api,this.cartItem);
|
|
}
|
|
|
|
public getCart(uid:number):Observable<Cartdto[]>{
|
|
let get_api=this.path + '/cart/'+uid;
|
|
return this.httpClient.get<Cartdto[]>(get_api);
|
|
}
|
|
|
|
public getCartdto(uid:number):Observable<Cartdto[]>{
|
|
let get_api=this.path + '/cartdto/'+uid;
|
|
this.cartItem= {};
|
|
return this.httpClient.get<Cartdto[]>(get_api);
|
|
}
|
|
|
|
public addquantity(cid:number,quantity:number):Observable<Cartdto[]>{
|
|
let get_api=this.path + '/cartput/'+cid+'/'+quantity;
|
|
return this.httpClient.post<Cartdto[]>(get_api,this.cartItem);
|
|
}
|
|
public deleteCartItem(cid:number):Observable<any>{
|
|
let api=this.path+'/cart/'+cid;
|
|
return this.httpClient.delete<any>(api);
|
|
}
|
|
|
|
public deleteCart(uid:number):Observable<any>{
|
|
let api=this.path+'/usercart/'+uid;
|
|
return this.httpClient.delete<any>(api);
|
|
}
|
|
|
|
public getCartsum(uid:number):Observable<any>{
|
|
let get_api=this.path + '/cartsum/'+uid;
|
|
this.cartItem= {};
|
|
return this.httpClient.post<any>(get_api,this.cartItem);
|
|
}
|
|
|
|
public postOrder(order:Orderdto,uid:number):Observable<any>{
|
|
let get_api=this.path + '/order/'+uid;
|
|
return this.httpClient.post<any>(get_api,order);
|
|
}
|
|
|
|
public getOrder(uid:number):Observable<any>{
|
|
let get_api=this.path + '/orderdto/'+uid;
|
|
return this.httpClient.get<any>(get_api);
|
|
}
|
|
|
|
public deleteOrderItem(cid:number):Observable<any>{
|
|
let api=this.path+'/order/'+cid;
|
|
return this.httpClient.delete<any>(api);
|
|
}
|
|
|
|
public getordersum(uid:number,onid:number):Observable<any>{
|
|
let get_api=this.path + '/ordersum/'+uid+'/'+onid;
|
|
return this.httpClient.get<any>(get_api);
|
|
}
|
|
|
|
public getordersumByUid(uid:number):Observable<any>{
|
|
let get_api=this.path + '/orderuid/'+uid;
|
|
return this.httpClient.get<any>(get_api);
|
|
}
|
|
|
|
|
|
public addquantityOrder(cid:number,quantity:number):Observable<Orderdto[]>{
|
|
let get_api=this.path + '/orderput/'+cid+'/'+quantity;
|
|
return this.httpClient.post<Orderdto[]>(get_api,this.cartItem);
|
|
}
|
|
|
|
|
|
getProducts()
|
|
{
|
|
return this.productList.asObservable();
|
|
}
|
|
|
|
setProduct(product : any)
|
|
{
|
|
this.cartItemList.push(...product)
|
|
this.productList.next(product)
|
|
}
|
|
|
|
addtoCart(product : any)
|
|
{
|
|
this.cartItemList.push(product);
|
|
this.productList.next(this.cartItemList);
|
|
this.getTotalPrice();
|
|
}
|
|
|
|
getTotalPrice() : number{
|
|
let grandTotal = 0;
|
|
this.cartItemList.map((a:any)=>{
|
|
a.total= a.price*a.quantity;
|
|
grandTotal+=a.total;
|
|
})
|
|
return grandTotal;
|
|
}
|
|
|
|
removeCartItem(product: any)
|
|
{
|
|
this.cartItemList.map((a:any,index:any)=>{
|
|
if(product.id===a.id)
|
|
{
|
|
this.cartItemList.splice(index,1);
|
|
}
|
|
})
|
|
}
|
|
}
|