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
@@ -0,0 +1,13 @@
-- CreateTable
CREATE TABLE `PantryItem` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`productId` INTEGER NOT NULL,
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`updatedAt` DATETIME(3) NOT NULL,
UNIQUE INDEX `PantryItem_productId_key`(`productId`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- AddForeignKey
ALTER TABLE `PantryItem` ADD CONSTRAINT `PantryItem_productId_fkey` FOREIGN KEY (`productId`) REFERENCES `Product`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
+10 -1
View File
@@ -18,8 +18,9 @@ model Product {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
inventoryItems InventoryItem[]
inventoryItems InventoryItem[]
recipeIngredients RecipeIngredient[]
pantryItems PantryItem[]
}
model InventoryItem {
@@ -81,6 +82,14 @@ model RecipeIngredient {
unit String
note String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model PantryItem {
id Int @id @default(autoincrement())
productId Int @unique
product Product @relation(fields: [productId], references: [id], onDelete: Cascade)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
+2
View File
@@ -5,6 +5,7 @@ import { ProductsModule } from './products/products.module';
import { InventoryModule } from './inventory/inventory.module';
import { RecipesModule } from './recipes/recipes.module';
import { QuickImportModule } from './quick-import/quick-import.module';
import { PantryModule } from './pantry/pantry.module';
@Module({
@@ -15,6 +16,7 @@ import { QuickImportModule } from './quick-import/quick-import.module';
InventoryModule,
RecipesModule,
QuickImportModule,
PantryModule,
],
})
export class AppModule {}
@@ -0,0 +1,7 @@
import { IsInt, IsPositive } from 'class-validator';
export class CreatePantryItemDto {
@IsInt()
@IsPositive()
productId: number;
}
+23
View File
@@ -0,0 +1,23 @@
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post } from '@nestjs/common';
import { PantryService } from './pantry.service';
import { CreatePantryItemDto } from './dto/create-pantry-item.dto';
@Controller('pantry')
export class PantryController {
constructor(private readonly pantryService: PantryService) {}
@Get()
findAll() {
return this.pantryService.findAll();
}
@Post()
create(@Body() body: CreatePantryItemDto) {
return this.pantryService.create(body);
}
@Delete(':id')
remove(@Param('id', ParseIntPipe) id: number) {
return this.pantryService.remove(id);
}
}
+11
View File
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { PantryController } from './pantry.controller';
import { PantryService } from './pantry.service';
import { PrismaModule } from '../prisma/prisma.module';
@Module({
controllers: [PantryController],
providers: [PantryService],
imports: [PrismaModule],
})
export class PantryModule {}
+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 } });
}
}