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

Commit

Permalink
fix(spaces): validate inputs of functions (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppitonak authored and joshuawilson committed Jan 9, 2019
1 parent 91f808c commit ffef6b0
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions src/app/spaces/space.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export class SpaceService {
}

getSpaces(pageSize: number = 20): Observable<Space[]> {
if (pageSize <= 0) {
return observableThrowError('Page limit cannot be less or equal 0');
}
const url: string = `${this.spacesUrl}?page[limit]=${pageSize}`;
return this.getSpacesDelegate(url, true);
}
Expand All @@ -61,6 +64,12 @@ export class SpaceService {
}

getSpaceByName(userName: string, spaceName: string): Observable<Space> {
if (!userName) {
return observableThrowError('User name cannot be empty');
}
if (!spaceName) {
return observableThrowError('Space name cannot be empty');
}
const url: string = `${this.namedSpacesUrl}/${encodeURIComponent(userName)}/${encodeURIComponent(spaceName)}`;
return this.http.get<Space>(url, { headers: this.headers })
.pipe(
Expand Down Expand Up @@ -101,6 +110,9 @@ export class SpaceService {
}

create(space: Space): Observable<Space> {
if (!space) {
return observableThrowError('Space cannot be undefined');
}
const url: string = this.spacesUrl;
const payload: string = JSON.stringify({ data: space });
return this.http.post<Space>(url, payload, { headers: this.headers })
Expand All @@ -112,6 +124,9 @@ export class SpaceService {
}

update(space: Space): Observable<Space> {
if (!space) {
return observableThrowError('Space cannot be undefined');
}
const url: string = `${this.spacesUrl}/${space.id}`;
const payload: string = JSON.stringify({ data: space });
return this.http.patch<Space>(url, payload, { headers: this.headers })
Expand All @@ -123,20 +138,32 @@ export class SpaceService {
}

deleteSpace(space: Space, skipCluster: boolean = false): Observable<Space> {
if (!space) {
return observableThrowError('Space cannot be undefined');
}
const url: string = `${this.spacesUrl}/${space.id}`;
const options = {headers: this.headers, params: new HttpParams().set('skipCluster', skipCluster.toString()) };
const options = { headers: this.headers, params: new HttpParams().set('skipCluster', skipCluster.toString()) };
return this.http.delete(url, options)
.pipe(
map(() => {}),
catchError((error: any): Observable<any> => this.handleError(error))
);
}

search(searchText: string, pageSize: number = 20, pageNumber: number = 0): Observable<Space[]> {
const url: string = this.searchSpacesUrl;
search(searchText: string = '*', pageSize: number = 20, pageNumber: number = 0): Observable<Space[]> {
if (searchText === '') {
searchText = '*';
}
if (!searchText) {
return observableThrowError('Search query cannot be undefined');
}
if (pageSize <= 0) {
return observableThrowError('Page limit cannot be less or equal 0');
}
if (pageNumber < 0) {
return observableThrowError('Page offset cannot be less than 0');
}
const url: string = this.searchSpacesUrl;
const params: HttpParams = new HttpParams().set('q', searchText)
.append('page[offset]', (pageSize * pageNumber).toString())
.append('page[limit]', pageSize.toString());
Expand All @@ -161,6 +188,12 @@ export class SpaceService {

// Currently serves to fetch the list of all spaces owned by a user.
getSpacesByName(userName: string, pageSize: number = 20): Observable<Space[]> {
if (!userName) {
return observableThrowError('User name cannot be empty');
}
if (pageSize <= 0) {
return observableThrowError('Page limit cannot be less or equal 0');
}
const url: string = `${this.namedSpacesUrl}/${encodeURIComponent(userName)}?page[limit]=${pageSize}`;
return this.getSpacesDelegate(url, false);
}
Expand All @@ -181,6 +214,9 @@ export class SpaceService {
}

getSpaceById(spaceId: string): Observable<Space> {
if (!spaceId) {
return observableThrowError('ID cannot be empty');
}
const url: string = `${this.spacesUrl}/${encodeURIComponent(spaceId)}`;
return this.http.get<Space>(url, { headers: this.headers })
.pipe(
Expand Down Expand Up @@ -252,5 +288,4 @@ export class SpaceService {
})
);
}

}

0 comments on commit ffef6b0

Please sign in to comment.