-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from GuoXiCheng/dev-c
Dev c
- Loading branch information
Showing
9 changed files
with
62 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
module.exports = { | ||
testTimeout: 30000, | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
collectCoverageFrom: [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 5 additions & 6 deletions
11
src/storage-lib/gitee/gitee-response.ts → src/storage-lib/base/base-comment.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |