-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[add] models & controllers of Base, Activity Log (#28)
- Loading branch information
Showing
13 changed files
with
527 additions
and
250 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,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", | ||
|
@@ -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", | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -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 }; | ||
} | ||
} |
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 |
---|---|---|
@@ -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)); | ||
} | ||
} |
Oops, something went wrong.