feat: make pantry items and meal plan entries user-scoped; update related services and controllers
This commit is contained in:
@@ -1,23 +1,30 @@
|
||||
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post } from '@nestjs/common';
|
||||
import { PantryService } from './pantry.service';
|
||||
import { CreatePantryItemDto } from './dto/create-pantry-item.dto';
|
||||
import { CurrentUser } from '../auth/decorators/current-user.decorator';
|
||||
|
||||
@Controller('pantry')
|
||||
export class PantryController {
|
||||
constructor(private readonly pantryService: PantryService) {}
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
return this.pantryService.findAll();
|
||||
findAll(@CurrentUser() user: { userId: number }) {
|
||||
return this.pantryService.findAll(user.userId);
|
||||
}
|
||||
|
||||
@Post()
|
||||
create(@Body() body: CreatePantryItemDto) {
|
||||
return this.pantryService.create(body);
|
||||
create(
|
||||
@CurrentUser() user: { userId: number },
|
||||
@Body() body: CreatePantryItemDto,
|
||||
) {
|
||||
return this.pantryService.create(user.userId, body);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id', ParseIntPipe) id: number) {
|
||||
return this.pantryService.remove(id);
|
||||
remove(
|
||||
@CurrentUser() user: { userId: number },
|
||||
@Param('id', ParseIntPipe) id: number,
|
||||
) {
|
||||
return this.pantryService.remove(user.userId, id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,9 @@ import { CreatePantryItemDto } from './dto/create-pantry-item.dto';
|
||||
export class PantryService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
findAll() {
|
||||
findAll(userId: number) {
|
||||
return this.prisma.pantryItem.findMany({
|
||||
where: { userId },
|
||||
include: {
|
||||
product: true,
|
||||
},
|
||||
@@ -17,9 +18,14 @@ export class PantryService {
|
||||
});
|
||||
}
|
||||
|
||||
async create(data: CreatePantryItemDto) {
|
||||
async create(userId: number, data: CreatePantryItemDto) {
|
||||
const existing = await this.prisma.pantryItem.findUnique({
|
||||
where: { productId: data.productId },
|
||||
where: {
|
||||
userId_productId: {
|
||||
userId,
|
||||
productId: data.productId,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
@@ -27,13 +33,15 @@ export class PantryService {
|
||||
}
|
||||
|
||||
return this.prisma.pantryItem.create({
|
||||
data: { productId: data.productId },
|
||||
data: { userId, productId: data.productId },
|
||||
include: { product: true },
|
||||
});
|
||||
}
|
||||
|
||||
async remove(id: number) {
|
||||
const item = await this.prisma.pantryItem.findUnique({ where: { id } });
|
||||
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`);
|
||||
|
||||
Reference in New Issue
Block a user