aboutsummaryrefslogtreecommitdiff
path: root/Timeline/ClientApp/src/app/todo/todo-service
diff options
context:
space:
mode:
Diffstat (limited to 'Timeline/ClientApp/src/app/todo/todo-service')
-rw-r--r--Timeline/ClientApp/src/app/todo/todo-service/todo-list.service.spec.ts54
-rw-r--r--Timeline/ClientApp/src/app/todo/todo-service/todo-list.service.ts43
2 files changed, 97 insertions, 0 deletions
diff --git a/Timeline/ClientApp/src/app/todo/todo-service/todo-list.service.spec.ts b/Timeline/ClientApp/src/app/todo/todo-service/todo-list.service.spec.ts
new file mode 100644
index 00000000..d8283b54
--- /dev/null
+++ b/Timeline/ClientApp/src/app/todo/todo-service/todo-list.service.spec.ts
@@ -0,0 +1,54 @@
+import { TestBed } from '@angular/core/testing';
+import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
+import { toArray } from 'rxjs/operators';
+
+import { TodoItem } from '../todo-item';
+import { TodoListService, IssueResponse } from './todo-list.service';
+
+
+describe('TodoListServiceService', () => {
+ beforeEach(() => TestBed.configureTestingModule({
+ imports: [HttpClientTestingModule]
+ }));
+
+ it('should be created', () => {
+ const service: TodoListService = TestBed.get(TodoListService);
+ expect(service).toBeTruthy();
+ });
+
+ it('should work well', () => {
+ const service: TodoListService = TestBed.get(TodoListService);
+
+ const baseUrl = service.baseUrl;
+
+ const mockIssueList: IssueResponse = [{
+ number: 1,
+ title: 'Issue title 1',
+ state: 'open',
+ html_url: 'test_url1'
+ }, {
+ number: 2,
+ title: 'Issue title 2',
+ state: 'closed',
+ html_url: 'test_url2',
+ pull_request: {}
+ }];
+
+ const mockTodoItemList: TodoItem[] = [{
+ number: 1,
+ title: 'Issue title 1',
+ isClosed: false,
+ detailUrl: 'test_url1'
+ }];
+
+ service.getWorkItemList().pipe(toArray()).subscribe(data => {
+ expect(data).toEqual(mockTodoItemList);
+ });
+
+ const httpController: HttpTestingController = TestBed.get(HttpTestingController);
+
+ httpController.expectOne(request => request.url === baseUrl + '/issues' && request.params.get('state') === 'all').flush(mockIssueList);
+
+ httpController.verify();
+ });
+});
diff --git a/Timeline/ClientApp/src/app/todo/todo-service/todo-list.service.ts b/Timeline/ClientApp/src/app/todo/todo-service/todo-list.service.ts
new file mode 100644
index 00000000..83bf47ec
--- /dev/null
+++ b/Timeline/ClientApp/src/app/todo/todo-service/todo-list.service.ts
@@ -0,0 +1,43 @@
+import { Injectable } from '@angular/core';
+import { HttpClient } from '@angular/common/http';
+import { Observable, from } from 'rxjs';
+import { switchMap, map, filter } from 'rxjs/operators';
+
+import {TodoItem} from '../todo-item';
+
+export interface IssueResponseItem {
+ number: number;
+ title: string;
+ state: string;
+ html_url: string;
+ pull_request?: any;
+}
+
+export type IssueResponse = IssueResponseItem[];
+
+@Injectable({
+ providedIn: 'root'
+})
+export class TodoListService {
+
+ readonly baseUrl = 'https://api.github.com/repos/crupest/Timeline';
+
+ constructor(private client: HttpClient) { }
+
+ getWorkItemList(): Observable<TodoItem> {
+ return this.client.get<IssueResponse>(`${this.baseUrl}/issues`, {
+ params: {
+ state: 'all'
+ }
+ }).pipe(
+ switchMap(result => from(result)),
+ filter(result => result.pull_request === undefined), // filter out pull requests.
+ map(result => <TodoItem>{
+ number: result.number,
+ title: result.title,
+ isClosed: result.state === 'closed',
+ detailUrl: result.html_url
+ })
+ );
+ }
+}