import { Injectable, InternalServerErrorException } from '@nestjs/common'; import { PrismaService } from '../prisma/prisma.service'; import { CreateProductDto } from './dto/create-product.dto'; import { UpdateProductDto } from './dto/update-product.dto'; import { Logger } from '@nestjs/common'; @Injectable() export class ProductsService { private readonly logger: Logger = new Logger('products resolver'); constructor(private prisma: PrismaService) {} async create(createProductDto: CreateProductDto) { try { await this.prisma.products.create({ data: createProductDto, }); } catch (err) { throw new InternalServerErrorException('Failed to create a new product'); } return 'successfully uploaded a new product'; } async findByCategory(category: string, page: number) { try { if (category === 'all') { return await this.prisma.products.findMany({ skip: page * 50, take: 50, }); } else { return await this.prisma.products.findMany({ skip: page * 50, take: 50, where: { category }, }); } } catch (err) { throw new InternalServerErrorException( 'Failed to find products by category', ); } } async findTotalPages(category: string) { try { let productsCount; if (category === 'all') { productsCount = await this.prisma.products.count(); } else { productsCount = await this.prisma.products.count({ where: { category }, }); } this.logger.log(productsCount); this.logger.log(Math.ceil(productsCount / 50)); return Math.ceil(productsCount / 50); } catch (err) { this.logger.error(err); throw new InternalServerErrorException(err); } } async update(id: number, updateProductDto: UpdateProductDto) { return `This action updates a #${id} product`; } async remove(id: number) { return `This action removes a #${id} product`; } }