Gestion erreurs création/modification défi
This commit is contained in:
@ -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,
|
||||
|
Reference in New Issue
Block a user