import { Injectable, ConflictException, NotFoundException } from '@nestjs/common'; import { PrismaService } from '../prisma/prisma.service'; import { CreatePantryItemDto } from './dto/create-pantry-item.dto'; @Injectable() export class PantryService { constructor(private readonly prisma: PrismaService) {} findAll(userId: number) { return this.prisma.pantryItem.findMany({ where: { userId }, include: { product: true, }, orderBy: { product: { name: 'asc' }, }, }); } async create(userId: number, data: CreatePantryItemDto) { const existing = await this.prisma.pantryItem.findUnique({ where: { userId_productId: { userId, productId: data.productId, }, }, }); if (existing) { throw new ConflictException('Produkten finns redan i baslagret'); } return this.prisma.pantryItem.create({ data: { userId, productId: data.productId }, include: { product: true }, }); } async remove(userId: number, id: number) { const item = await this.prisma.pantryItem.findFirst({ where: { id, userId }, }); if (!item) { throw new NotFoundException(`PantryItem med id ${id} hittades inte`); } return this.prisma.pantryItem.delete({ where: { id } }); } }