diff --git a/server/src/runs/entities/run.entity.ts b/server/src/runs/entities/run.entity.ts new file mode 100644 index 0000000..3438ab1 --- /dev/null +++ b/server/src/runs/entities/run.entity.ts @@ -0,0 +1,32 @@ +import { PlayerRun } from "@prisma/client" + +export class RunEntity implements PlayerRun { + constructor(partial: Partial) { + 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 +} diff --git a/server/src/runs/runs.controller.spec.ts b/server/src/runs/runs.controller.spec.ts new file mode 100644 index 0000000..d103209 --- /dev/null +++ b/server/src/runs/runs.controller.spec.ts @@ -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) + }) + + it('should be defined', () => { + expect(controller).toBeDefined() + }) +}) diff --git a/server/src/runs/runs.controller.ts b/server/src/runs/runs.controller.ts new file mode 100644 index 0000000..726caed --- /dev/null +++ b/server/src/runs/runs.controller.ts @@ -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> { + const [runs, total] = await this.runsService.findAll(queryPagination) + return paginateOutput(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 { + return new RunEntity(await this.runsService.findOne(id)) + } +} diff --git a/server/src/runs/runs.module.ts b/server/src/runs/runs.module.ts new file mode 100644 index 0000000..5c3c533 --- /dev/null +++ b/server/src/runs/runs.module.ts @@ -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 {} diff --git a/server/src/runs/runs.service.spec.ts b/server/src/runs/runs.service.spec.ts new file mode 100644 index 0000000..9064af0 --- /dev/null +++ b/server/src/runs/runs.service.spec.ts @@ -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) + }) + + it('should be defined', () => { + expect(service).toBeDefined() + }) +}) diff --git a/server/src/runs/runs.service.ts b/server/src/runs/runs.service.ts new file mode 100644 index 0000000..1a06049 --- /dev/null +++ b/server/src/runs/runs.service.ts @@ -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 { + return await this.prisma.playerRun.findUnique({ where: { id } }) + } +}