Files
sporedimk/price-compare-api/src/product/product.controller.ts
T
2025-05-06 10:01:03 +02:00

58 lines
1.4 KiB
TypeScript

import { Controller, Get, Post, Put, Body, Param, Query } from '@nestjs/common';
import { ProductService } from './product.service';
import { Prisma } from '@prisma/client';
@Controller('products')
export class ProductController {
constructor(private readonly productService: ProductService) {}
@Get(':id')
async getProduct(@Param('id') id: string) {
return this.productService.getProduct(Number(id));
}
@Get()
async getProducts(
@Query('skip') skip?: string,
@Query('take') take?: string,
@Query('search') search?: string,
@Query('maxPrice') maxPrice?: string,
) {
if (search) {
return this.productService.searchProducts(search);
}
return this.productService.getProducts({
skip: skip ? Number(skip) : undefined,
take: take ? Number(take) : undefined,
where: maxPrice
? {
prices: {
some: {
regularPrice: {
lte: Number(maxPrice),
},
},
},
}
: undefined,
});
}
@Post()
async createProduct(@Body() data: Prisma.ProductCreateInput) {
return this.productService.createProduct(data);
}
@Put(':id')
async updateProduct(
@Param('id') id: string,
@Body() data: Prisma.ProductUpdateInput,
) {
return this.productService.updateProduct({
where: { id: Number(id) },
data,
});
}
}