Ajout endpoints défis
This commit is contained in:
@ -0,0 +1,20 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing'
|
||||
import { ChallengeActionsController } from './challenge-actions.controller'
|
||||
import { ChallengeActionsService } from './challenge-actions.service'
|
||||
|
||||
describe('ChallengeActionsController', () => {
|
||||
let controller: ChallengeActionsController
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [ChallengeActionsController],
|
||||
providers: [ChallengeActionsService],
|
||||
}).compile()
|
||||
|
||||
controller = module.get<ChallengeActionsController>(ChallengeActionsController)
|
||||
})
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(controller).toBeDefined()
|
||||
})
|
||||
})
|
76
server/src/challenge-actions/challenge-actions.controller.ts
Normal file
76
server/src/challenge-actions/challenge-actions.controller.ts
Normal file
@ -0,0 +1,76 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete, ParseIntPipe, HttpCode, UseGuards, Req, Query, NotFoundException } from '@nestjs/common'
|
||||
import { ChallengeActionsService } from './challenge-actions.service'
|
||||
import { AuthenticatedRequest, JwtAuthGuard } from 'src/auth/jwt-auth.guard'
|
||||
import { ApiBearerAuth, ApiCreatedResponse, ApiForbiddenResponse, ApiNotFoundResponse, ApiOkResponse, ApiUnauthorizedResponse } from '@nestjs/swagger'
|
||||
import { ChallengeActionEntity } from './entities/challenge-action.entity'
|
||||
import { CreateChallengeActionDto } from './dto/create-challenge-action.dto'
|
||||
import { ApiOkResponsePaginated, paginateOutput } from 'src/common/utils/pagination.utils'
|
||||
import { QueryPaginationDto } from 'src/common/dto/pagination-query.dto'
|
||||
import { PaginateOutputDto } from 'src/common/dto/pagination-output.dto'
|
||||
import { UpdateChallengeActionDto } from './dto/update-challenge-action.dto'
|
||||
|
||||
@Controller('challenge-actions')
|
||||
export class ChallengeActionsController {
|
||||
constructor(private readonly challengeActionsService: ChallengeActionsService) {}
|
||||
|
||||
@Post()
|
||||
@HttpCode(201)
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiCreatedResponse({ type: ChallengeActionEntity, description: "Objet créé avec succès" })
|
||||
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
|
||||
@ApiForbiddenResponse({ description: "Permission refusée" })
|
||||
async create(@Req() request: AuthenticatedRequest, @Body() createChallengeActionDto: CreateChallengeActionDto): Promise<ChallengeActionEntity> {
|
||||
const user = request.user
|
||||
const challenge = await this.challengeActionsService.create(user, createChallengeActionDto)
|
||||
return new ChallengeActionEntity(challenge)
|
||||
}
|
||||
|
||||
@Get()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiOkResponsePaginated(ChallengeActionEntity)
|
||||
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
|
||||
@ApiForbiddenResponse({ description: "Permission refusée" })
|
||||
async findAll(@Query() queryPagination?: QueryPaginationDto): Promise<PaginateOutputDto<ChallengeActionEntity>> {
|
||||
const [challengeActions, total] = await this.challengeActionsService.findAll(queryPagination)
|
||||
return paginateOutput<ChallengeActionEntity>(challengeActions.map(challengeAction => new ChallengeActionEntity(challengeAction)), total, queryPagination)
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiOkResponse({ type: ChallengeActionEntity })
|
||||
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
|
||||
@ApiForbiddenResponse({ description: "Permission refusée" })
|
||||
@ApiNotFoundResponse({ description: "Objet non trouvé" })
|
||||
async findOne(@Param('id', ParseIntPipe) id: number): Promise<ChallengeActionEntity> {
|
||||
const challenge = await this.challengeActionsService.findOne(id)
|
||||
if (!challenge)
|
||||
throw new NotFoundException(`Défi inexistant avec l'identifiant ${id}`)
|
||||
return new ChallengeActionEntity(challenge)
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiOkResponse({ type: ChallengeActionEntity })
|
||||
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
|
||||
@ApiForbiddenResponse({ description: "Permission refusée" })
|
||||
@ApiNotFoundResponse({ description: "Objet non trouvé" })
|
||||
async update(@Param('id', ParseIntPipe) id: number, @Body() updateChallengeActionDto: UpdateChallengeActionDto) {
|
||||
return await this.challengeActionsService.update(id, updateChallengeActionDto)
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@HttpCode(204)
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiOkResponse({ type: ChallengeActionEntity })
|
||||
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
|
||||
@ApiForbiddenResponse({ description: "Permission refusée" })
|
||||
@ApiNotFoundResponse({ description: "Objet non trouvé" })
|
||||
async remove(@Param('id', ParseIntPipe) id: number) {
|
||||
await this.challengeActionsService.remove(id)
|
||||
}
|
||||
}
|
11
server/src/challenge-actions/challenge-actions.module.ts
Normal file
11
server/src/challenge-actions/challenge-actions.module.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common'
|
||||
import { ChallengeActionsService } from './challenge-actions.service'
|
||||
import { ChallengeActionsController } from './challenge-actions.controller'
|
||||
import { PrismaModule } from 'src/prisma/prisma.module'
|
||||
|
||||
@Module({
|
||||
controllers: [ChallengeActionsController],
|
||||
providers: [ChallengeActionsService],
|
||||
imports: [PrismaModule],
|
||||
})
|
||||
export class ChallengeActionsModule {}
|
@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing'
|
||||
import { ChallengeActionsService } from './challenge-actions.service'
|
||||
|
||||
describe('ChallengeActionsService', () => {
|
||||
let service: ChallengeActionsService
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [ChallengeActionsService],
|
||||
}).compile()
|
||||
|
||||
service = module.get<ChallengeActionsService>(ChallengeActionsService)
|
||||
})
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined()
|
||||
})
|
||||
})
|
41
server/src/challenge-actions/challenge-actions.service.ts
Normal file
41
server/src/challenge-actions/challenge-actions.service.ts
Normal file
@ -0,0 +1,41 @@
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { CreateChallengeActionDto } from './dto/create-challenge-action.dto'
|
||||
import { UpdateChallengeActionDto } from './dto/update-challenge-action.dto'
|
||||
import { ChallengeAction, User } from '@prisma/client'
|
||||
import { PrismaService } from 'src/prisma/prisma.service'
|
||||
import { QueryPaginationDto } from 'src/common/dto/pagination-query.dto'
|
||||
import { paginate } from 'src/common/utils/pagination.utils'
|
||||
|
||||
@Injectable()
|
||||
export class ChallengeActionsService {
|
||||
constructor(private prisma: PrismaService) { }
|
||||
|
||||
async create(authenticatedUser: User, createChallengeActionDto: CreateChallengeActionDto): Promise<ChallengeAction> {
|
||||
const data = { ...createChallengeActionDto, userId: authenticatedUser.id }
|
||||
return await this.prisma.challengeAction.create({ data: data })
|
||||
}
|
||||
|
||||
async findAll(queryPagination?: QueryPaginationDto): Promise<[ChallengeAction[], number]> {
|
||||
return [
|
||||
await this.prisma.challengeAction.findMany({
|
||||
...paginate(queryPagination),
|
||||
}),
|
||||
await this.prisma.challenge.count(),
|
||||
]
|
||||
}
|
||||
|
||||
async findOne(id: number): Promise<ChallengeAction> {
|
||||
return await this.prisma.challengeAction.findUnique({ where: { id } })
|
||||
}
|
||||
|
||||
async update(id: number, updateChallengeActionDto: UpdateChallengeActionDto): Promise<ChallengeAction> {
|
||||
return await this.prisma.challengeAction.update({
|
||||
where: { id },
|
||||
data: updateChallengeActionDto,
|
||||
})
|
||||
}
|
||||
|
||||
async remove(id: number): Promise<ChallengeAction> {
|
||||
return await this.prisma.challengeAction.delete({ where: { id } })
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
import { ApiProperty } from "@nestjs/swagger"
|
||||
import { Type } from "class-transformer"
|
||||
import { IsBoolean, IsInt } from "class-validator"
|
||||
|
||||
export class CreateChallengeActionDto {
|
||||
@IsInt()
|
||||
@Type(() => Number)
|
||||
@ApiProperty({ description: "Identifiant du défi rattaché à l'action" })
|
||||
challengeId: number
|
||||
|
||||
@IsBoolean()
|
||||
@Type(() => Boolean)
|
||||
@ApiProperty({ description: "Est-ce que le défi est actuellement en train d'être réalisé" })
|
||||
active: boolean
|
||||
|
||||
@IsBoolean()
|
||||
@Type(() => Boolean)
|
||||
@ApiProperty({ description: "Est-ce que le défi a été réussi" })
|
||||
success: boolean
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/swagger'
|
||||
import { CreateChallengeActionDto } from './create-challenge-action.dto'
|
||||
|
||||
export class UpdateChallengeActionDto extends PartialType(CreateChallengeActionDto) {}
|
@ -0,0 +1,23 @@
|
||||
import { ApiProperty } from "@nestjs/swagger"
|
||||
import { ChallengeAction } from "@prisma/client"
|
||||
|
||||
export class ChallengeActionEntity implements ChallengeAction {
|
||||
constructor(partial: Partial<ChallengeActionEntity>) {
|
||||
Object.assign(this, partial)
|
||||
}
|
||||
|
||||
@ApiProperty({ description: "Identifiant unique" })
|
||||
id: number
|
||||
|
||||
@ApiProperty({ description: "Identifiant de l'utilisateur⋅rice effectuant le défi" })
|
||||
userId: number
|
||||
|
||||
@ApiProperty({ description: "Identifiant du défi rattaché à l'action" })
|
||||
challengeId: number
|
||||
|
||||
@ApiProperty({ description: "Est-ce que le défi est actuellement en train d'être réalisé" })
|
||||
active: boolean
|
||||
|
||||
@ApiProperty({ description: "Est-ce que le défi a été réussi" })
|
||||
success: boolean
|
||||
}
|
Reference in New Issue
Block a user