100 lines
3.8 KiB
TypeScript
100 lines
3.8 KiB
TypeScript
import { Controller, Get, Post, Body, Patch, Param, Delete, Query, Req, UseInterceptors, UploadedFiles } from '@nestjs/common';
|
|
import { FilesInterceptor } from '@nestjs/platform-express';
|
|
import { InventoryService } from './inventory.service';
|
|
import { CreateProductDto } from './dto/create-product.dto';
|
|
import { UpdateProductDto } from './dto/update-product.dto';
|
|
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
|
|
// import { Roles } from '../../common/decorators/roles.decorator';
|
|
import { PaginationDto } from '../../common/dto/pagination.dto';
|
|
|
|
@ApiTags('products')
|
|
@Controller('products')
|
|
export class UsersController {
|
|
constructor(private readonly inventoryService: InventoryService) {}
|
|
|
|
@Get('/store')
|
|
// @Roles('admin')
|
|
@ApiOperation({ summary: 'Get all products with pagination and filters' })
|
|
@ApiResponse({ status: 200, description: 'Return paginated products.' })
|
|
async findAll(@Query() paginationDto: PaginationDto) {
|
|
const result = await this.inventoryService.findAll(paginationDto,true);
|
|
return {
|
|
message: 'products fetched successfully',
|
|
data: result.data,
|
|
meta: result.meta
|
|
};
|
|
}
|
|
|
|
@Get('/inventory')
|
|
// @Roles('admin')
|
|
@ApiOperation({ summary: 'Get all products with pagination and filters' })
|
|
@ApiResponse({ status: 200, description: 'Return paginated products.' })
|
|
async findAllByUserId(@Req() req: Request, @Query() paginationDto: PaginationDto) {
|
|
// console.log(req['user'].id)
|
|
// const id = 1
|
|
const id = Number(req['user'].id);
|
|
const result = await this.inventoryService.findAllByUserId(id,paginationDto);
|
|
return {
|
|
message: 'products fetched successfully',
|
|
data: result.data,
|
|
meta: result.meta
|
|
};
|
|
}
|
|
|
|
@Get('/id/:id')
|
|
// @Roles('admin')
|
|
@ApiOperation({ summary: 'Get a product by ID' })
|
|
@ApiResponse({ status: 200, description: 'Return the product.' })
|
|
@ApiResponse({ status: 404, description: 'product not found.' })
|
|
async findOne(@Param('id') id: string) {
|
|
const productId = Number(id)
|
|
const data = await this.inventoryService.findOne(productId);
|
|
return { message: 'product fetched successfully', data };
|
|
}
|
|
|
|
@Post()
|
|
@ApiOperation({ summary: 'Create a new product' })
|
|
@ApiResponse({ status: 201, description: 'Product created successfully.' })
|
|
@ApiResponse({ status: 500, description: 'Internal server error.' })
|
|
@UseInterceptors(FilesInterceptor('urlImg'))
|
|
async create(
|
|
@Req() req: Request,
|
|
@Body() createUserDto: CreateProductDto,
|
|
@UploadedFiles() files: Express.Multer.File[],
|
|
// @Query('roleId') roleId?: string,
|
|
) {
|
|
const id = Number(req['user'].id);
|
|
const data = await this.inventoryService.create(files,createUserDto,id)
|
|
return { message: 'User created successfully', data };
|
|
}
|
|
|
|
@Patch('/upload')
|
|
@ApiOperation({ summary: 'Update a product' })
|
|
@ApiResponse({ status: 200, description: 'Product uploaded successfully.'})
|
|
@ApiResponse({ status: 404, description: 'Product not found.' })
|
|
@ApiResponse({ status: 500, description: 'Internal server error.' })
|
|
@UseInterceptors(FilesInterceptor('urlImg'))
|
|
async uploadFile(
|
|
@Req() req: Request,
|
|
@UploadedFiles() files: Express.Multer.File[],
|
|
@Body() body: any
|
|
) {
|
|
const id = Number(req['user'].id);
|
|
const result = await this.inventoryService.update(files,body,id);
|
|
return { data: result };
|
|
}
|
|
|
|
|
|
|
|
@Delete(':id')
|
|
// @Roles('admin')
|
|
@ApiOperation({ summary: 'Delete a Product' })
|
|
@ApiResponse({ status: 200, description: 'Product deleted successfully.' })
|
|
@ApiResponse({ status: 404, description: 'Product not found.' })
|
|
async remove(@Req() req: Request, @Param('id') id: string) {
|
|
const productId = Number(id);
|
|
const userId = Number(req['user'].id);
|
|
return await this.inventoryService.remove(productId,userId);
|
|
}
|
|
}
|