Ajout endpoints tentatives de course

This commit is contained in:
Emmy D'Anello 2024-12-08 23:03:30 +01:00
parent 33689d9c76
commit 20b4f2e7e8
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
6 changed files with 142 additions and 0 deletions

View 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 joueurse 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
}

View 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()
})
})

View 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))
}
}

View 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 {}

View 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()
})
})

View 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 } })
}
}