generated from idea2app/REST-Node-ts
-
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.
[add] models & controllers of Standard & Evaluation (#16)
- Loading branch information
Showing
12 changed files
with
296 additions
and
83 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
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": "@kaiyuanshe/openhackathon-service", | ||
"version": "0.20.0", | ||
"version": "0.21.0", | ||
"license": "LGPL-3.0", | ||
"author": "[email protected]", | ||
"description": "RESTful API service scaffold based on Node.js & TypeScript", | ||
|
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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { | ||
Authorized, | ||
Body, | ||
CurrentUser, | ||
ForbiddenError, | ||
Get, | ||
HttpCode, | ||
JsonController, | ||
NotFoundError, | ||
Param, | ||
Post, | ||
QueryParams | ||
} from 'routing-controllers'; | ||
import { ResponseSchema } from 'routing-controllers-openapi'; | ||
|
||
import { | ||
BaseFilter, | ||
dataSource, | ||
Evaluation, | ||
EvaluationListChunk, | ||
Team, | ||
User | ||
} from '../model'; | ||
import { searchConditionOf } from '../utility'; | ||
import { ActivityLogController } from './ActivityLog'; | ||
import { HackathonController } from './Hackathon'; | ||
|
||
const store = dataSource.getRepository(Evaluation), | ||
teamStore = dataSource.getRepository(Team); | ||
|
||
@JsonController('/hackathon/:name/team/:tid/evaluation') | ||
export class EvaluationController { | ||
@Post() | ||
@Authorized() | ||
@HttpCode(201) | ||
@ResponseSchema(Evaluation) | ||
async createOne( | ||
@CurrentUser() createdBy: User, | ||
@Param('name') name: string, | ||
@Param('tid') tid: number, | ||
@Body() evaluation: Evaluation | ||
) { | ||
const team = await teamStore.findOne({ | ||
where: { id: tid }, | ||
relations: ['hackathon'] | ||
}); | ||
if (!team) throw new NotFoundError(); | ||
|
||
const { hackathon } = team, | ||
now = Date.now(); | ||
if ( | ||
now < +new Date(hackathon.judgeStartedAt) || | ||
now > +new Date(hackathon.judgeEndedAt) | ||
) | ||
throw new ForbiddenError('Not in evaluation period'); | ||
|
||
await HackathonController.ensureJudge(createdBy.id, name); | ||
|
||
const saved = await store.save({ | ||
...evaluation, | ||
team, | ||
hackathon: team.hackathon, | ||
createdBy | ||
}); | ||
await ActivityLogController.logCreate( | ||
createdBy, | ||
'Evaluation', | ||
saved.id | ||
); | ||
return saved; | ||
} | ||
|
||
@Get() | ||
@ResponseSchema(EvaluationListChunk) | ||
async getList( | ||
@Param('tid') tid: number, | ||
@QueryParams() { keywords, pageSize, pageIndex }: BaseFilter | ||
) { | ||
const where = searchConditionOf<Evaluation>( | ||
['scores', 'comment'], | ||
keywords, | ||
{ team: { id: tid } } | ||
); | ||
const [list, count] = await store.findAndCount({ | ||
where, | ||
skip: pageSize * (pageIndex - 1), | ||
take: pageSize | ||
}); | ||
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
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
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
Oops, something went wrong.