From e2ac15234a824564affcca50d6df80a8f22af806 Mon Sep 17 00:00:00 2001 From: buqiyuan <1743369777@qq.com> Date: Mon, 15 Aug 2022 15:42:44 +0800 Subject: [PATCH] feat: api history list support remote server --- .../pages/api/history/eo-history.component.ts | 44 +++++++++++++------ .../services/storage/IndexedDB/lib/index.ts | 6 +-- .../shared/services/storage/http/lib/index.ts | 8 +++- .../services/storage/storage.service.ts | 1 + 4 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/workbench/browser/src/app/pages/api/history/eo-history.component.ts b/src/workbench/browser/src/app/pages/api/history/eo-history.component.ts index 06b0b61cf..9e41ae70b 100644 --- a/src/workbench/browser/src/app/pages/api/history/eo-history.component.ts +++ b/src/workbench/browser/src/app/pages/api/history/eo-history.component.ts @@ -1,5 +1,7 @@ import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; +import { StorageService } from 'eo/workbench/browser/src/app/shared/services/storage'; +import { StorageRes, StorageResStatus } from 'eo/workbench/browser/src/app/shared/services/storage/index.model'; import { IndexedDBStorage } from '../../../../../../../workbench/browser/src/app/shared/services/storage/IndexedDB/lib/index'; import { MessageService } from '../../../shared/services/message'; @@ -11,24 +13,31 @@ import { MessageService } from '../../../shared/services/message'; export class HistoryComponent implements OnInit { historyList = []; colorHash = new Map().set('get', 'green').set('post', 'blue').set('delete', 'red').set('put', 'pink'); - constructor(public storageInstance: IndexedDBStorage, private router: Router, private message: MessageService) {} - ngOnInit() { - const observer = this.loadAllTest(); - observer.subscribe((result: any) => { - // console.log(result.data); - this.historyList = result.data.reverse(); - }); - this.message.get().subscribe(({ type }) => { + constructor(private storage: StorageService, private router: Router, private message: MessageService) {} + async ngOnInit() { + const result = await this.loadAllTest(); + this.historyList = result.reverse(); + console.log('this.historyList', this.historyList); + this.message.get().subscribe(async ({ type }) => { if (type === 'updateHistory') { - this.loadAllTest().subscribe((result: any) => { - this.historyList = result.data.reverse(); - }); + const data = await this.loadAllTest(); + this.historyList = data.reverse(); } }); } loadAllTest() { - return this.storageInstance.apiTestHistoryLoadAllByProjectID(1); + // return this.storageInstance.apiTestHistoryLoadAllByProjectID(1); + return new Promise((resolve) => { + this.storage.run('apiTestHistoryLoadAllByProjectID', [1], (result: StorageRes) => { + if (result.status === StorageResStatus.success) { + resolve(result.data); + } else { + console.error(result.data); + resolve(result.data); + } + }); + }); } methodColor(type) { @@ -38,7 +47,7 @@ export class HistoryComponent implements OnInit { gotoTestHistory(data) { this.router.navigate(['home/api/test'], { queryParams: { - uuid:`history_${data.uuid}` + uuid: `history_${data.uuid}`, }, }); } @@ -46,7 +55,14 @@ export class HistoryComponent implements OnInit { clearAllHistory() { const uuids = this.historyList.map((it) => it.uuid); this.historyList = []; - this.storageInstance.apiTestHistoryBulkRemove(uuids); + // this.storageInstance.apiTestHistoryBulkRemove(uuids); + this.storage.run('apiTestHistoryBulkRemove', [uuids], (result: StorageRes) => { + // if (result.status === StorageResStatus.success) { + // resolve(result.data); + // } else { + // console.error(result.data); + // } + }); } cancel() {} } diff --git a/src/workbench/browser/src/app/shared/services/storage/IndexedDB/lib/index.ts b/src/workbench/browser/src/app/shared/services/storage/IndexedDB/lib/index.ts index 680cd4180..bb72193ca 100644 --- a/src/workbench/browser/src/app/shared/services/storage/IndexedDB/lib/index.ts +++ b/src/workbench/browser/src/app/shared/services/storage/IndexedDB/lib/index.ts @@ -470,9 +470,9 @@ export class IndexedDBStorage extends Dexie implements StorageInterface { * * @param uuid */ - apiTestHistoryLoad(uuid: number | string): Observable { + apiTestHistoryLoad = (uuid: number | string): Observable => { return this.load(this.apiTestHistory, uuid); - } + }; /** * Load all apiTestHistory items by apiDataID. @@ -691,7 +691,7 @@ export class IndexedDBStorage extends Dexie implements StorageInterface { return new Observable((obs) => { const fun = async () => { const result = {}; - const tables = ['environment', 'group', 'project', 'apiData']; + const tables = ['environment', 'group', 'project', 'apiData']; for (let i = 0; i < tables.length; i++) { const tableName = tables[i]; if (tableName === 'project') { diff --git a/src/workbench/browser/src/app/shared/services/storage/http/lib/index.ts b/src/workbench/browser/src/app/shared/services/storage/http/lib/index.ts index b9332bdc2..9b0a63e53 100644 --- a/src/workbench/browser/src/app/shared/services/storage/http/lib/index.ts +++ b/src/workbench/browser/src/app/shared/services/storage/http/lib/index.ts @@ -117,9 +117,13 @@ export class HttpStorage implements StorageInterface { apiTestHistoryBulkRemove(uuids: Array) { return this.http.delete(`/api_test_history?uuids=[${uuids}]`) as Observable; } - apiTestHistoryLoad: (uuid: number | string) => Observable; + apiTestHistoryLoad(uuid: number | string) { + return this.http.get(`/api_test_history/${uuid}`) as Observable; + } apiTestHistoryBulkLoad: (uuids: Array) => Observable; - apiTestHistoryLoadAllByProjectID: (projectID: number | string) => Observable; + apiTestHistoryLoadAllByProjectID(projectID: number | string) { + return this.http.get(`/api_test_history?projectID=${projectID}`) as Observable; + } apiTestHistoryLoadAllByApiDataID(apiDataID: number | string) { return this.http.get(`/api_test_history?apiDataID=${apiDataID}`) as Observable; } diff --git a/src/workbench/browser/src/app/shared/services/storage/storage.service.ts b/src/workbench/browser/src/app/shared/services/storage/storage.service.ts index 43aaa6b63..85ab3af04 100644 --- a/src/workbench/browser/src/app/shared/services/storage/storage.service.ts +++ b/src/workbench/browser/src/app/shared/services/storage/storage.service.ts @@ -39,6 +39,7 @@ export class StorageService { data: undefined, callback, }; + console.log('this.instance', this.instance, action); if (!this.instance[action]) { throw Error(`Lack request API: ${action}`); }