Skip to content
This repository was archived by the owner on Nov 14, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 9 additions & 21 deletions Typescript/src/api-v3.class.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
module Api.V3 {
"use strict";
export class ResponseCollection<T> {
public data: {
items: T[]
export interface IResponseCollection<T> {
data?: {
count?: number;
items?: T[]
};
/* tslint:disable */
private header: Header;
/* tslint:enable */
}

export class ResponseItem<T> {
public data: T;
/* tslint:disable */
private header: Header;
/* tslint:enable */
export interface IResponseItem<T> {
data?: T;
}
export class ResponseError {
export interface IResponseError {
/* tslint:disable */
public Status: number;
public Message: string;
Status?: number;
Message?: string;
/* tslint:enable */
}

class Header {
public generated: string;
public serverTime: number;
public queryTime: number;
public principal: string;
}

export class EnumValue {
public key: any;
public label: string;
Expand Down
82 changes: 67 additions & 15 deletions Typescript/src/api-v3.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ module Test {
public getCollectionByFilterAsync(filter: string, types: any & any[], apiUrl?: string, fieldTypes?: any & any[]): ng.IPromise<any[]> {
return super.getCollectionByFilterAsync(filter, types, apiUrl, fieldTypes);
}
public getCollectionCountByFilterAsync(filter: string, apiUrl?: string): ng.IPromise<number> {
return super.getCollectionCountByFilterAsync(filter, apiUrl);
}
public postItemAsync(types: any & any[], data: any, apiUrl?: string, fieldTypes?: any & any[]): ng.IPromise<any> {
return super.postItemAsync(types, data, apiUrl, fieldTypes);
}
Expand Down Expand Up @@ -63,10 +66,10 @@ module Test {
hasPosted: () => { },
hasDeleted: () => { },
};
let responseError: Api.V3.ResponseError;
let responseItemEmpty: Api.V3.ResponseItem<any>;
let responseCollection: Api.V3.ResponseCollection<any>;
let responseCollectionEmpty: Api.V3.ResponseCollection<any>;
let responseError: Api.V3.IResponseError;
let responseItemEmpty: Api.V3.IResponseItem<any>;
let responseCollection: Api.V3.IResponseCollection<any>;
let responseCollectionEmpty: Api.V3.IResponseCollection<any>;
let $rootScope: angular.IRootScopeService;
let $q: angular.IQService
describe("api.v3.service", () => {
Expand All @@ -84,13 +87,13 @@ module Test {
spyOn(bogus, "hasRejected");
spyOn(bogus, "hasPosted");
spyOn(bogus, "hasDeleted");
responseError = new Api.V3.ResponseError();
responseError = {};
responseError.Message = "bogus";
responseItemEmpty = new Api.V3.ResponseItem();
responseCollection = new Api.V3.ResponseCollection();
responseCollection.data = { items: [{}] };
responseCollectionEmpty = new Api.V3.ResponseCollection();
responseCollectionEmpty.data = { items: [] };
responseItemEmpty = {};
responseCollection = {};
responseCollection.data = { items: [{}], count: 1 };
responseCollectionEmpty = {};
responseCollectionEmpty.data = { items: [], count: 0 };
});
describe("GET", () => {
// GET ITEM
Expand Down Expand Up @@ -122,7 +125,7 @@ module Test {
it("should callfromApiData with the response data", () => {
spyOn(Api.V3, "fromApiData")
let data = { id: 1, name: "bogus" };
let responseItem = new Api.V3.ResponseItem<any>();
let responseItem: Api.V3.IResponseItem<any> = {};
responseItem.data = data;
$httpBackend.expectGET(/api\/v3\/mainapi/i).respond(200, responseItem);
service.getItemByFilterAsync("", TestServiceBogusClass);
Expand Down Expand Up @@ -334,7 +337,7 @@ module Test {
it("should callfromApiData with the response data", () => {
spyOn(Api.V3, "fromApiData")
let item = { id: 1, name: "bogus" };
let myResponseCollection = new Api.V3.ResponseCollection<any>();
let myResponseCollection: Api.V3.IResponseCollection<any> = {};
myResponseCollection.data = { items: [item] };
$httpBackend.expectGET(/api\/v3\/mainapi/i).respond(200, myResponseCollection);
service.getCollectionByFilterAsync("", TestServiceBogusClass);
Expand Down Expand Up @@ -424,6 +427,55 @@ module Test {
expect(service.getCollectionByFilterAsync).toHaveBeenCalledWith("", TestServiceBogusClass, undefined, TestServiceFooClass);
});
});
describe("getCollectionCountByFilterAsync", () => {
it("should resolve when everything goes right", () => {
spyOn(Api.V3, "fromApiData");
service.getCollectionCountByFilterAsync("")
.then(bogus.hasResolved, bogus.hasRejected);
$httpBackend.expectGET(/api\/v3\/mainapi/i).respond(200, responseCollectionEmpty);
$httpBackend.flush();
expect(bogus.hasResolved).toHaveBeenCalled();
expect(bogus.hasRejected).not.toHaveBeenCalled();
});
it("should reject if the http fails, and not call fromApiData", () => {
spyOn(Api.V3, "fromApiData");
service.getCollectionCountByFilterAsync("")
.then(bogus.hasResolved, (message: string) => {
bogus.hasRejected();
expect(message).toBe(responseError.Message);
})
$httpBackend.expectGET(/api\/v3\/mainapi/i).respond(500, responseError);
$httpBackend.flush();
expect(bogus.hasResolved).not.toHaveBeenCalled();
expect(bogus.hasRejected).toHaveBeenCalled();
});
it("should resolve with the count", () => {
spyOn(Api.V3, "fromApiData");
service.getCollectionCountByFilterAsync("")
.then((cnt: number) => {
bogus.hasResolved();
expect(cnt).toBe(responseCollection.data.count)
});
$httpBackend.expectGET(/api\/v3\/mainapi/i).respond(200, responseCollection);
$httpBackend.flush();
expect(bogus.hasResolved).toHaveBeenCalled();
});
it("should call with the right fields", () => {
service.getCollectionCountByFilterAsync("");
$httpBackend.expectGET(/api\/v3\/mainapi\?fields=collection\.count/i).respond(500, responseError);
expect($httpBackend.flush).not.toThrow;
});
it("should call with the right filter", () => {
service.getCollectionCountByFilterAsync("bogus=excellent");
$httpBackend.expectGET(/api\/v3\/mainapi\?bogus=excellent\&fields=collection\.count/i).respond(500, responseError);
expect($httpBackend.flush).not.toThrow;
});
it("should call the apiUrl specified if any", () => {
service.getCollectionCountByFilterAsync("", "/api/v3/bogus");
$httpBackend.expectGET(/api\/v3\/bogus/i).respond(500, responseError);
expect($httpBackend.flush).not.toThrow;
});
});
});
describe("POST", () => {
describe("postItemAsync", () => {
Expand Down Expand Up @@ -469,7 +521,7 @@ module Test {
it("should callfromApiData with the response data", () => {
spyOn(Api.V3, "fromApiData")
let responseData = { id: 1, name: "bogus" };
let responseItem = new Api.V3.ResponseItem<any>();
let responseItem: Api.V3.IResponseItem<any> = {};
responseItem.data = responseData;
$httpBackend.expectPOST(/api\/v3\/mainapi/i).respond(200, responseItem);
service.postItemAsync(TestServiceBogusClass, data);
Expand Down Expand Up @@ -585,7 +637,7 @@ module Test {
it("should callfromApiData with the response data", () => {
spyOn(Api.V3, "fromApiData")
let responseData = { id: 1, name: "bogus" };
let responseItem = new Api.V3.ResponseItem<any>();
let responseItem: Api.V3.IResponseItem<any> = {};
responseItem.data = responseData;
$httpBackend.expectPOST(/api\/v3\/mainapi/i).respond(200, responseItem);
service.postItemsAsync(TestServiceBogusClass, data);
Expand Down Expand Up @@ -701,7 +753,7 @@ module Test {
it("should callfromApiData with the response data", () => {
spyOn(Api.V3, "fromApiData")
let responseData = { id: 1, name: "bogus" };
let responseItem = new Api.V3.ResponseItem<any>();
let responseItem: Api.V3.IResponseItem<any> = {};
responseItem.data = responseData;
$httpBackend.expectPUT(/api\/v3\/mainapi/i).respond(200, responseItem);
service.putItemAsync(12, TestServiceBogusClass, data);
Expand Down
40 changes: 31 additions & 9 deletions Typescript/src/api-v3.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ module Api.V3 {
let url = this.getUrlToCall(apiUrl, filter, fieldTypes || types);
return this.callAndTransformCollection(method, url, types);
}
protected getCollectionCountByFilterAsync(filter: string, apiUrl?: string): ng.IPromise<number> {
let method = HttpMethod.GET;
if (!!filter) {
filter = "?" + filter;
}

let url = this.getUrlToCall(apiUrl, filter) + "fields=collection.count";
return this.callAndTransformCount(method, url);
}

/////////////////////
// POST ITEM //
Expand Down Expand Up @@ -109,7 +118,7 @@ module Api.V3 {
return this.callAndTransformCollection(method, url, types, postableData);
} else {
let dfd = this.$q.defer();
dfd.reject(<Api.V3.ResponseError>{ Message: "You have to provide an id for each item." });
dfd.reject(<Api.V3.IResponseError>{ Message: "You have to provide an id for each item." });
return dfd.promise;
}
}
Expand Down Expand Up @@ -142,9 +151,9 @@ module Api.V3 {
data: postableData,
headers: { "Content-Type": "application/json" }, // Need to specify content-type because it's "text/plain" by default for DELETE
})
.then((response: ng.IHttpPromiseCallbackArg<ResponseItem<any>>) => {
.then((response: ng.IHttpPromiseCallbackArg<IResponseItem<any>>) => {
dfd.resolve();
}, (response: ng.IHttpPromiseCallbackArg<ResponseError>) => {
}, (response: ng.IHttpPromiseCallbackArg<IResponseError>) => {
dfd.reject(response.data.Message);
});
} else {
Expand Down Expand Up @@ -181,9 +190,9 @@ module Api.V3 {
url: url,
data: data,
})
.then((response: ng.IHttpPromiseCallbackArg<ResponseItem<any>>) => {
.then((response: ng.IHttpPromiseCallbackArg<IResponseItem<any>>) => {
dfd.resolve(Api.V3.fromApiData(types, response.data.data));
}, (response: ng.IHttpPromiseCallbackArg<ResponseError>) => {
}, (response: ng.IHttpPromiseCallbackArg<IResponseError>) => {
dfd.reject(response.data.Message);
});
return dfd.promise;
Expand All @@ -195,13 +204,13 @@ module Api.V3 {
url: url,
data: data,
})
.then((response: ng.IHttpPromiseCallbackArg<ResponseCollection<any>>) => {
.then((response: ng.IHttpPromiseCallbackArg<IResponseCollection<any>>) => {
dfd.resolve(
_.map(response.data.data.items, (item: any) => {
return Api.V3.fromApiData(types, item);
})
);
}, (response: ng.IHttpPromiseCallbackArg<ResponseError>) => {
}, (response: ng.IHttpPromiseCallbackArg<IResponseError>) => {
dfd.reject(response.data.Message);
});
return dfd.promise;
Expand All @@ -213,9 +222,22 @@ module Api.V3 {
url: url,
data: data,
})
.then((response: ng.IHttpPromiseCallbackArg<ResponseItem<any>>) => {
.then((response: ng.IHttpPromiseCallbackArg<IResponseItem<any>>) => {
dfd.resolve();
}, (response: ng.IHttpPromiseCallbackArg<ResponseError>) => {
}, (response: ng.IHttpPromiseCallbackArg<IResponseError>) => {
dfd.reject(response.data.Message);
});
return dfd.promise;
}
private callAndTransformCount(method: string, url: string): ng.IPromise<number> {
let dfd = this.$q.defer();
this.$http({
method: method,
url: url,
})
.then((response: ng.IHttpPromiseCallbackArg<IResponseCollection<any>>) => {
dfd.resolve(response.data.data.count);
}, (response: ng.IHttpPromiseCallbackArg<IResponseError>) => {
dfd.reject(response.data.Message);
});
return dfd.promise;
Expand Down