Skip to content

Commit

Permalink
[add] models & controllers of Base, Activity Log (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
TechQuery authored Oct 15, 2024
1 parent 2e8da8f commit c5a8238
Show file tree
Hide file tree
Showing 13 changed files with 527 additions and 250 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kys-service",
"version": "0.7.0",
"version": "0.9.0",
"license": "AGPL-3.0",
"author": "[email protected]",
"description": "RESTful API service of KaiYuanShe",
Expand Down Expand Up @@ -32,8 +32,9 @@
"koa2-swagger-ui": "^5.10.0",
"koagger": "^0.3.0",
"koajax": "^3.0.2",
"marked": "^14.1.3",
"mobx": "^6.13.3",
"mobx-lark": "^1.1.1",
"mobx-lark": "^2.0.0-rc.3",
"mobx-restful": "^1.0.1",
"pg": "^8.13.0",
"pg-connection-string": "^2.7.0",
Expand Down
85 changes: 26 additions & 59 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

114 changes: 114 additions & 0 deletions src/controller/ActivityLog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { Get, JsonController, Param, QueryParams } from 'routing-controllers';
import { ResponseSchema } from 'routing-controllers-openapi';
import { FindOptionsWhere } from 'typeorm';

import {
ActivityLog,
ActivityLogFilter,
ActivityLogListChunk,
BaseFilter,
dataSource,
LogableTable,
Operation,
User,
UserRank,
UserRankListChunk
} from '../model';

const store = dataSource.getRepository(ActivityLog),
userStore = dataSource.getRepository(User),
userRankStore = dataSource.getRepository(UserRank);

@JsonController('/activity-log')
export class ActivityLogController {
static logCreate(
createdBy: User,
tableName: ActivityLog['tableName'],
recordId: number
) {
const operation = Operation.Create;

return store.save({ createdBy, operation, tableName, recordId });
}

static logUpdate(
createdBy: User,
tableName: ActivityLog['tableName'],
recordId: number
) {
const operation = Operation.Update;

return store.save({ createdBy, operation, tableName, recordId });
}

static logDelete(
createdBy: User,
tableName: ActivityLog['tableName'],
recordId: number
) {
const operation = Operation.Delete;

return store.save({ createdBy, operation, tableName, recordId });
}

@Get('/user-rank')
@ResponseSchema(UserRankListChunk)
async getUserRankList(@QueryParams() { pageSize, pageIndex }: BaseFilter) {
const skip = pageSize * (pageIndex - 1);

const [list, count] = await userRankStore.findAndCount({
order: { score: 'DESC' },
skip,
take: pageSize
});
for (let i = 0, item: UserRank; (item = list[i]); i++) {
item.rank = skip + i + 1;
item.user = await userStore.findOneBy({ id: item.userId });
}
return { list, count };
}

@Get('/user/:id')
@ResponseSchema(ActivityLogListChunk)
getUserList(
@Param('id') id: number,
@QueryParams() { operation, pageSize, pageIndex }: ActivityLogFilter
) {
return this.queryList(
{ operation, createdBy: { id } },
{ pageSize, pageIndex }
);
}

@Get('/:table/:id')
@ResponseSchema(ActivityLogListChunk)
getList(
@Param('table') tableName: keyof typeof LogableTable,
@Param('id') recordId: number,
@QueryParams() { operation, pageSize, pageIndex }: ActivityLogFilter
) {
return this.queryList(
{ operation, tableName, recordId },
{ pageSize, pageIndex }
);
}

async queryList(
where: FindOptionsWhere<ActivityLog>,
{ pageSize, pageIndex }: BaseFilter
) {
const [list, count] = await store.findAndCount({
where,
relations: ['createdBy'],
skip: pageSize * (pageIndex - 1),
take: pageSize
});

for (const activity of list)
activity.record = await dataSource
.getRepository<ActivityLog['record']>(activity.tableName)
.findOneBy({ id: activity.recordId });

return { list, count };
}
}
29 changes: 29 additions & 0 deletions src/controller/Base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { marked } from 'marked';
import { Controller, Get, HeaderParam, HttpCode } from 'routing-controllers';

import { isProduct } from '../utility';

@Controller()
export class BaseController {
static entryOf(host: string) {
host = 'http://' + host;

return `
- HTTP served at ${host}
- Swagger API served at ${host}/docs/
- Swagger API exposed at ${host}/docs/spec
${isProduct ? '' : `- Mock API served at ${host}/mock/`}
`;
}

@Get('/_health')
@HttpCode(200)
getHealthStatus() {
return '';
}

@Get()
getIndex(@HeaderParam('host') host: string) {
return marked(BaseController.entryOf(host));
}
}
Loading

0 comments on commit c5a8238

Please sign in to comment.