traintrape-moi/server/src/challenge-actions/challenge-actions.service.ts

51 lines
1.8 KiB
TypeScript

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'
import { FilterChallengeActionsDto } from './dto/filter-challenge-action.dto'
@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, filterChallengeActions: FilterChallengeActionsDto): Promise<[ChallengeAction[], number]> {
console.log(filterChallengeActions)
return [
await this.prisma.challengeAction.findMany({
...paginate(queryPagination),
where: filterChallengeActions,
}),
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 },
})
}
}