import { Injectable } from "@angular/core"; import { Utility } from "./utility"; import { ConfigDataDTO } from "../../public-api"; import { ConfigService } from "../services/configService"; /** * Exposes the User endpoint * */ @Injectable({ providedIn: 'root' }) export class ConfigState { configs: Array; constructor(private config: ConfigService, private utility: Utility) { this.configs = new Array(); } get(id: string, done: any, error: any) { const self = this; this.config.getByID(id).subscribe({ next(rslt) { if (rslt) { done(rslt.results); } }, error(e) { error(e) }, complete() { } }) } getAll(done: any, error: any) { const self = this; this.config.getAll().subscribe({ next(rslt) { console.log(JSON.stringify(rslt)) rslt?.results.forEach(f => { const index = self.configs.findIndex(fi => fi._id === f._id) console.log('Modify' + JSON.stringify(f)) if(index >= 0){ self.configs[index] = f; } else { console.log(JSON.stringify(rslt)) self.configs.push(f) } }) }, error(e) { error(e) }, complete() { } }) } post(config: ConfigDataDTO, done: any, error: any) { const self = this; this.config.post(config).subscribe({ next(rslt) { done(rslt.results) // console.log(JSON.stringify(rslt)) // rslt?.results.forEach(f => { // const index = self.configs.findIndex(fi => fi._id === f._id) // console.log('Modify' + JSON.stringify(f)) // if(index >= 0){ // self.configs[index] = f; // } else { // console.log(JSON.stringify(rslt)) // self.configs.push(f) // } // }) }, error(e) { error(e) }, complete() { } }) } postAll(configs: Array, done: any, error: any) { const self = this; this.config.postAll(configs).subscribe({ next(rslt) { // console.log(JSON.stringify(rslt)) // rslt?.results.forEach(f => { // const index = self.configs.findIndex(fi => fi._id === f._id) // console.log('Modify' + JSON.stringify(f)) // if(index >= 0){ // self.configs[index] = f; // } else { // console.log(JSON.stringify(rslt)) // self.configs.push(f) // } // }) done(rslt.results) }, error(e) { error(e) }, complete() { } }) } put(config: ConfigDataDTO, done:any, err:any) { const self = this; this.config.put(config).subscribe({ next(rslt) { done(rslt.results) // console.log(JSON.stringify(rslt)) // rslt?.results.forEach(f => { // const index = self.configs.findIndex(fi => fi._id === f._id) // console.log('Modify' + JSON.stringify(f)) // if(index >= 0){ // self.configs[index] = f; // } else { // console.log(JSON.stringify(rslt)) // self.configs.push(f) // } // }) }, error(e) { err(e) }, complete() { } }) } delete(config: ConfigDataDTO, done: any, error: any) { const self = this; this.config.delete(config).subscribe({ next(rslt) { if (rslt) { rslt?.results.forEach((f: ConfigDataDTO) => { const index = self.configs.findIndex(fi => f._id === fi._id); if (index >= 0) { self.configs.splice(index, 1) } }) } done(); }, error(e) { error(e) }, complete() { } }) } }