Skip to content

Commit

Permalink
#68, Added order-by segment on it with unit-test, unit-test environme…
Browse files Browse the repository at this point in the history
…nt added
  • Loading branch information
thehoneymad committed Jun 23, 2016
1 parent 04e07eb commit 445225f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
17 changes: 15 additions & 2 deletions app/job/shared/job.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,27 @@ import {PageEnvelope, Pagination} from '../../shared/pagination';
import {AppSettings} from '../../shared/app.settings';
import {Job} from '../shared/job';

import {QueryBuilder} from '../../shared/query-builder/query-builder';

@Injectable()
export class JobService {
constructor(private shttp: SecureHttp) { }
private _queryBuilder: QueryBuilder;

constructor(private shttp: SecureHttp) {
// INFO: Should be injected here
this._queryBuilder = new QueryBuilder();
}

private jobUrl = AppSettings.TASKCAT_API_BASE + 'job';

getHistory(): Observable<PageEnvelope<Job>> {
return this.shttp.secureGet(this.jobUrl + '/odata')
let queryString : string = this._queryBuilder.orderBy([
{
propName: "CreateTime",
orderDirection: "desc"
}]).toQueryString();

return this.shttp.secureGet(this.jobUrl + '/odata' + queryString)
.map((res: Response) => {
if (res.status < 200 || res.status >= 300) {
throw new Error('Response status: ' + res.status);
Expand Down
6 changes: 3 additions & 3 deletions app/shared/query-builder/query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { Injectable } from '@angular/core';
export type OrderDirection = "asc" | "desc";

@Injectable()
export class QueryBuilderService {
export class QueryBuilder {
private _querySegments: Array<string> = new Array<string>();

constructor() {
}

public orderBy(props: Array<{ propName: string, orderDirection?: OrderDirection }>): QueryBuilderService {
public orderBy(props: Array<{ propName: string, orderDirection?: OrderDirection }>): QueryBuilder {
if (props && props.length > 0) {
let querySegment: string = "$orderby";
let orderbySegment = props.map(
Expand All @@ -20,7 +20,7 @@ export class QueryBuilderService {
return this;
}

public ToQueryString(): string {
public toQueryString(): string {
return "?".concat(this._querySegments.join("&"));
}
}
8 changes: 4 additions & 4 deletions tests/query-builder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { QueryBuilderService } from '../app/shared/query-builder/query-builder';
import { QueryBuilder } from '../app/shared/query-builder/query-builder';

describe('QueryBuilder Service Tests', () => {
it('Intantiates Okay', () => {
let service = new QueryBuilderService();
let service = new QueryBuilder();
expect(service).not.toBe(null);
});

it('Generates proper query for OrderBy', () => {
let service = new QueryBuilderService();
let service = new QueryBuilder();
service = service.orderBy([
{
propName: "test_prop1"
Expand All @@ -20,7 +20,7 @@ describe('QueryBuilder Service Tests', () => {
propName: "test_prop3",
orderDirection: "asc"
}]);
let qstring = service.ToQueryString();
let qstring = service.toQueryString();
expect(qstring).toBe("?$orderby=test_prop1 asc,test_prop2 desc,test_prop3 asc");
});
});
Expand Down

0 comments on commit 445225f

Please sign in to comment.