Skip to content

Commit

Permalink
fix : get score logic (#59)
Browse files Browse the repository at this point in the history
* fix : get score logic

* fix : getscore logic
  • Loading branch information
lshtar13 authored Aug 26, 2023
1 parent 1160216 commit a1af2bc
Show file tree
Hide file tree
Showing 13 changed files with 203 additions and 90 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"minio": "^7.1.1",
"nestjs-minio": "^2.5.0",
"nestjs-minio-client": "^2.1.0",
"papaparse": "^5.4.1",
"passport": "^0.6.0",
"passport-local": "^1.0.0",
"reflect-metadata": "^0.1.13",
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

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

9 changes: 9 additions & 0 deletions prisma/migrations/20230826061858_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- AlterTable
ALTER TABLE "UserTestCase" ADD COLUMN "problemId" INTEGER,
ADD COLUMN "repoId" INTEGER;

-- AddForeignKey
ALTER TABLE "UserTestCase" ADD CONSTRAINT "UserTestCase_problemId_fkey" FOREIGN KEY ("problemId") REFERENCES "Problem"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "UserTestCase" ADD CONSTRAINT "UserTestCase_repoId_fkey" FOREIGN KEY ("repoId") REFERENCES "Repo"("id") ON DELETE SET NULL ON UPDATE CASCADE;
30 changes: 17 additions & 13 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ enum Role {
Student
}


enum TestcaseType {
OPENED
HIDDEN
Expand All @@ -31,16 +30,16 @@ model User {
score Score[]
UserGroup UserRepo[]
UserTestCase UserTestCase[]
}

model Repo {
id Int @id @default(autoincrement())
name String @unique @db.VarChar(255)
Problem Problem[]
UserRepo UserRepo[]
TestCase TestCase[]
Problem Problem[]
UserRepo UserRepo[]
TestCase TestCase[]
UserTestCase UserTestCase[]
}

model UserRepo {
Expand All @@ -64,14 +63,15 @@ model Score {
}

model Problem {
id Int @id @default(autoincrement())
title String @db.VarChar(255)
text String @db.Text
uuid String @default("null") @db.VarChar(255)
score Score[]
testCase TestCase[]
Repo Repo @relation(fields: [repoId], references: [id], onDelete: Cascade)
repoId Int
id Int @id @default(autoincrement())
title String @db.VarChar(255)
text String @db.Text
uuid String @default("null") @db.VarChar(255)
score Score[]
testCase TestCase[]
Repo Repo @relation(fields: [repoId], references: [id], onDelete: Cascade)
repoId Int
UserTestCase UserTestCase[]
}

model TestCase {
Expand All @@ -93,4 +93,8 @@ model UserTestCase {
userId Int
TestCase TestCase @relation(fields: [testCaseId], references: [id])
testCaseId Int
Problem Problem? @relation(fields: [problemId], references: [id])
problemId Int?
Repo Repo? @relation(fields: [repoId], references: [id])
repoId Int?
}
18 changes: 12 additions & 6 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ export class AuthService {
async validateUser(
username: string,
password: string
): Promise<{ userId: number }> {
): Promise<{ userId: number; username: string }> {
const user = await this.prisma.user.findUnique({
where: {
username
},
select: {
id: true,
password: true
password: true,
username: true
}
})

Expand All @@ -36,7 +37,8 @@ export class AuthService {
}

return {
userId: user.id
userId: user.id,
username: user.username
}
}

Expand All @@ -57,11 +59,14 @@ export class AuthService {
return user.role === Role.Tutor
}

async deSerializeUser(userId: number): Promise<{ userId: number }> {
async deSerializeUser(
userId: number
): Promise<{ userId: number; username: string }> {
const user = await this.prisma.user.findUnique({
where: { id: userId },
select: {
id: true
id: true,
username: true
}
})

Expand All @@ -70,7 +75,8 @@ export class AuthService {
}

return {
userId: user.id
userId: user.id,
username: user.username
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/auth/class/authenticated-user.class.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
export class AuthenticatedUser {
#userId: number
constructor(userId: number) {
#username: string

constructor(userId: number, username: string) {
this.#userId = userId
this.#username = username
}

get userId() {
return this.#userId
}

get username() {
return this.#username
}
}
4 changes: 3 additions & 1 deletion src/auth/serializer/local.serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export class LocalSerializer extends PassportSerializer {
async deserializeUser(userId: number, done: CallableFunction) {
return await this.authService
.deSerializeUser(userId)
.then((user) => done(null, new AuthenticatedUser(user.userId)))
.then((user) =>
done(null, new AuthenticatedUser(user.userId, user.username))
)
.catch((error) => done(error))
}
}
2 changes: 1 addition & 1 deletion src/auth/strategy/local.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class LocalStrategy extends PassportStrategy(Strategy) {
if (!user) {
throw new UnauthorizedException()
} else {
return new AuthenticatedUser(user.userId)
return new AuthenticatedUser(user.userId, user.username)
}
}
}
33 changes: 33 additions & 0 deletions src/submit/dto/score.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { IsNotEmpty, IsNumber, IsString } from 'class-validator'

export class ScoreDTO {
@IsNumber()
@IsNotEmpty()
problemId: number

@IsString()
@IsNotEmpty()
problemName: string

@IsNumber()
@IsNotEmpty()
pass: number

@IsNumber()
@IsNotEmpty()
total: number
}

export class ScoreCSVDTO {
@IsNumber()
@IsNotEmpty()
problemId: number

@IsString()
@IsNotEmpty()
problemName: string

@IsString()
@IsNotEmpty()
data: string
}
26 changes: 14 additions & 12 deletions src/submit/submit.controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Controller, Get, Param, ParseIntPipe, Req } from '@nestjs/common'
import { SubmitService } from './submit.service'
import { Roles } from 'src/common/decorator/roles.decorator'
import { Role, type Score } from '@prisma/client'
import { Role } from '@prisma/client'
import { AuthenticatedRequest } from 'src/common/interface/authenticated-request.interface'
import type { ScoreCSVDTO, ScoreDTO } from './dto/score.dto'

@Controller('submit')
export class SubmitController {
Expand All @@ -12,34 +13,35 @@ export class SubmitController {
async getMyScore(
@Param('problemId', ParseIntPipe) problemId: number,
@Req() req: AuthenticatedRequest
): Promise<Score> {
return await this.submitService.getScore(req.user.userId, problemId)
): Promise<ScoreDTO> {
return await this.submitService.getScore(req.user.username, problemId)
}

@Get('result/userId/:userId/problemId/:problemId')
@Get('result/username/:username/problemId/:problemId')
@Roles(Role.Tutor)
async getStudentScore(
@Param('userId', ParseIntPipe) userId: number,
@Param('username') username: string,
@Param('problemId', ParseIntPipe) problemId: number
): Promise<Score> {
return await this.submitService.getScore(userId, problemId)
): Promise<ScoreDTO> {
console.log(username, problemId)
return await this.submitService.getScore(username, problemId)
}

@Get('result/all/userId/:userId/reopId/:reopId')
@Get('result/all/username/:username/reopId/:reopId')
@Roles(Role.Tutor)
async getStudentScores(
@Param('userId', ParseIntPipe) userId: number,
@Param('username') username: string,
@Param('reopId', ParseIntPipe) reopId: number
): Promise<Score[]> {
return await this.submitService.getRepoScores(userId, reopId)
): Promise<ScoreDTO[]> {
return await this.submitService.getRepoScores(username, reopId)
}

@Get('result/all/problemId/:problemId/reopId/:reopId')
@Roles(Role.Tutor)
async getRepoProblemScores(
@Param('problemId', ParseIntPipe) problemId: number,
@Param('reopId', ParseIntPipe) repoId: number
): Promise<Score[]> {
): Promise<ScoreCSVDTO> {
return await this.submitService.getRepoProblemScores(problemId, repoId)
}
}
Loading

0 comments on commit a1af2bc

Please sign in to comment.