Finished value service
This commit is contained in:
@@ -21,12 +21,12 @@ export class ValueService {
|
||||
cb(null, data.meta);
|
||||
return;
|
||||
}
|
||||
cb(data.data[0], null);
|
||||
cb(this.unflatObject(data.data[0]), null);
|
||||
});
|
||||
}
|
||||
|
||||
public getValues(path: string, filters: any, cb: (res: Object, err: any) => void) {
|
||||
this.httpCachedService.getJSON(this.PREFIX+path, this.flatObject(filters), (data: ResponseInterface, e: Response | any) => {
|
||||
public getValues(path: string, filters: {[_:string]: any}, cb: (res: Object, err: any) => void) {
|
||||
this.httpCachedService.getJSON(this.PREFIX+path, this.flatObject(filters), (data, e) => {
|
||||
if (e) {
|
||||
cb(null, e);
|
||||
return;
|
||||
@@ -35,17 +35,67 @@ export class ValueService {
|
||||
cb(null, data.meta);
|
||||
return;
|
||||
}
|
||||
cb(data.data, null);
|
||||
let ret = data.data;
|
||||
for (let i=0; i<ret.length; i++)
|
||||
ret[i] = this.unflatObject(ret[i]);
|
||||
cb(ret, null);
|
||||
});
|
||||
}
|
||||
|
||||
private flatObject(obj: any, prefix?: string): {[_:string]: any} {
|
||||
public postValues(path: string, values: {[_:string]: any}, cb: (res: Object, err: any) => void) {
|
||||
this.httpCachedService.postJSON(this.PREFIX + path, {}, this.flatObject(values), (data, e) => {
|
||||
if (e) {
|
||||
cb(null, e);
|
||||
return;
|
||||
}
|
||||
if (data.meta.code != 0) {
|
||||
cb(null, data.meta);
|
||||
return;
|
||||
}
|
||||
cb(this.unflatObject(data.data[0]), null);
|
||||
});
|
||||
}
|
||||
|
||||
public putValues(path: string, id: any, values: {[_:string]: any}, cb: (res: Object, err: any) => void) {
|
||||
this.httpCachedService.putJSON(this.PREFIX+path+"/"+id, {}, this.flatObject(values), (data, e) => {
|
||||
if (e) {
|
||||
cb(null, e);
|
||||
return;
|
||||
}
|
||||
if (data.meta.code != 0) {
|
||||
cb(null, data.meta);
|
||||
return;
|
||||
}
|
||||
cb(this.unflatObject(data.data[0]), null);
|
||||
});
|
||||
}
|
||||
|
||||
public deleteValues(path: string, id: any, cb: (res: Object, err: any) => void) {
|
||||
this.httpCachedService.deleteJSON(this.PREFIX+path+"/"+id, {}, (data, e) => {
|
||||
if (e) {
|
||||
cb(null, e);
|
||||
return;
|
||||
}
|
||||
if (data.meta.code != 0) {
|
||||
cb(null, data.meta);
|
||||
return;
|
||||
}
|
||||
cb(null, null);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private flatObject(obj: {[_:string]: any}, prefix: string = ''): {[_:string]: any} {
|
||||
let ret: {[_:string]: any} = {};
|
||||
if (!prefix)
|
||||
prefix = '';
|
||||
else
|
||||
prefix = prefix+'.';
|
||||
if (Object.prototype.toString.call(obj) == "[object Object]") {
|
||||
if (prefix)
|
||||
prefix = prefix+'.';
|
||||
for (let key in obj) {
|
||||
if (obj.hasOwnProperty(key)) {
|
||||
ret = Object.assign(ret, this.flatObject(obj[key], prefix+key));
|
||||
@@ -56,4 +106,25 @@ export class ValueService {
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
private unflatObject(obj: {[_:string]: any}): {[_:string]: any} {
|
||||
let ret: {[_:string]: any} = {};
|
||||
for (let key in obj) {
|
||||
if (obj.hasOwnProperty(key)) {
|
||||
let strings: string[] = key.split('.');
|
||||
ret = this.addValueToPath(ret, strings, obj[key]);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
private addValueToPath(obj: {[_:string]: any}, path: string[], value: any) {
|
||||
let key = path.shift();
|
||||
if (!path || !path.length)
|
||||
return Object.assign(obj, {[key]: value});
|
||||
if (!obj.hasOwnProperty(key))
|
||||
obj[key] = {};
|
||||
obj[key] = Object.assign(obj[key], this.addValueToPath(obj[key], path, value));
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user