From 3044a154b1d65b7a6057b3de04e2f8bd3b8155bc Mon Sep 17 00:00:00 2001 From: Steven Zou Date: Tue, 9 May 2017 17:18:49 +0800 Subject: [PATCH] Implement repository service interface --- .../lib/src/service/access-log.service.ts | 4 +- src/ui_ng/lib/src/service/interface.ts | 10 +++- .../src/service/repository.service.spec.ts | 24 ++++++--- .../lib/src/service/repository.service.ts | 53 +++++++++++++++---- src/ui_ng/lib/src/utils.ts | 26 ++++++++- 5 files changed, 98 insertions(+), 19 deletions(-) diff --git a/src/ui_ng/lib/src/service/access-log.service.ts b/src/ui_ng/lib/src/service/access-log.service.ts index c414cdaa3a..fd1ac4ba57 100644 --- a/src/ui_ng/lib/src/service/access-log.service.ts +++ b/src/ui_ng/lib/src/service/access-log.service.ts @@ -24,7 +24,7 @@ export abstract class AccessLogService { * @abstract * @param {(number | string)} projectId * @param {RequestQueryParams} [queryParams] - * @returns {(Observable | AccessLog[])} + * @returns {(Observable | Promise | AccessLog[])} * * @memberOf AccessLogService */ @@ -35,7 +35,7 @@ export abstract class AccessLogService { * * @abstract * @param {number} lines : Specify how many lines should be returned. - * @returns {(Observable | AccessLog[])} + * @returns {(Observable | Promise | AccessLog[])} * * @memberOf AccessLogService */ diff --git a/src/ui_ng/lib/src/service/interface.ts b/src/ui_ng/lib/src/service/interface.ts index 73375248d3..1767189614 100644 --- a/src/ui_ng/lib/src/service/interface.ts +++ b/src/ui_ng/lib/src/service/interface.ts @@ -18,7 +18,15 @@ export interface Base { * @interface Repository * @extends {Base} */ -export interface Repository extends Base { } +export interface Repository extends Base { + name: string; + tags_count: number; + owner_id?: number; + project_id?: number; + description?: string; + start_count?: number; + pull_count?: number; +} /** * Interface for the tag of repository diff --git a/src/ui_ng/lib/src/service/repository.service.spec.ts b/src/ui_ng/lib/src/service/repository.service.spec.ts index aa8285319f..32314dd6be 100644 --- a/src/ui_ng/lib/src/service/repository.service.spec.ts +++ b/src/ui_ng/lib/src/service/repository.service.spec.ts @@ -1,20 +1,32 @@ import { TestBed, inject } from '@angular/core/testing'; -import { ReplicationService, ReplicationDefaultService } from './replication.service'; +import { RepositoryService, RepositoryDefaultService } from './repository.service'; +import { SharedModule } from '../shared/shared.module'; +import { SERVICE_CONFIG, IServiceConfig } from '../service.config'; -describe('ReplicationService', () => { +describe('RepositoryService', () => { beforeEach(() => { + const mockConfig: IServiceConfig = { + repositoryBaseEndpoint: "/api/repositories/testing" + }; + TestBed.configureTestingModule({ + imports: [ + SharedModule + ], providers: [ - ReplicationDefaultService, + RepositoryDefaultService, { - provide: ReplicationService, - useClass: ReplicationDefaultService + provide: RepositoryService, + useClass: RepositoryDefaultService + }, { + provide: SERVICE_CONFIG, + useValue: mockConfig }] }); }); - it('should be initialized', inject([ReplicationDefaultService], (service: ReplicationService) => { + it('should be initialized', inject([RepositoryDefaultService], (service: RepositoryService) => { expect(service).toBeTruthy(); })); }); diff --git a/src/ui_ng/lib/src/service/repository.service.ts b/src/ui_ng/lib/src/service/repository.service.ts index 68f05c4850..e5ddd6d4fb 100644 --- a/src/ui_ng/lib/src/service/repository.service.ts +++ b/src/ui_ng/lib/src/service/repository.service.ts @@ -1,8 +1,11 @@ import { Observable } from 'rxjs/Observable'; import { RequestQueryParams } from './RequestQueryParams'; import { Repository } from './interface'; -import { Injectable } from "@angular/core"; +import { Injectable, Inject } from "@angular/core"; import 'rxjs/add/observable/of'; +import { Http } from '@angular/http'; +import { SERVICE_CONFIG, IServiceConfig } from '../service.config'; +import { buildHttpRequestOptions, HTTP_JSON_OPTIONS } from '../utils'; /** * Define service methods for handling the repository related things. @@ -24,22 +27,22 @@ export abstract class RepositoryService { * @param {(number | string)} projectId * @param {string} repositoryName * @param {RequestQueryParams} [queryParams] - * @returns {(Observable | Repository[])} + * @returns {(Observable | Promise | Repository[])} * * @memberOf RepositoryService */ - abstract getRepositories(projectId: number | string, repositoryName?: string, queryParams?: RequestQueryParams): Observable | Repository[]; + abstract getRepositories(projectId: number | string, repositoryName?: string, queryParams?: RequestQueryParams): Observable | Promise | Repository[]; /** * DELETE the specified repository. * * @abstract * @param {string} repositoryName - * @returns {(Observable | any)} + * @returns {(Observable | Promise | any)} * * @memberOf RepositoryService */ - abstract deleteRepository(repositoryName: string): Observable | any; + abstract deleteRepository(repositoryName: string): Observable | Promise | any; } /** @@ -51,11 +54,43 @@ export abstract class RepositoryService { */ @Injectable() export class RepositoryDefaultService extends RepositoryService { - public getRepositories(projectId: number | string, repositoryName?: string, queryParams?: RequestQueryParams): Observable | Repository[] { - return Observable.of([]); + constructor( + private http: Http, + @Inject(SERVICE_CONFIG) private config: IServiceConfig + ) { + super(); } - public deleteRepository(repositoryName: string): Observable | any { - return Observable.of({}); + public getRepositories(projectId: number | string, repositoryName?: string, queryParams?: RequestQueryParams): Observable | Promise | Repository[] { + if (!projectId) { + return Promise.reject("Bad argument"); + } + + if (!queryParams) { + queryParams = new RequestQueryParams(); + } + + queryParams.set('project_id', "" + projectId); + queryParams.set('detail', '1'); + if (repositoryName && repositoryName.trim() !== '') { + queryParams.set('q', repositoryName); + } + + let url: string = this.config.repositoryBaseEndpoint ? this.config.repositoryBaseEndpoint : "/api/repositories"; + return this.http.get(url, buildHttpRequestOptions(queryParams)).toPromise() + .then(response => response.json() as Repository[]) + .catch(error => Promise.reject(error)); + } + + public deleteRepository(repositoryName: string): Observable | Promise | any { + if (!repositoryName) { + return Promise.reject('Bad argument'); + } + let url: string = this.config.repositoryBaseEndpoint ? this.config.repositoryBaseEndpoint : '/api/repositories'; + url = `${url}/${repositoryName}/tags`; + + return this.http.delete(url, HTTP_JSON_OPTIONS).toPromise() + .then(response => response) + .catch(error => { Promise.reject(error) }); } } \ No newline at end of file diff --git a/src/ui_ng/lib/src/utils.ts b/src/ui_ng/lib/src/utils.ts index 0b5baa5952..85dcce116c 100644 --- a/src/ui_ng/lib/src/utils.ts +++ b/src/ui_ng/lib/src/utils.ts @@ -1,6 +1,7 @@ import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/toPromise'; import { RequestOptions, Headers } from '@angular/http'; +import { RequestQueryParams } from './service/RequestQueryParams'; /** * Convert the different async channels to the Promise type. @@ -40,6 +41,29 @@ export const DEFAULT_LANG = 'en-us'; export const HTTP_JSON_OPTIONS: RequestOptions = new RequestOptions({ headers: new Headers({ - "Content-Type": 'application/json' + "Content-Type": 'application/json', + "Accept": 'application/json' }) }); + +/** + * Build http request options + * + * @export + * @param {RequestQueryParams} params + * @returns {RequestOptions} + */ +export function buildHttpRequestOptions(params: RequestQueryParams): RequestOptions { + let reqOptions: RequestOptions = new RequestOptions({ + headers: new Headers({ + "Content-Type": 'application/json', + "Accept": 'application/json' + }) + }); + + if (params) { + reqOptions.search = params; + } + + return reqOptions; +}