Ajout endpoints tentatives de course
This commit is contained in:
parent
33689d9c76
commit
20b4f2e7e8
32
server/src/runs/entities/run.entity.ts
Normal file
32
server/src/runs/entities/run.entity.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { PlayerRun } from "@prisma/client"
|
||||
|
||||
export class RunEntity implements PlayerRun {
|
||||
constructor(partial: Partial<RunEntity>) {
|
||||
Object.assign(this, partial)
|
||||
}
|
||||
|
||||
/**
|
||||
* Identifiant unique de la tentative
|
||||
*/
|
||||
id: number
|
||||
|
||||
/**
|
||||
* Identifant de l'objet de la partie
|
||||
*/
|
||||
gameId: number
|
||||
|
||||
/**
|
||||
* Identifiant de læ joueur⋅se actuellement en course
|
||||
*/
|
||||
runnerId: number
|
||||
|
||||
/**
|
||||
* Date et heure de début de course
|
||||
*/
|
||||
start: Date
|
||||
|
||||
/**
|
||||
* Date et heure de fin de course
|
||||
*/
|
||||
end: Date | null
|
||||
}
|
20
server/src/runs/runs.controller.spec.ts
Normal file
20
server/src/runs/runs.controller.spec.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing'
|
||||
import { RunsController } from './runs.controller'
|
||||
import { RunsService } from './runs.service'
|
||||
|
||||
describe('RunsController', () => {
|
||||
let controller: RunsController
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [RunsController],
|
||||
providers: [RunsService],
|
||||
}).compile()
|
||||
|
||||
controller = module.get<RunsController>(RunsController)
|
||||
})
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined()
|
||||
})
|
||||
})
|
40
server/src/runs/runs.controller.ts
Normal file
40
server/src/runs/runs.controller.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common'
|
||||
import { RunsService } from './runs.service'
|
||||
import { RunEntity } from './entities/run.entity'
|
||||
import { PaginateOutputDto } from 'src/common/dto/pagination-output.dto'
|
||||
import { QueryPaginationDto } from 'src/common/dto/pagination-query.dto'
|
||||
import { JwtAuthGuard } from 'src/auth/jwt-auth.guard'
|
||||
import { ApiBearerAuth } from '@nestjs/swagger'
|
||||
import { ApiOkResponsePaginated, paginateOutput } from 'src/common/utils/pagination.utils'
|
||||
|
||||
@Controller('runs')
|
||||
export class RunsController {
|
||||
constructor(private readonly runsService: RunsService) {}
|
||||
|
||||
/**
|
||||
* Recherche de tentatives de course
|
||||
*
|
||||
* @throws {401} Non authentifié⋅e
|
||||
*/
|
||||
@Get()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiOkResponsePaginated(RunEntity)
|
||||
async findAll(@Query() queryPagination: QueryPaginationDto): Promise<PaginateOutputDto<RunEntity>> {
|
||||
const [runs, total] = await this.runsService.findAll(queryPagination)
|
||||
return paginateOutput<RunEntity>(runs.map(challenge => new RunEntity(challenge)), total, queryPagination)
|
||||
}
|
||||
|
||||
/**
|
||||
* Recherche d'une tentative de course par identifiant
|
||||
*
|
||||
* @throws {401} Non authentifié⋅e
|
||||
* @throws {404} Essai non trouvé
|
||||
*/
|
||||
@Get(':id')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
async findOne(@Param('id') id: number): Promise<RunEntity> {
|
||||
return new RunEntity(await this.runsService.findOne(id))
|
||||
}
|
||||
}
|
11
server/src/runs/runs.module.ts
Normal file
11
server/src/runs/runs.module.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common'
|
||||
import { RunsService } from './runs.service'
|
||||
import { RunsController } from './runs.controller'
|
||||
import { PrismaModule } from 'src/prisma/prisma.module'
|
||||
|
||||
@Module({
|
||||
controllers: [RunsController],
|
||||
providers: [RunsService],
|
||||
imports: [PrismaModule],
|
||||
})
|
||||
export class RunsModule {}
|
18
server/src/runs/runs.service.spec.ts
Normal file
18
server/src/runs/runs.service.spec.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing'
|
||||
import { RunsService } from './runs.service'
|
||||
|
||||
describe('RunsService', () => {
|
||||
let service: RunsService
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [RunsService],
|
||||
}).compile()
|
||||
|
||||
service = module.get<RunsService>(RunsService)
|
||||
})
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined()
|
||||
})
|
||||
})
|
21
server/src/runs/runs.service.ts
Normal file
21
server/src/runs/runs.service.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { PlayerRun } from '@prisma/client'
|
||||
import { QueryPaginationDto } from 'src/common/dto/pagination-query.dto'
|
||||
import { paginate } from 'src/common/utils/pagination.utils'
|
||||
import { PrismaService } from 'src/prisma/prisma.service'
|
||||
|
||||
@Injectable()
|
||||
export class RunsService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
async findAll(queryPagination: QueryPaginationDto): Promise<[PlayerRun[], number]> {
|
||||
return [
|
||||
await this.prisma.playerRun.findMany({ ...paginate(queryPagination) }),
|
||||
await this.prisma.playerRun.count(),
|
||||
]
|
||||
}
|
||||
|
||||
async findOne(id: number): Promise<PlayerRun> {
|
||||
return await this.prisma.playerRun.findUnique({ where: { id } })
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user