Skip to content

Commit

Permalink
Merge pull request #37 from GuoXiCheng/dev-c
Browse files Browse the repository at this point in the history
add create and deleteById
  • Loading branch information
GuoXiCheng authored Dec 13, 2023
2 parents 7038f63 + dbcc8e1 commit 341522f
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 20 deletions.
21 changes: 15 additions & 6 deletions src/__tests__/user-storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});
});
30 changes: 25 additions & 5 deletions src/request-lib/axios/axios-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,31 @@ export class AxiosRequest extends BaseRequest {
});
}

post(url: string) {
this.axios.post(url, undefined, {
headers: {
'Authorization': `Bearer ${this.accessToken}`
}
async post<T>(url: string, data: any): Promise<T> {
return new Promise((resolve, reject) => {
this.axios.post<T>(url, data, {
headers: {
'Authorization': `Bearer ${this.accessToken}`
}
}).then((res) => {
resolve(res.data);
}).catch(error => {
reject(error);
});
});
}

async delete<T>(url: string): Promise<void> {
return new Promise((resolve, reject) => {
this.axios.delete<T>(url, {
headers: {
'Authorization': `Bearer ${this.accessToken}`
}
}).then(() => {
resolve();
}).catch(error => {
reject(error);
});
});
}
}
3 changes: 2 additions & 1 deletion src/request-lib/base/base-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export abstract class BaseRequest {
}

abstract get<T>(url: string): Promise<T>;
abstract post(url: string): void;
abstract post<T>(url: string, data: any): Promise<T>;
abstract delete<T>(url: string): Promise<void>;

async authenticate() {
switch(this.options.storagePlatform) {
Expand Down
8 changes: 7 additions & 1 deletion src/request-lib/wx/wx-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ export class WxRequest extends BaseRequest {
});
}

post(url: string) {
// post(url: string) {
// this.wx.request({
// url,
// method: 'POST',
// header: {
// 'Authorization': `Bearer ${this.accessToken}`
// }
// });
// }
post<T>(url: string, data: any): Promise<T> {
throw new Error('Method not implemented.');
}
delete<T>(url: string): Promise<void> {
throw new Error('Method not implemented.');
}
}
20 changes: 17 additions & 3 deletions src/storage-lib/base/base-storage.ts
Original file line number Diff line number Diff line change
@@ -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<any[]>;
abstract create(data: any): void;
abstract deleteById(id: number): Promise<void>;

async deleteAll() {
const resultList = await this.find();
const res = resultList.map((item)=>(this.deleteById(item.id)));
return Promise.all(res);
}

// 序列化: 将对象转换为字符串
serialize() { }
serialize<M>(obj: M): string {
try {
return JSON.stringify(obj);
} catch (error) {
console.error(error);
throw new Error(`can not serialize ${obj}`);
}
}

// 反序列化: 将字符串转换为对象
deserialize<M>(comment: BaseComment): M {
Expand Down
39 changes: 35 additions & 4 deletions src/storage-lib/gitee/gitee-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,55 @@ import { BaseComment } from "../base/base-comment";
import { BaseModel } from "../base/base-model";
import { BaseStorage } from "../base/base-storage";

export abstract class GiteeStorage<M extends BaseModel> extends BaseStorage {
export class GiteeStorage<T extends BaseModel> extends BaseStorage {
private endpoint: string;

constructor(private request: BaseRequest, private issueNumber: string) {
super();
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<BaseComment>(url);
return this.deserialize<M>(response);
return this.deserialize<T>(response);
}

async find() {
/**
* Retrieves a list of comments from the Gitee storage.
* @returns {Promise<T[]>} A promise that resolves to an array of deserialized comments.
*/
async find(): Promise<T[]> {
const url = `${this.endpoint}/issues/${this.issueNumber}/comments`;
const response = await this.request.get<BaseComment[]>(url);
return response.map((item)=>this.deserialize<M>(item));
return response.map((item)=>this.deserialize<T>(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<T, 'id' | 'created_at' | 'updated_at'>) {
const url = `${this.endpoint}/issues/${this.issueNumber}/comments`;
const response = await this.request.post<BaseComment>(url, {body: this.serialize<Omit<T, 'id' | 'created_at' | 'updated_at'>>(data)});
return this.deserialize<T>(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<void> {
const url = `${this.endpoint}/issues/comments/${id}`;
return await this.request.delete(url);
}

}

0 comments on commit 341522f

Please sign in to comment.