Gestion erreurs création/modification défi

This commit is contained in:
2024-12-14 13:10:32 +01:00
parent 8878a13f4f
commit 9dfb2ba15d
2 changed files with 12 additions and 4 deletions

View File

@ -1,4 +1,4 @@
import { ConflictException, Injectable, NotFoundException } from '@nestjs/common'
import { BadRequestException, ConflictException, Injectable, NotFoundException } from '@nestjs/common'
import { CreateChallengeDto } from './dto/create-challenge.dto'
import { UpdateChallengeDto } from './dto/update-challenge.dto'
import { Challenge, Player } from '@prisma/client'
@ -13,6 +13,8 @@ export class ChallengesService {
async create(createChallengeDto: CreateChallengeDto): Promise<Challenge> {
const data = { ...createChallengeDto }
if (await this.prisma.challenge.findFirst({ where: { title: createChallengeDto.title } }))
throw new BadRequestException(`Un défi existe déjà avec pour titre « ${createChallengeDto.title} ».`)
return await this.prisma.challenge.create({ data: data })
}
@ -40,6 +42,8 @@ export class ChallengesService {
async update(id: number, updateChallengeDto: UpdateChallengeDto): Promise<Challenge> {
if (!await this.findOne(id))
throw new NotFoundException(`Aucun défi n'existe avec l'identifiant ${id}`)
if (await this.prisma.challenge.findFirst({ where: { title: updateChallengeDto.title, id: { not: id } } }))
throw new BadRequestException(`Un défi existe déjà avec pour titre « ${updateChallengeDto.title} ».`)
return await this.prisma.challenge.update({
where: { id },
data: updateChallengeDto,