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