import { Injectable, NotFoundException } from '@nestjs/common' import { CreateMoneyUpdateDto } from './dto/create-money-update.dto' import { UpdateMoneyUpdateDto } from './dto/update-money-update.dto' import { PrismaService } from 'src/prisma/prisma.service' import { MoneyUpdate, Player } from '@prisma/client' import { QueryPaginationDto } from 'src/common/dto/pagination-query.dto' import { PlayerFilterDto } from 'src/common/dto/player_filter.dto' import { paginate } from 'src/common/utils/pagination.utils' @Injectable() export class MoneyUpdatesService { constructor(private prisma: PrismaService) { } async create(player: Player, createMoneyUpdateDto: CreateMoneyUpdateDto): Promise { return await this.prisma.moneyUpdate.create({ data: { ...createMoneyUpdateDto, playerId: player.id, } }) } async findAll(queryPagination: QueryPaginationDto, playerFilter: PlayerFilterDto): Promise<[MoneyUpdate[], number]> { return [ await this.prisma.moneyUpdate.findMany({ where: playerFilter, ...paginate(queryPagination), }), await this.prisma.moneyUpdate.count({ where: playerFilter, }), ] } async findOne(id: number): Promise { return await this.prisma.moneyUpdate.findUnique({ where: { id }, }) } async update(id: number, updateMoneyUpdateDto: UpdateMoneyUpdateDto): Promise { if (!this.findOne(id)) throw new NotFoundException(`Aucune modification de solde n'existe avec l'identifiant ${id}`) return await this.prisma.moneyUpdate.update({ where: { id }, data: updateMoneyUpdateDto, }) } async remove(id: number): Promise { if (!this.findOne(id)) throw new NotFoundException(`Aucune modification de solde n'existe avec l'identifiant ${id}`) return await this.prisma.moneyUpdate.delete({ where: { id }, }) } }