feat: PantryItem (Baslager) - tabell, backend-modul och frontend-sida

This commit is contained in:
Nils-Johan Gynther
2026-04-15 22:06:40 +02:00
parent 65ec74ac7d
commit 47d1aafd9e
12 changed files with 373 additions and 1 deletions
+44
View File
@@ -0,0 +1,44 @@
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() {
return this.prisma.pantryItem.findMany({
include: {
product: true,
},
orderBy: {
product: { name: 'asc' },
},
});
}
async create(data: CreatePantryItemDto) {
const existing = await this.prisma.pantryItem.findUnique({
where: { productId: data.productId },
});
if (existing) {
throw new ConflictException('Produkten finns redan i baslagret');
}
return this.prisma.pantryItem.create({
data: { productId: data.productId },
include: { product: true },
});
}
async remove(id: number) {
const item = await this.prisma.pantryItem.findUnique({ where: { id } });
if (!item) {
throw new NotFoundException(`PantryItem med id ${id} hittades inte`);
}
return this.prisma.pantryItem.delete({ where: { id } });
}
}