This repository has been archived by the owner on Nov 10, 2023. It is now read-only.
-
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 Create question route and service (#16)
* feat: add create grading question proto and types * feat: add create workspace question proto and types * feat: add create grading question repository * feat: add create grading testcase repository * feat: add create grading question route and service * feat: create update workspace question repo * feat: add create workspace question service and route * feat: add guard for workspace owner only * feat: add gateway route and service for create question * feat: add header validation in create question route * feat: add response type of create question
- Loading branch information
1 parent
95ac788
commit 7b03201
Showing
23 changed files
with
337 additions
and
5 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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { BadRequestException, createParamDecorator, ExecutionContext } from '@nestjs/common'; | ||
import { plainToInstance } from 'class-transformer'; | ||
import { validate } from 'class-validator'; | ||
|
||
export const RequestHeader = createParamDecorator( | ||
async (value: any, ctx: ExecutionContext) => { | ||
const { headers } = ctx.switchToHttp().getRequest(); | ||
|
||
const dto = plainToInstance(value, headers, { excludeExtraneousValues: true }); | ||
|
||
const errors = await validate(dto); | ||
if (errors.length > 0) { | ||
throw new BadRequestException('Invalid request headers'); | ||
} | ||
|
||
return dto; | ||
}, | ||
); |
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,43 @@ | ||
import { QuestionLevel } from '@codern/internal'; | ||
import { Expose, Transform } from 'class-transformer'; | ||
import { | ||
IsDefined, IsEnum, IsInt, IsString, | ||
} from 'class-validator'; | ||
|
||
export class CreateQuestionDto { | ||
|
||
@IsString() | ||
@IsDefined() | ||
@Expose({ name: 'question-name' }) | ||
name!: string; | ||
|
||
@IsString() | ||
@IsDefined() | ||
@Expose({ name: 'question-description' }) | ||
description!: string; | ||
|
||
@Transform(({ value }) => Number.parseInt(value, 10)) | ||
@IsInt() | ||
@IsDefined() | ||
@Expose({ name: 'question-memory-limit' }) | ||
memoryLimit!: number; | ||
|
||
@Transform(({ value }) => Number.parseInt(value, 10)) | ||
@IsInt({ message: 'invalid request header `question-time-limit`' }) | ||
@IsDefined() | ||
@Expose({ name: 'question-time-limit' }) | ||
timeLimit!: number; | ||
|
||
@Transform(({ value }) => String(value).toUpperCase()) | ||
@IsEnum(QuestionLevel) | ||
@IsDefined() | ||
@Expose({ name: 'question-level' }) | ||
level!: QuestionLevel; | ||
|
||
@Transform(({ value }) => Number.parseInt(value, 10)) | ||
@IsInt() | ||
@IsDefined() | ||
@Expose({ name: 'question-score' }) | ||
score!: number; | ||
|
||
} |
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,37 @@ | ||
import { | ||
BadRequestException, | ||
CanActivate, ExecutionContext, Inject, Injectable, | ||
} from '@nestjs/common'; | ||
import { ClientGrpc } from '@nestjs/microservices'; | ||
import { FastifyRequest } from 'fastify'; | ||
import { firstValueFrom } from 'rxjs'; | ||
import { WorkspaceService } from '@/services/WorkspaceService'; | ||
|
||
type WorkspaceGuardParams = { | ||
workspaceId: string | undefined, | ||
questionId: string | undefined, | ||
}; | ||
|
||
@Injectable() | ||
export class WorkspaceOwnerGuard implements CanActivate { | ||
|
||
private readonly workspaceService: WorkspaceService; | ||
|
||
public constructor(@Inject('WORKSPACE_PACKAGE') client: ClientGrpc) { | ||
this.workspaceService = client.getService<WorkspaceService>('WorkspaceService'); | ||
} | ||
|
||
public async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const request = context.switchToHttp().getRequest<FastifyRequest>(); | ||
|
||
const { workspaceId } = request.params as WorkspaceGuardParams; | ||
if (!workspaceId) throw new BadRequestException(); | ||
|
||
const { workspace } = await firstValueFrom( | ||
this.workspaceService.getWorkspaceById({ workspaceId: Number(workspaceId) }), | ||
); | ||
|
||
return workspace.ownerId === request.user.id; | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { Prisma, Question } from '@prisma/client'; | ||
import { PrismaService } from '@/services/PrismaService'; | ||
|
||
@Injectable() | ||
export class QuestionRepository { | ||
|
||
private readonly prismaService: PrismaService; | ||
|
||
public constructor(prismaService: PrismaService) { | ||
this.prismaService = prismaService; | ||
} | ||
|
||
public async createQuestion(question: Prisma.QuestionCreateInput): Promise<Question> { | ||
const createdQuestion = await this.prismaService.question.create({ data: question }); | ||
|
||
return createdQuestion; | ||
} | ||
|
||
} |
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.