Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(be): check if score or score-weight is non-negative integer #2064

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/backend/apps/admin/src/contest/contest.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Module } from '@nestjs/common'
import { RolesModule } from '@libs/auth'
import { ProblemModule } from '@admin/problem/problem.module'
import { ContestResolver } from './contest.resolver'
import { ContestService } from './contest.service'

@Module({
imports: [RolesModule],
imports: [RolesModule, ProblemModule],
providers: [ContestService, ContestResolver]
})
export class ContestModule {}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Field, InputType, Int } from '@nestjs/graphql'
import { IntScoreScalar } from '@admin/problem/scalar/int-score.scalar'

@InputType()
export class ProblemScoreInput {
@Field(() => Int)
problemId: number

@Field(() => Int)
@Field(() => IntScoreScalar)
score: number
}
9 changes: 6 additions & 3 deletions apps/backend/apps/admin/src/problem/model/testcase.input.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { InputType, PickType } from '@nestjs/graphql'
import { Field, InputType, PickType } from '@nestjs/graphql'
import { ProblemTestcaseCreateInput } from '@admin/@generated'
import { IntScoreScalar } from '../scalar/int-score.scalar'

@InputType()
export class Testcase extends PickType(ProblemTestcaseCreateInput, [
'input',
'output',
'scoreWeight',
'isHidden'
]) {}
]) {
@Field(() => IntScoreScalar, { nullable: true })
scoreWeight?: number
}
7 changes: 5 additions & 2 deletions apps/backend/apps/admin/src/problem/problem.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
WorkbookProblemResolver
} from './problem.resolver'
import { ProblemService } from './problem.service'
import { IntScoreScalar } from './scalar/int-score.scalar'

@Module({
imports: [StorageModule, ConfigModule],
Expand All @@ -17,7 +18,9 @@ import { ProblemService } from './problem.service'
TagResolver,
ProblemService,
ContestProblemResolver,
WorkbookProblemResolver
]
WorkbookProblemResolver,
IntScoreScalar
],
exports: [IntScoreScalar]
})
export class ProblemModule {}
29 changes: 29 additions & 0 deletions apps/backend/apps/admin/src/problem/scalar/int-score.scalar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Scalar, type CustomScalar } from '@nestjs/graphql'
import { type ValueNode, Kind } from 'graphql'
import { UnprocessableDataException } from '@libs/exception'

@Scalar('IntScore', () => IntScoreScalar)
export class IntScoreScalar implements CustomScalar<number, number> {
parseValue(value: number) {
if (value < 0 || !Number.isInteger(value)) {
throw new UnprocessableDataException(
'Score(ScoreWeight) must be a non-negative integer.'
)
}
return value
}

serialize(value: number) {
return value
}

parseLiteral(ast: ValueNode) {
if (ast.kind === Kind.INT) {
const value = parseInt(ast.value, 10)
if (value >= 0) return value
}
throw new UnprocessableDataException(
'Score(ScoreWeight) must be a non-negative integer.'
)
}
}
3 changes: 2 additions & 1 deletion collection/admin/Problem/Create Problem/Succeed.bru
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ body:graphql:vars {
{
"input": "input",
"output": "output",
"isHidden": false
"isHidden": false,
"scoreWeight": 10
}
],
"tagIds": [1]
Expand Down