import { Injectable } from "@angular/core"; import { ReviewService } from "../services/reviewService"; import { Utility } from "./utility"; import { Review } from "../../public-api"; /** * Exposes the User endpoint * */ @Injectable({ providedIn: 'root' }) export class ReviewState { reviews!: Array; constructor(private review: ReviewService, private utility:Utility) { this.reviews = new Array(); } getReviews(id: string) { return this.reviews.filter(f => f.productID === id); } get(done:any,error:any) { const self = this; this.review.getByID().subscribe({ next(rslt) { if (rslt) { if(rslt.results.length === 1){ done(rslt.results[0]); } else { done(); } } }, error(e) { error(e); }, complete() { } }) } getProduct(productID: string, done:any,error:any) { this.review.getByProduct(productID).subscribe({ next(rslt) { if (rslt) { // self.utility.updateRslt(self.reviews, rslt) done(rslt.results) } }, error(e) { error(e); }, complete() { } }) } getByAllProducts(ids:Array, done:any,error:any){ } post(review: Review,done:any,error:any) { const self = this; this.review.post(review).subscribe({ next(rslt) { if (rslt) { // self.utility.updateRslt(self.reviews, rslt) } }, error(e) { error(e); }, complete() { } }) } delete(review: Review, done:any,error:any) { const self = this; this.review.delete(review).subscribe({ next(rslt) { if (rslt) { done(rslt.results) // rslt.results.forEach((f:Review) => { // const index = self.reviews.findIndex(fi => f._id === fi._id); // if(index >=0){ // self.reviews.splice(index,1) // } // }) } }, error(e) { error(e); }, complete() { } }) } }