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
* @param {(number | string)} projectId
* @param {RequestQueryParams} [queryParams]
* @returns {(Observable<AccessLog[]> | AccessLog[])}
* @returns {(Observable<AccessLog[]> | Promise<AccessLog[]> | 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[]> | AccessLog[])}
* @returns {(Observable<AccessLog[]> | Promise<AccessLog[]> | AccessLog[])}
*
* @memberOf AccessLogService
*/

View File

@ -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

View File

@ -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();
}));
});

View File

@ -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[]> | Repository[])}
* @returns {(Observable<Repository[]> | Promise<Repository[]> | Repository[])}
*
* @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.
*
* @abstract
* @param {string} repositoryName
* @returns {(Observable<any> | any)}
* @returns {(Observable<any> | Promise<any> | any)}
*
* @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()
export class RepositoryDefaultService extends RepositoryService {
public getRepositories(projectId: number | string, repositoryName?: string, queryParams?: RequestQueryParams): Observable<Repository[]> | Repository[] {
return Observable.of([]);
constructor(
private http: Http,
@Inject(SERVICE_CONFIG) private config: IServiceConfig
) {
super();
}
public deleteRepository(repositoryName: string): Observable<any> | any {
return Observable.of({});
public getRepositories(projectId: number | string, repositoryName?: string, queryParams?: RequestQueryParams): Observable<Repository[]> | Promise<Repository[]> | 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<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 'rxjs/add/operator/toPromise';
import { RequestOptions, Headers } from '@angular/http';
import { RequestQueryParams } from './service/RequestQueryParams';
/**
* 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({
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;
}