Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

Commit

Permalink
feat(space): Added a search method to the SpacesService.
Browse files Browse the repository at this point in the history
Fixes fabric8-ui/fabric8-ui#69

Consumers of this API can provide a searchtext to filter spaces
containing the provided text in either the name or description
of the space.
  • Loading branch information
VineetReynolds authored and joshuawilson committed Mar 2, 2017
1 parent 83d7d72 commit baa47ab
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/app/spaces/space.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,22 @@ describe('Service: SpaceService', () => {
});
}));

it('Search a space by name', async(() => {
let matchedData: Space = cloneDeep(responseData[0]);

mockService.connections.subscribe((connection: any) => {
connection.mockRespond(new Response(
new ResponseOptions({
body: JSON.stringify({data: matchedData}),
status: 200
})
));
});

spaceService.search("test")
.then(data => {
expect(data).toEqual(matchedData);
});
}));

});
21 changes: 20 additions & 1 deletion src/app/spaces/space.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable, Inject } from '@angular/core';
import { Headers, Http } from '@angular/http';
import { Headers, Http, URLSearchParams } from '@angular/http';

import { cloneDeep } from 'lodash';

Expand All @@ -14,6 +14,7 @@ export class SpaceService {
private headers = new Headers({ 'Content-Type': 'application/json' });
private spacesUrl: string;
private namedSpacesUrl: string;
private searchSpacesUrl: string;
private nextLink: string = null;

// Array of all spaces that have been retrieved from the REST API.
Expand All @@ -32,6 +33,7 @@ export class SpaceService {
}
this.spacesUrl = apiUrl + 'spaces';
this.namedSpacesUrl = apiUrl + 'namedspaces';
this.searchSpacesUrl = apiUrl + 'search/spaces';
}

getSpaces(pageSize: number = 20): Promise<Space[]> {
Expand Down Expand Up @@ -130,6 +132,23 @@ export class SpaceService {
.catch(this.handleError);
}

search(searchText: string): Promise<Space[]> {
let url = this.searchSpacesUrl;
let params: URLSearchParams = new URLSearchParams();
params.set("q", searchText);

return this.http
.get(url, {search: params, headers: this.headers})
.toPromise()
.then(response => {
// Extract data from JSON API response, and assert to an array of spaces.
let newSpaces: Space[] = response.json().data as Space[];
return newSpaces;
})
.catch(this.handleError);
}


// Adds or updates the client-local list of spaces,
// with spaces retrieved from the server, usually as a page in a paginated collection.
// If a space already exists in the client-local collection,
Expand Down

0 comments on commit baa47ab

Please sign in to comment.