import { Injectable } from "@angular/core"; import { Utility } from "./utility"; import { SellService } from "../services/sellService"; import { Selling } from "../../public-api"; /** * Exposes the User endpoint * */ @Injectable({ providedIn: 'root' }) export class SellState { //your selling options sells: Array = new Array; //other peoples selling options marketplace: Array = new Array; constructor(private sell: SellService, private utility:Utility) { this.sells = new Array(); } get(id:string,error:any) { const self = this; this.sell.get(id).subscribe({ next(rslt: { results: Selling[]; }) { if (rslt) { self.utility.update(self.sells, rslt.results) } }, error(e:any) { error(e); }, complete() { } }) } getByProductID(id:string,error:any) { const self = this; this.sell.get(id).subscribe({ next(rslt: { results: Selling[]; }) { if (rslt) { self.utility.update(self.sells, rslt.results) } }, error(e:any) { error(e); }, complete() { } }) } getByProductIDs(ids:string[],error:any) { const self = this; this.sell.getByProducts(ids).subscribe({ next(rslt: { results: Selling[]; }) { if (rslt) { self.utility.update(self.sells, rslt.results) } }, error(e:any) { error(e); }, complete() { } }) } post(sell: Selling,done:any, error:any) { const self = this; this.sell.post(sell).subscribe({ next(rslt:any) { if (rslt) { self.utility.update(self.sells, rslt) } }, error(e:any) { error(e); }, complete() { done() } }) } put(sell: Selling, done:any, error:any) { const self = this; this.sell.put(sell).subscribe({ next(rslt:any) { if (rslt) { self.utility.updateRslt(self.sells, rslt) } }, error(e:any) { error(e); }, complete() { done(); } }) } delete(sell: Selling, done:any, error:any) { const self = this; this.sell.delete(sell).subscribe({ next(rslt:any) { if (rslt) { rslt.results.forEach((f:Selling) => { const index = self.sells.findIndex(fi => f._id === fi._id); if(index >=0){ self.sells.splice(index,1) } }) } }, error(e:any) { error(e); }, complete() { done(); } }) } }