From 4486e99225f8cabe5b656d249f8803b150688e25 Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Sat, 7 Dec 2024 11:05:32 +0100 Subject: [PATCH] Ajout des endpoints API User --- server/src/app.module.ts | 3 ++- server/src/prisma/prisma.service.ts | 3 ++- server/src/users/entities/user.entity.ts | 16 ++++++++++++++++ server/src/users/users.controller.spec.ts | 20 ++++++++++++++++++++ server/src/users/users.controller.ts | 21 +++++++++++++++++++++ server/src/users/users.module.ts | 11 +++++++++++ server/src/users/users.service.spec.ts | 18 ++++++++++++++++++ server/src/users/users.service.ts | 15 +++++++++++++++ 8 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 server/src/users/entities/user.entity.ts create mode 100644 server/src/users/users.controller.spec.ts create mode 100644 server/src/users/users.controller.ts create mode 100644 server/src/users/users.module.ts create mode 100644 server/src/users/users.service.spec.ts create mode 100644 server/src/users/users.service.ts diff --git a/server/src/app.module.ts b/server/src/app.module.ts index c93660b..b00281e 100644 --- a/server/src/app.module.ts +++ b/server/src/app.module.ts @@ -1,9 +1,10 @@ import { Module } from '@nestjs/common' import { PrismaService } from './prisma/prisma.service' import { PrismaModule } from './prisma/prisma.module' +import { UsersModule } from './users/users.module' @Module({ - imports: [PrismaModule], + imports: [PrismaModule, UsersModule], providers: [PrismaService], }) export class AppModule {} diff --git a/server/src/prisma/prisma.service.ts b/server/src/prisma/prisma.service.ts index ff36ace..a18770c 100644 --- a/server/src/prisma/prisma.service.ts +++ b/server/src/prisma/prisma.service.ts @@ -1,4 +1,5 @@ import { Injectable } from '@nestjs/common' +import { PrismaClient } from '@prisma/client' @Injectable() -export class PrismaService {} +export class PrismaService extends PrismaClient {} diff --git a/server/src/users/entities/user.entity.ts b/server/src/users/entities/user.entity.ts new file mode 100644 index 0000000..4502acc --- /dev/null +++ b/server/src/users/entities/user.entity.ts @@ -0,0 +1,16 @@ +import { ApiProperty } from "@nestjs/swagger"; +import { User } from "@prisma/client"; + +export class UserEntity implements User { + @ApiProperty({description: "Identifiant unique"}) + id: number + + @ApiProperty({description: "Nom de læ joueur⋅se"}) + name: string + + @ApiProperty({description: "Nombre de jetons dont dispose actuellement læ joueur⋅se"}) + money: number + + @ApiProperty({description: "Est-ce que cet⋅te joueur⋅se est cellui actuellement en course"}) + currentRunner: boolean +} diff --git a/server/src/users/users.controller.spec.ts b/server/src/users/users.controller.spec.ts new file mode 100644 index 0000000..a76d310 --- /dev/null +++ b/server/src/users/users.controller.spec.ts @@ -0,0 +1,20 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { UsersController } from './users.controller'; +import { UsersService } from './users.service'; + +describe('UsersController', () => { + let controller: UsersController; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [UsersController], + providers: [UsersService], + }).compile(); + + controller = module.get(UsersController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); +}); diff --git a/server/src/users/users.controller.ts b/server/src/users/users.controller.ts new file mode 100644 index 0000000..f602df0 --- /dev/null +++ b/server/src/users/users.controller.ts @@ -0,0 +1,21 @@ +import { Controller, Get, Param } from '@nestjs/common' +import { UsersService } from './users.service' +import { ApiOkResponse } from '@nestjs/swagger' +import { UserEntity } from './entities/user.entity' + +@Controller('users') +export class UsersController { + constructor(private readonly usersService: UsersService) {} + + @Get() + @ApiOkResponse({ type: UserEntity, isArray: true }) + findAll() { + return this.usersService.findAll() + } + + @Get(':id') + @ApiOkResponse({ type: UserEntity }) + findOne(@Param('id') id: string) { + return this.usersService.findOne(+id) + } +} diff --git a/server/src/users/users.module.ts b/server/src/users/users.module.ts new file mode 100644 index 0000000..9dfdd54 --- /dev/null +++ b/server/src/users/users.module.ts @@ -0,0 +1,11 @@ +import { Module } from '@nestjs/common' +import { UsersService } from './users.service' +import { UsersController } from './users.controller' +import { PrismaModule } from 'src/prisma/prisma.module' + +@Module({ + controllers: [UsersController], + providers: [UsersService], + imports: [PrismaModule], +}) +export class UsersModule {} diff --git a/server/src/users/users.service.spec.ts b/server/src/users/users.service.spec.ts new file mode 100644 index 0000000..b87ef3c --- /dev/null +++ b/server/src/users/users.service.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing' +import { UsersService } from './users.service' + +describe('UsersService', () => { + let service: UsersService + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [UsersService], + }).compile() + + service = module.get(UsersService) + }) + + it('should be defined', () => { + expect(service).toBeDefined() + }) +}) diff --git a/server/src/users/users.service.ts b/server/src/users/users.service.ts new file mode 100644 index 0000000..47720ec --- /dev/null +++ b/server/src/users/users.service.ts @@ -0,0 +1,15 @@ +import { Injectable } from '@nestjs/common' +import { PrismaService } from 'src/prisma/prisma.service' + +@Injectable() +export class UsersService { + constructor(private prisma: PrismaService) {} + + findAll() { + return this.prisma.user.findMany() + } + + findOne(id: number) { + return this.prisma.user.findUnique({ where: { id } }) + } +}