Merge pull request #2268 from steven-zou/master

Implement repository service interface
This commit is contained in:
Steven Zou 2017-05-09 17:49:20 +08:00 committed by GitHub
commit 79544d39e0
5 changed files with 98 additions and 19 deletions

View File

@ -24,7 +24,7 @@ export abstract class AccessLogService {
* @abstract * @abstract
* @param {(number | string)} projectId * @param {(number | string)} projectId
* @param {RequestQueryParams} [queryParams] * @param {RequestQueryParams} [queryParams]
* @returns {(Observable<AccessLog[]> | AccessLog[])} * @returns {(Observable<AccessLog[]> | Promise<AccessLog[]> | AccessLog[])}
* *
* @memberOf AccessLogService * @memberOf AccessLogService
*/ */
@ -35,7 +35,7 @@ export abstract class AccessLogService {
* *
* @abstract * @abstract
* @param {number} lines : Specify how many lines should be returned. * @param {number} lines : Specify how many lines should be returned.
* @returns {(Observable<AccessLog[]> | AccessLog[])} * @returns {(Observable<AccessLog[]> | Promise<AccessLog[]> | AccessLog[])}
* *
* @memberOf AccessLogService * @memberOf AccessLogService
*/ */

View File

@ -18,7 +18,15 @@ export interface Base {
* @interface Repository * @interface Repository
* @extends {Base} * @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 * Interface for the tag of repository

View File

@ -1,20 +1,32 @@
import { TestBed, inject } from '@angular/core/testing'; 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(() => { beforeEach(() => {
const mockConfig: IServiceConfig = {
repositoryBaseEndpoint: "/api/repositories/testing"
};
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [
SharedModule
],
providers: [ providers: [
ReplicationDefaultService, RepositoryDefaultService,
{ {
provide: ReplicationService, provide: RepositoryService,
useClass: ReplicationDefaultService 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(); expect(service).toBeTruthy();
})); }));
}); });

View File

@ -1,8 +1,11 @@
import { Observable } from 'rxjs/Observable'; import { Observable } from 'rxjs/Observable';
import { RequestQueryParams } from './RequestQueryParams'; import { RequestQueryParams } from './RequestQueryParams';
import { Repository } from './interface'; import { Repository } from './interface';
import { Injectable } from "@angular/core"; import { Injectable, Inject } from "@angular/core";
import 'rxjs/add/observable/of'; 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. * Define service methods for handling the repository related things.
@ -24,22 +27,22 @@ export abstract class RepositoryService {
* @param {(number | string)} projectId * @param {(number | string)} projectId
* @param {string} repositoryName * @param {string} repositoryName
* @param {RequestQueryParams} [queryParams] * @param {RequestQueryParams} [queryParams]
* @returns {(Observable<Repository[]> | Repository[])} * @returns {(Observable<Repository[]> | Promise<Repository[]> | Repository[])}
* *
* @memberOf RepositoryService * @memberOf RepositoryService
*/ */
abstract getRepositories(projectId: number | string, repositoryName?: string, queryParams?: RequestQueryParams): Observable<Repository[]> | Repository[]; abstract getRepositories(projectId: number | string, repositoryName?: string, queryParams?: RequestQueryParams): Observable<Repository[]> | Promise<Repository[]> | Repository[];
/** /**
* DELETE the specified repository. * DELETE the specified repository.
* *
* @abstract * @abstract
* @param {string} repositoryName * @param {string} repositoryName
* @returns {(Observable<any> | any)} * @returns {(Observable<any> | Promise<any> | any)}
* *
* @memberOf RepositoryService * @memberOf RepositoryService
*/ */
abstract deleteRepository(repositoryName: string): Observable<any> | any; abstract deleteRepository(repositoryName: string): Observable<any> | Promise<any> | any;
} }
/** /**
@ -51,11 +54,43 @@ export abstract class RepositoryService {
*/ */
@Injectable() @Injectable()
export class RepositoryDefaultService extends RepositoryService { export class RepositoryDefaultService extends RepositoryService {
public getRepositories(projectId: number | string, repositoryName?: string, queryParams?: RequestQueryParams): Observable<Repository[]> | Repository[] { constructor(
return Observable.of([]); private http: Http,
@Inject(SERVICE_CONFIG) private config: IServiceConfig
) {
super();
} }
public deleteRepository(repositoryName: string): Observable<any> | any { public getRepositories(projectId: number | string, repositoryName?: string, queryParams?: RequestQueryParams): Observable<Repository[]> | Promise<Repository[]> | Repository[] {
return Observable.of({}); 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<any> | Promise<any> | 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) });
} }
} }

View File

@ -1,6 +1,7 @@
import { Observable } from 'rxjs/Observable'; import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/toPromise'; import 'rxjs/add/operator/toPromise';
import { RequestOptions, Headers } from '@angular/http'; import { RequestOptions, Headers } from '@angular/http';
import { RequestQueryParams } from './service/RequestQueryParams';
/** /**
* Convert the different async channels to the Promise<T> type. * Convert the different async channels to the Promise<T> type.
@ -40,6 +41,29 @@ export const DEFAULT_LANG = 'en-us';
export const HTTP_JSON_OPTIONS: RequestOptions = new RequestOptions({ export const HTTP_JSON_OPTIONS: RequestOptions = new RequestOptions({
headers: new Headers({ 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;
}