Skip to content

Commit

Permalink
Merge pull request #51 from GuoXiCheng/dev-c
Browse files Browse the repository at this point in the history
update storage
  • Loading branch information
GuoXiCheng authored Dec 25, 2023
2 parents f4bf608 + c09c44c commit 84b2891
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 69 deletions.
25 changes: 12 additions & 13 deletions src/__tests__/book-storage.test.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
import { BookStorage } from "./helper/book-storage";
import { Book } from "./helper/book-storage";

describe('Test Book Storage', () => {
const bookStorage = new BookStorage();

test('Test deleteAll Book', async () => {
await bookStorage.deleteAll();
const detail = await bookStorage.find();
await Book.deleteAll();
const detail = await Book.find();
expect(detail.length).toEqual(0);
});

test('Test create & findById Book', async () => {
await bookStorage.create({
await Book.create({
book_name: 'test-book',
book_author: 'test-author',
book_price: 100
});
const findResult = await bookStorage.find();
const findResult = await Book.find();
expect(findResult.length).toEqual(1);

const findByIdResult = await bookStorage.findById(findResult[0].id);
const findByIdResult = await Book.findById(findResult[0].id);
expect(findByIdResult).toEqual(findResult[0]);
});

test('Test updateById Book', async () => {
const findResult = await bookStorage.find();
const updateResult = await bookStorage.updateById(findResult[0].id, {
const findResult = await Book.find();
const updateResult = await Book.updateById(findResult[0].id, {
book_name: 'test-book-update',
book_author: 'test-author-update',
book_price: 200
});
const findByIdResult = await bookStorage.findById(findResult[0].id);
const findByIdResult = await Book.findById(findResult[0].id);
expect(updateResult.book_name).toEqual(findByIdResult.book_name);
expect(updateResult.book_author).toEqual(findByIdResult.book_author);
expect(updateResult.book_price).toEqual(findByIdResult.book_price);
Expand All @@ -40,7 +39,7 @@ describe('Test Book Storage', () => {

test('Test deleteById Book failed', async () => {
try {
await bookStorage.deleteById(123);
await Book.deleteById(123);
} catch (error: any) {
expect(error.response.data).toEqual({
"message": "Not Found",
Expand All @@ -51,7 +50,7 @@ describe('Test Book Storage', () => {

test('Test updateById Book failed', async () => {
try {
await bookStorage.updateById(123, {
await Book.updateById(123, {
book_name: 'test-book-update',
book_author: 'test-author-update',
book_price: 200
Expand All @@ -66,7 +65,7 @@ describe('Test Book Storage', () => {

test('Test findById Book failed', async () => {
try {
await bookStorage.findById(123);
await Book.findById(123);
} catch (error: any) {
expect(error.response.data).toEqual({
"message": "Not Found",
Expand Down
25 changes: 12 additions & 13 deletions src/__tests__/chat-storage.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { ChatStorage } from "./helper/chat-storage";
import { Chat } from "./helper/chat-storage";

describe('Use Gitlab Test Chat Storage', () => {
const chatStorage = new ChatStorage();

test('Test deleteAll Chat', async () => {
await chatStorage.deleteAll();
const detail = await chatStorage.find();
await Chat.deleteAll();
const detail = await Chat.find();
expect(detail.length).toEqual(0);
});

test('Test create & findById Chat', async () => {
await chatStorage.create({
await Chat.create({
participants: ['test-user', 'test-user-update'],
messages: [
{
Expand All @@ -20,16 +19,16 @@ describe('Use Gitlab Test Chat Storage', () => {
}
]
});
const findResult = await chatStorage.find();
const findResult = await Chat.find();
expect(findResult.length).toEqual(1);

const findByIdResult = await chatStorage.findById(findResult[0].id);
const findByIdResult = await Chat.findById(findResult[0].id);
expect(findByIdResult).toEqual(findResult[0]);
});

test('Test updateById Chat', async () => {
const findResult = await chatStorage.find();
const updateResult = await chatStorage.updateById(findResult[0].id, {
const findResult = await Chat.find();
const updateResult = await Chat.updateById(findResult[0].id, {
participants: ['test-user', 'test-user-update'],
messages: [
{
Expand All @@ -39,7 +38,7 @@ describe('Use Gitlab Test Chat Storage', () => {
}
]
});
const findByIdResult = await chatStorage.findById(findResult[0].id);
const findByIdResult = await Chat.findById(findResult[0].id);
expect(updateResult.participants).toEqual(findByIdResult.participants);
expect(updateResult.messages).toEqual(findByIdResult.messages);
expect(updateResult.id).toEqual(findByIdResult.id);
Expand All @@ -49,7 +48,7 @@ describe('Use Gitlab Test Chat Storage', () => {

test('Test deleteById Chat failed', async () => {
try {
await chatStorage.deleteById(123);
await Chat.deleteById(123);
} catch (error: any) {
expect(error.response.data).toEqual({
"message": "404 Not found"
Expand All @@ -59,7 +58,7 @@ describe('Use Gitlab Test Chat Storage', () => {

test('Test updateById Chat failed', async () => {
try {
await chatStorage.updateById(123, {
await Chat.updateById(123, {
participants: ['test-user', 'test-user-update'],
messages: [
{
Expand All @@ -78,7 +77,7 @@ describe('Use Gitlab Test Chat Storage', () => {

test('Test findById Chat failed', async () => {
try {
await chatStorage.findById(123);
await Chat.findById(123);
} catch (error: any) {
expect(error.response.data).toEqual({
"message": "404 Not found"
Expand Down
16 changes: 13 additions & 3 deletions src/__tests__/helper/book-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@ import { GithubStorage } from "../../storage-lib";
import { BookModel } from "./book-model";
import { GITHUB_NUMBER, githubRequest } from "./helper";

export class BookStorage extends GithubStorage<BookModel> {
constructor() {
class BookStorage extends GithubStorage<BookModel> {
private static instance: BookStorage;
private constructor() {
super(githubRequest, GITHUB_NUMBER);
}
}

public static getInstance(): BookStorage {
if (!BookStorage.instance) {
BookStorage.instance = new BookStorage();
}
return BookStorage.instance;
}
}

export const Book = BookStorage.getInstance();
16 changes: 13 additions & 3 deletions src/__tests__/helper/chat-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@ import { GitlabStorage } from "../../storage-lib";
import { ChatModel } from "./chat-model";
import { GITLAB_NUMBER, gitlabRequest } from "./helper";

export class ChatStorage extends GitlabStorage<ChatModel> {
constructor() {
class ChatStorage extends GitlabStorage<ChatModel> {
private static instance: ChatStorage;
private constructor() {
super(gitlabRequest, GITLAB_NUMBER);
}
}

public static getInstance(): ChatStorage {
if (!ChatStorage.instance) {
ChatStorage.instance = new ChatStorage();
}
return ChatStorage.instance;
}
}

export const Chat = ChatStorage.getInstance();
2 changes: 1 addition & 1 deletion src/__tests__/helper/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const giteeRequest = createRequest({
platform: 'gitee',
owner: process.env.TEST_GITEE_OWNER as string,
repo: process.env.TEST_GITEE_REPO as string,
useEncrypt: false,
useEncrypt: true,
encryptFn: (data: string) => {
return CryptoJS.AES.encrypt(data, ENCRYPT_KEY).toString();
},
Expand Down
15 changes: 12 additions & 3 deletions src/__tests__/helper/user-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@ import { GITEE_NUMBER, giteeRequest } from './helper';
import { UserModel } from './user-model';

export class UserStorage extends GiteeStorage<UserModel> {

constructor() {
private static instance: UserStorage;
private constructor() {
super(giteeRequest, GITEE_NUMBER);
}

public static getInstance(): UserStorage {
if (!UserStorage.instance) {
UserStorage.instance = new UserStorage();
}
return UserStorage.instance;
}

}
}

export const User = UserStorage.getInstance();
25 changes: 12 additions & 13 deletions src/__tests__/user-storage.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
import { UserStorage } from "./helper/user-storage";
import { User } from "./helper/user-storage";

describe('Test User Storage', () => {
const userStorage = new UserStorage();

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

test('Test create & findById User', async () => {
await userStorage.create({
await User.create({
name: 'test-user',
age: 18
});
const findResult = await userStorage.find();
const findResult = await User.find();
expect(findResult.length).toEqual(1);

const findByIdResult = await userStorage.findById(findResult[0].id);
const findByIdResult = await User.findById(findResult[0].id);
expect(findByIdResult).toEqual(findResult[0]);
});

test('Test updateById User', async () => {
const findResult = await userStorage.find();
const updateResult = await userStorage.updateById(findResult[0].id, {
const findResult = await User.find();
const updateResult = await User.updateById(findResult[0].id, {
name: 'test-user-update',
age: 20
});
const findByIdResult = await userStorage.findById(findResult[0].id);
const findByIdResult = await User.findById(findResult[0].id);
expect(updateResult.name).toEqual(findByIdResult.name);
expect(updateResult.age).toEqual(findByIdResult.age);
expect(updateResult.id).toEqual(findByIdResult.id);
Expand All @@ -37,15 +36,15 @@ describe('Test User Storage', () => {

test('Test deleteById User failed', async () => {
try {
await userStorage.deleteById(123);
await User.deleteById(123);
} catch (error: any) {
expect(error.response.data).toEqual({ message: '404 Not Found' });
}
});

test('Test updateById User failed', async () => {
try {
await userStorage.updateById(123, {
await User.updateById(123, {
name: 'test-user-update',
age: 20
});
Expand All @@ -56,7 +55,7 @@ describe('Test User Storage', () => {

test('Test findById User failed', async () => {
try {
await userStorage.findById(123);
await User.findById(123);
} catch (error: any) {
expect(error.response.data).toEqual({ message: '404 Not Found' });
}
Expand Down
24 changes: 12 additions & 12 deletions src/storage-lib/base/base-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ export abstract class BaseStorage<T extends BaseModel> {
this.decryptFn = request.decryptFn;
}

protected getRoute(routeType: RouteType, id?: number): string {
protected getRoute(routeType: keyof typeof RouteType, id?: number): string {
switch (routeType) {
case 'find':
case 'create':
case RouteType.find:
case RouteType.create:
return `${this.endpoint}/issues/${this.issueNumber}/comments`;
case 'findById':
case 'updateById':
case 'deleteById':
case RouteType.findById:
case RouteType.updateById:
case RouteType.deleteById:
return `${this.endpoint}/issues/comments/${id}`;
default:
throw new Error(`routeType ${routeType} is not supported`);
Expand All @@ -37,7 +37,7 @@ export abstract class BaseStorage<T extends BaseModel> {
* @returns A promise that resolves to an array of items.
*/
async find(): Promise<T[]> {
const url = this.getRoute('find');
const url = this.getRoute(RouteType.find);
const response = await this.request.get<BaseComment[]>(url);
return response.map((item) => this.deserialize<T>(item));
}
Expand All @@ -48,7 +48,7 @@ export abstract class BaseStorage<T extends BaseModel> {
* @returns Matches the record
*/
async findById(id: number) {
const url = this.getRoute('findById', id);
const url = this.getRoute(RouteType.findById, id);
const response = await this.request.get<BaseComment>(url);
return this.deserialize<T>(response);
}
Expand All @@ -60,7 +60,7 @@ export abstract class BaseStorage<T extends BaseModel> {
* @returns A promise that resolves to the created record.
*/
async create(data: PlainObject<T>): Promise<T> {
const url = this.getRoute('create');
const url = this.getRoute(RouteType.create);
const body = this.serialize<PlainObject<T>>(data);
const response = await this.request.post<BaseComment>(url, body);
return this.deserialize<T>(response);
Expand All @@ -73,7 +73,7 @@ export abstract class BaseStorage<T extends BaseModel> {
* @returns A promise that resolves to the updated record.
*/
async updateById(id: number, data: PlainObject<T>): Promise<T> {
const url = this.getRoute('updateById', id);
const url = this.getRoute(RouteType.updateById, id);
const body = this.serialize<PlainObject<T>>(data);
const response = await this.request.patch<BaseComment>(url, body);
return this.deserialize<T>(response);
Expand All @@ -85,7 +85,7 @@ export abstract class BaseStorage<T extends BaseModel> {
* @returns A Promise that resolves when the item is successfully deleted.
*/
async deleteById(id: number): Promise<void> {
const url = this.getRoute('deleteById', id);
const url = this.getRoute(RouteType.deleteById, id);
await this.request.delete(url);
}

Expand All @@ -94,7 +94,7 @@ export abstract class BaseStorage<T extends BaseModel> {
* @returns A Promise that resolves when all items are deleted.
*/
async deleteAll(): Promise<void> {
const findUrl = this.getRoute('find');
const findUrl = this.getRoute(RouteType.find);
const findResult = await this.request.get<BaseComment[]>(findUrl);
await Promise.all(findResult.map((item) => this.deleteById(item.id)));
}
Expand Down
8 changes: 7 additions & 1 deletion src/storage-lib/base/route-type.ts
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
export type RouteType = 'find' | 'findById' | 'create' | 'updateById' | 'deleteById';
export enum RouteType {
find = 'find',
findById = 'findById',
create = 'create',
updateById = 'updateById',
deleteById = 'deleteById'
}
Loading

0 comments on commit 84b2891

Please sign in to comment.