Skip to content

Commit

Permalink
Merge pull request #36 from GuoXiCheng/dev-c
Browse files Browse the repository at this point in the history
Dev c
  • Loading branch information
GuoXiCheng authored Dec 12, 2023
2 parents 1b5dafb + 82617a9 commit 7038f63
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 33 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
testTimeout: 30000,
preset: 'ts-jest',
testEnvironment: 'node',
collectCoverageFrom: [
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/helper/user-model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseModel } from '../../storage-lib/base/base-model';
export class UserModel extends BaseModel {
export interface UserModel extends BaseModel {
name: string;
age: number;
}
3 changes: 2 additions & 1 deletion src/__tests__/helper/user-storage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { GiteeStorage } from '../../storage-lib/gitee/gitee-storage';
import { StartTest } from './start-test';
import { UserModel } from './user-model';

export class UserStorage extends GiteeStorage {
export class UserStorage extends GiteeStorage<UserModel> {
constructor() {
super(StartTest.createGiteeRequest(), StartTest.getGiteeIssueNumber());
}
Expand Down
15 changes: 11 additions & 4 deletions src/__tests__/user-storage.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { UserStorage } from "./helper/user-storage";

test('Test Storage Lib', async () => {
describe('Test User Storage', () => {
const userStorage = new UserStorage();
const detail = await userStorage.find();
expect(detail.length).toBeGreaterThan(0);
}, 30000);

test('Test User Storage', async () => {
const detail = await userStorage.find();
expect(detail.length).toEqual(1);

const first = detail[0];
const result = await userStorage.findById(first.id);
expect(detail[0]).toEqual(result);
}, 30000);
});
2 changes: 1 addition & 1 deletion src/request-lib/base/base-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export abstract class BaseRequest {
}
}

getUrlPrefix() {
getEndpoint() {
switch(this.options.storagePlatform) {
case StoragePlatform.gitee:
return `${this.baseUrl}/api/v5/repos/${this.options.owner}/${this.options.repo}`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export interface GiteeResponse {
id: number;
body: string;
user: object;
created_at: string;
updated_at: string;
export interface BaseComment {
id: number;
body: string;
created_at: string;
updated_at: string;
}
8 changes: 4 additions & 4 deletions src/storage-lib/base/base-model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export class BaseModel {
id!: number;
created_at!: string;
updated_at!: string;
export interface BaseModel {
id: number;
created_at: string;
updated_at: string;
}
24 changes: 21 additions & 3 deletions src/storage-lib/base/base-storage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
export interface BaseStorage {
findById(): void;
find(): void;
import { BaseModel } from "./base-model";
import { BaseComment } from "./base-comment";

export abstract class BaseStorage {
abstract findById(id: number): void;
abstract find(): void;

// 序列化: 将对象转换为字符串
serialize() { }

// 反序列化: 将字符串转换为对象
deserialize<M>(comment: BaseComment): M {
try {
const {id, body, created_at, updated_at} = comment;
const obj = JSON.parse(body);
return {id, ...obj, created_at, updated_at}
} catch (error) {
console.error(error);
throw new Error(`can not deserialize ${comment}`);
}
}
}
29 changes: 16 additions & 13 deletions src/storage-lib/gitee/gitee-storage.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { BaseRequest } from "../../request-lib";
import { BaseComment } from "../base/base-comment";
import { BaseModel } from "../base/base-model";
import { BaseStorage } from "../base/base-storage";
import { GiteeResponse } from "./gitee-response";

export abstract class GiteeStorage implements BaseStorage {
constructor(private request: BaseRequest, private issueNumber: string) { }
export abstract class GiteeStorage<M extends BaseModel> extends BaseStorage {
private endpoint: string;

async findById(): Promise<void> {
this.request.get("");
constructor(private request: BaseRequest, private issueNumber: string) {
super();
this.endpoint = request.getEndpoint();
}

async findById(id: number) {
const url = `${this.endpoint}/issues/comments/${id}`;
const response = await this.request.get<BaseComment>(url);
return this.deserialize<M>(response);
}

async find() {
const url = `${this.request.getUrlPrefix()}/issues/${this.issueNumber}/comments`;
const response = await this.request.get<GiteeResponse[]>(url);
return response.map((item: { id: any; body: string; created_at: any; updated_at: any; }) => ({
id: item.id,
...JSON.parse(item.body),
created_at: item.created_at,
updated_at: item.updated_at
}));
const url = `${this.endpoint}/issues/${this.issueNumber}/comments`;
const response = await this.request.get<BaseComment[]>(url);
return response.map((item)=>this.deserialize<M>(item));
}

}

0 comments on commit 7038f63

Please sign in to comment.