From dbcc8e1912fa36f3565c4643df5809017eab177a Mon Sep 17 00:00:00 2001 From: Xicheng Guo Date: Wed, 13 Dec 2023 10:49:18 +0800 Subject: [PATCH] add create and deleteById --- src/__tests__/user-storage.test.ts | 21 ++++++++++---- src/request-lib/axios/axios-request.ts | 30 ++++++++++++++++---- src/request-lib/base/base-request.ts | 3 +- src/request-lib/wx/wx-request.ts | 8 +++++- src/storage-lib/base/base-storage.ts | 20 +++++++++++-- src/storage-lib/gitee/gitee-storage.ts | 39 +++++++++++++++++++++++--- 6 files changed, 101 insertions(+), 20 deletions(-) diff --git a/src/__tests__/user-storage.test.ts b/src/__tests__/user-storage.test.ts index 28b9f40..e7a1c25 100644 --- a/src/__tests__/user-storage.test.ts +++ b/src/__tests__/user-storage.test.ts @@ -3,12 +3,21 @@ import { UserStorage } from "./helper/user-storage"; describe('Test User Storage', () => { const userStorage = new UserStorage(); - test('Test User Storage', async () => { + test('Test deleteAll User', async () => { + await userStorage.deleteAll(); const detail = await userStorage.find(); - expect(detail.length).toEqual(1); + expect(detail.length).toEqual(0); + }); - const first = detail[0]; - const result = await userStorage.findById(first.id); - expect(detail[0]).toEqual(result); - }, 30000); + test('Test create & User & findById User', async () => { + await userStorage.create({ + name: 'test-user', + age: 18 + }); + const findResult = await userStorage.find(); + expect(findResult.length).toEqual(1); + + const findByIdResult = await userStorage.findById(findResult[0].id); + expect(findByIdResult).toEqual(findResult[0]); + }); }); \ No newline at end of file diff --git a/src/request-lib/axios/axios-request.ts b/src/request-lib/axios/axios-request.ts index 4c35822..8064c3f 100644 --- a/src/request-lib/axios/axios-request.ts +++ b/src/request-lib/axios/axios-request.ts @@ -25,11 +25,31 @@ export class AxiosRequest extends BaseRequest { }); } - post(url: string) { - this.axios.post(url, undefined, { - headers: { - 'Authorization': `Bearer ${this.accessToken}` - } + async post(url: string, data: any): Promise { + return new Promise((resolve, reject) => { + this.axios.post(url, data, { + headers: { + 'Authorization': `Bearer ${this.accessToken}` + } + }).then((res) => { + resolve(res.data); + }).catch(error => { + reject(error); + }); + }); + } + + async delete(url: string): Promise { + return new Promise((resolve, reject) => { + this.axios.delete(url, { + headers: { + 'Authorization': `Bearer ${this.accessToken}` + } + }).then(() => { + resolve(); + }).catch(error => { + reject(error); + }); }); } } diff --git a/src/request-lib/base/base-request.ts b/src/request-lib/base/base-request.ts index 5b26bdb..2670ba9 100644 --- a/src/request-lib/base/base-request.ts +++ b/src/request-lib/base/base-request.ts @@ -13,7 +13,8 @@ export abstract class BaseRequest { } abstract get(url: string): Promise; - abstract post(url: string): void; + abstract post(url: string, data: any): Promise; + abstract delete(url: string): Promise; async authenticate() { switch(this.options.storagePlatform) { diff --git a/src/request-lib/wx/wx-request.ts b/src/request-lib/wx/wx-request.ts index abb1c41..87672fb 100644 --- a/src/request-lib/wx/wx-request.ts +++ b/src/request-lib/wx/wx-request.ts @@ -29,7 +29,7 @@ export class WxRequest extends BaseRequest { }); } - post(url: string) { + // post(url: string) { // this.wx.request({ // url, // method: 'POST', @@ -37,5 +37,11 @@ export class WxRequest extends BaseRequest { // 'Authorization': `Bearer ${this.accessToken}` // } // }); + // } + post(url: string, data: any): Promise { + throw new Error('Method not implemented.'); + } + delete(url: string): Promise { + throw new Error('Method not implemented.'); } } diff --git a/src/storage-lib/base/base-storage.ts b/src/storage-lib/base/base-storage.ts index ef0fb84..3dffb6a 100644 --- a/src/storage-lib/base/base-storage.ts +++ b/src/storage-lib/base/base-storage.ts @@ -1,12 +1,26 @@ -import { BaseModel } from "./base-model"; import { BaseComment } from "./base-comment"; export abstract class BaseStorage { abstract findById(id: number): void; - abstract find(): void; + abstract find(): Promise; + abstract create(data: any): void; + abstract deleteById(id: number): Promise; + + async deleteAll() { + const resultList = await this.find(); + const res = resultList.map((item)=>(this.deleteById(item.id))); + return Promise.all(res); + } // 序列化: 将对象转换为字符串 - serialize() { } + serialize(obj: M): string { + try { + return JSON.stringify(obj); + } catch (error) { + console.error(error); + throw new Error(`can not serialize ${obj}`); + } + } // 反序列化: 将字符串转换为对象 deserialize(comment: BaseComment): M { diff --git a/src/storage-lib/gitee/gitee-storage.ts b/src/storage-lib/gitee/gitee-storage.ts index f097295..a236569 100644 --- a/src/storage-lib/gitee/gitee-storage.ts +++ b/src/storage-lib/gitee/gitee-storage.ts @@ -3,7 +3,7 @@ import { BaseComment } from "../base/base-comment"; import { BaseModel } from "../base/base-model"; import { BaseStorage } from "../base/base-storage"; -export abstract class GiteeStorage extends BaseStorage { +export class GiteeStorage extends BaseStorage { private endpoint: string; constructor(private request: BaseRequest, private issueNumber: string) { @@ -11,16 +11,47 @@ export abstract class GiteeStorage extends BaseStorage { this.endpoint = request.getEndpoint(); } + /** + * Find records by ID. + * @param id Indicates the ID of the record + * @returns Matches the record + */ async findById(id: number) { const url = `${this.endpoint}/issues/comments/${id}`; const response = await this.request.get(url); - return this.deserialize(response); + return this.deserialize(response); } - async find() { + /** + * Retrieves a list of comments from the Gitee storage. + * @returns {Promise} A promise that resolves to an array of deserialized comments. + */ + async find(): Promise { const url = `${this.endpoint}/issues/${this.issueNumber}/comments`; const response = await this.request.get(url); - return response.map((item)=>this.deserialize(item)); + return response.map((item)=>this.deserialize(item)); + } + + /** + * Creates a new record in the Gitee storage. + * + * @param data - The data for the new record, excluding the 'id', 'created_at', and 'updated_at' fields. + * @returns A promise that resolves to the created record. + */ + async create(data: Omit) { + const url = `${this.endpoint}/issues/${this.issueNumber}/comments`; + const response = await this.request.post(url, {body: this.serialize>(data)}); + return this.deserialize(response); + } + + /** + * Deletes a comment by its ID. + * @param id The ID of the comment to delete. + * @returns A Promise that resolves when the comment is successfully deleted. + */ + async deleteById(id: number): Promise { + const url = `${this.endpoint}/issues/comments/${id}`; + return await this.request.delete(url); } } \ No newline at end of file