Skip to content

Commit

Permalink
Merge pull request #61 from GuoXiCheng/dev-c
Browse files Browse the repository at this point in the history
add user
  • Loading branch information
GuoXiCheng authored Jan 2, 2024
2 parents 3927fb9 + 5feb6c1 commit 1ee1e48
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tiny-crud",
"version": "1.0.17",
"version": "1.0.18",
"description": "",
"main": "dist/bundle.cjs.js",
"module": "dist/bundle.esm.js",
Expand Down
8 changes: 7 additions & 1 deletion src/__tests__/book-storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ describe('Test Book Storage', () => {
expect(findResult.length).toEqual(1);

const findByIdResult = await Book.findById(findResult[0].id);
expect(findByIdResult).toEqual(findResult[0]);
expect(findByIdResult.id).toEqual(findResult[0].id);
expect(findByIdResult.book_name).toEqual(findResult[0].book_name);
expect(findByIdResult.book_author).toEqual(findResult[0].book_author);
expect(findByIdResult.book_price).toEqual(findResult[0].book_price);
expect(findByIdResult.created_at).toEqual(findResult[0].created_at);
expect(findByIdResult.updated_at).toEqual(findResult[0].updated_at);
expect(findByIdResult.user).not.toBeNull();
});

test('Test updateById Book', async () => {
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/chat-storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ describe('Use Gitlab Test Chat Storage', () => {

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

test('Test updateById Chat', async () => {
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/user-storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe('Test User Storage', () => {

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

test('Test updateById User', async () => {
Expand Down
1 change: 1 addition & 0 deletions src/storage-lib/base/base-comment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BaseModel } from "./base-model";

export interface BaseComment extends BaseModel {
[x: string]: any;
body: string;
}
3 changes: 3 additions & 0 deletions src/storage-lib/base/base-model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { User } from "./user";

export interface BaseModel {
id: number;
created_at: string;
updated_at: string;
user: User;
}
6 changes: 5 additions & 1 deletion src/storage-lib/base/base-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BaseModel } from "./base-model";
import { BaseParams } from "./base-params";
import { PlainObject } from "./plain-object";
import { RouteType } from "./route-type";
import { User } from "./user";

export abstract class BaseStorage<T extends BaseModel> {
public readonly useEncrypt: boolean;
Expand All @@ -19,6 +20,8 @@ export abstract class BaseStorage<T extends BaseModel> {
this.decryptFn = request.decryptFn;
}

protected abstract extractUser(comment: BaseComment): User | null;

protected getRoute(routeType: keyof typeof RouteType, id?: number): string {
switch (routeType) {
case RouteType.find:
Expand Down Expand Up @@ -147,7 +150,8 @@ export abstract class BaseStorage<T extends BaseModel> {
} else {
obj = JSON.parse(body);
}
return {id, ...obj, created_at, updated_at}
const user = this.extractUser(comment);
return {id, ...obj, created_at, updated_at, user}
} catch (error) {
console.error(error);
throw new Error(`can not deserialize ${comment}`);
Expand Down
5 changes: 5 additions & 0 deletions src/storage-lib/base/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type User = {
id: number;
name: string;
avatar_url: string;
}
13 changes: 13 additions & 0 deletions src/storage-lib/gitee/gitee-storage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { BaseRequest } from "../../request-lib";
import { BaseComment } from "../base/base-comment";
import { BaseModel } from "../base/base-model";
import { BaseStorage } from "../base/base-storage";
import { IssueDetail } from "../base/issue-detail";
import { RouteType } from "../base/route-type";
import { User } from "../base/user";
import { GiteeDetail } from "./gitee-detail";
import { GiteeParams } from "./gitee-params";
import { GiteeUser } from "./gitee-user";

export class GiteeStorage<T extends BaseModel> extends BaseStorage<T> {

Expand Down Expand Up @@ -33,4 +36,14 @@ export class GiteeStorage<T extends BaseModel> extends BaseStorage<T> {
};
return result;
}

protected extractUser(comment: BaseComment): User | null {
const { user } = comment;
if (user) {
const { id, name, avatar_url } = user as GiteeUser;
return { id, name, avatar_url };
}
return null;
}

}
12 changes: 12 additions & 0 deletions src/storage-lib/github/github-storage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { BaseRequest } from "../../request-lib";
import { BaseComment } from "../base/base-comment";
import { BaseModel } from "../base/base-model";
import { BaseStorage } from "../base/base-storage";
import { IssueDetail } from "../base/issue-detail";
import { RouteType } from "../base/route-type";
import { User } from "../base/user";
import { GithubDetail } from "./github-detail";
import { GithubParams } from "./github-params";
import { GithubUser } from "./github-user";

export class GithubStorage<T extends BaseModel> extends BaseStorage<T> {

Expand Down Expand Up @@ -33,4 +36,13 @@ export class GithubStorage<T extends BaseModel> extends BaseStorage<T> {
};
return result;
}

protected extractUser(comment: BaseComment): User | null {
const { user } = comment;
if (user) {
const { id, login, avatar_url } = user as GithubUser;
return { id, name: login, avatar_url };
}
return null;
}
}
11 changes: 11 additions & 0 deletions src/storage-lib/gitlab/gitlab-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { BaseStorage } from "../base/base-storage";
import { IssueDetail } from "../base/issue-detail";
import { PlainObject } from "../base/plain-object";
import { RouteType } from "../base/route-type";
import { User } from "../base/user";
import { GitlabDetail } from "./gitlab-detail";
import { GitlabParams } from "./gitlab-params";
import { GitlabUser } from "./gitlab-user";

export class GitlabStorage<T extends BaseModel> extends BaseStorage<T> {

Expand Down Expand Up @@ -65,5 +67,14 @@ export class GitlabStorage<T extends BaseModel> extends BaseStorage<T> {
};
return result;
}

protected extractUser(comment: BaseComment): User | null {
const { author } = comment;
if (author) {
const { id, name, avatar_url } = author as GitlabUser;
return { id, name, avatar_url };
}
return null;
}

}

0 comments on commit 1ee1e48

Please sign in to comment.