150 lines
5.3 KiB
TypeScript
150 lines
5.3 KiB
TypeScript
import { Controller, Get, Post, Body, Patch, Param, Delete, Query, UseGuards } from '@nestjs/common';
|
|
import { ProductService } from '../providers/product.service';
|
|
import { CreateproductDto } from '../dto/create-product.dto';
|
|
import { UpdateproductDto } from '../dto/update-product.dto';
|
|
import { FindproductsDto } from '../dto/find-products.dto';
|
|
import {
|
|
ApiTags,
|
|
ApiOperation,
|
|
ApiQuery,
|
|
ApiBody,
|
|
ApiParam,
|
|
ApiBearerAuth,
|
|
} from '@nestjs/swagger';
|
|
import { Permission } from 'src/common/enums/permission.enum';
|
|
import { Permissions } from 'src/common/decorators/permissions.decorator';
|
|
import { CreateCategoryDto } from '../dto/create-category.dto';
|
|
import { CategoryService } from '../providers/category.service';
|
|
import { FindCategoriesDto } from '../dto/find-categories.dto';
|
|
import { UpdateCategoryDto } from '../dto/update-category.dto';
|
|
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
|
import { AuthGuard } from 'src/modules/auth/guards/auth.guard';
|
|
|
|
|
|
@Controller()
|
|
@ApiTags('products')
|
|
@ApiBearerAuth()
|
|
export class ProductController {
|
|
constructor(
|
|
private readonly productService: ProductService,
|
|
private readonly categoryService: CategoryService,
|
|
) { }
|
|
|
|
|
|
/*====================CATEGORY =========================*/
|
|
|
|
@Post('admin/category')
|
|
@UseGuards(AdminAuthGuard)
|
|
@Permissions(Permission.MANAGE_CATEGORIES)
|
|
@ApiOperation({ summary: 'Create a new category' })
|
|
createCategory(@Body() createproductDto: CreateCategoryDto) {
|
|
return this.categoryService.create(createproductDto);
|
|
}
|
|
|
|
@Get('admin/category')
|
|
@UseGuards(AdminAuthGuard)
|
|
@ApiOperation({ summary: 'Get a paginated list of Categories' })
|
|
@ApiQuery({ name: 'page', required: false, type: Number })
|
|
@ApiQuery({ name: 'limit', required: false, type: Number })
|
|
@ApiQuery({ name: 'search', required: false, type: String })
|
|
@ApiQuery({ name: 'orderBy', required: false, type: String })
|
|
@ApiQuery({ name: 'order', required: false, enum: ['asc', 'desc'] })
|
|
@ApiQuery({ name: 'isActive', required: false, type: Boolean })
|
|
async findAllCategories(@Query() dto: FindCategoriesDto) {
|
|
const result = await this.categoryService.findAll(dto);
|
|
return result;
|
|
}
|
|
|
|
|
|
@Get('admin/category/:id')
|
|
@UseGuards(AdminAuthGuard)
|
|
@ApiOperation({ summary: 'Get a category by id' })
|
|
@ApiParam({ name: 'id', required: true })
|
|
findCategoryById(@Param('id') id: string) {
|
|
return this.categoryService.findById(+id);
|
|
}
|
|
|
|
@Patch('admin/category/:id')
|
|
@UseGuards(AdminAuthGuard)
|
|
@Permissions(Permission.MANAGE_CATEGORIES)
|
|
@ApiOperation({ summary: 'Update a category' })
|
|
@ApiParam({ name: 'id', required: true })
|
|
@ApiBody({ type: UpdateCategoryDto })
|
|
updateCategory(@Param('id') id: string, @Body() dto: UpdateCategoryDto) {
|
|
return this.categoryService.update(+id, dto);
|
|
}
|
|
|
|
@Delete('admin/category/:id')
|
|
@UseGuards(AdminAuthGuard)
|
|
@Permissions(Permission.MANAGE_CATEGORIES)
|
|
@ApiOperation({ summary: 'Delete (soft) a category' })
|
|
@ApiParam({ name: 'id', required: true })
|
|
removeCategory(@Param('id') id: string,) {
|
|
return this.categoryService.remove(+id);
|
|
}
|
|
|
|
/* =============================== PRODUCT ================================= */
|
|
|
|
@Get('public/products')
|
|
@UseGuards(AuthGuard)
|
|
@ApiOperation({ summary: 'Get list of products' })
|
|
async findAllAsUser() {
|
|
const result = await this.productService.findAll({ limit: 1000 });
|
|
return result;
|
|
}
|
|
|
|
@Post('admin/product')
|
|
@UseGuards(AdminAuthGuard)
|
|
@Permissions(Permission.MANAGE_PRODUCTS)
|
|
@ApiOperation({ summary: 'Create a new product' })
|
|
@ApiBody({ type: CreateproductDto })
|
|
create(@Body() createproductDto: CreateproductDto) {
|
|
return this.productService.create(createproductDto);
|
|
}
|
|
|
|
@Get('admin/products')
|
|
@UseGuards(AdminAuthGuard)
|
|
@Permissions(Permission.MANAGE_PRODUCTS)
|
|
@ApiOperation({ summary: 'Get a paginated list of products' })
|
|
@ApiQuery({ name: 'page', required: false, type: Number })
|
|
@ApiQuery({ name: 'limit', required: false, type: Number })
|
|
@ApiQuery({ name: 'search', required: false, type: String })
|
|
@ApiQuery({ name: 'orderBy', required: false, type: String })
|
|
@ApiQuery({ name: 'order', required: false, enum: ['asc', 'desc'] })
|
|
@ApiQuery({ name: 'categoryId', required: false, type: String })
|
|
@ApiQuery({ name: 'isActive', required: false, type: Boolean })
|
|
async findAll(@Query() dto: FindproductsDto) {
|
|
const result = await this.productService.findAll(dto);
|
|
return result;
|
|
}
|
|
|
|
@Get('admin/products/:id')
|
|
@UseGuards(AdminAuthGuard)
|
|
@Permissions(Permission.MANAGE_PRODUCTS)
|
|
@ApiOperation({ summary: 'Get a product by id' })
|
|
@ApiParam({ name: 'id', required: true })
|
|
findById(@Param('id') id: string,) {
|
|
return this.productService.findById(+id);
|
|
}
|
|
|
|
@Patch('admin/products/:id')
|
|
@UseGuards(AdminAuthGuard)
|
|
@Permissions(Permission.MANAGE_PRODUCTS)
|
|
@ApiOperation({ summary: 'Update a product' })
|
|
@ApiParam({ name: 'id', required: true })
|
|
update(@Param('id') id: string, @Body() updateproductDto: UpdateproductDto) {
|
|
return this.productService.update(+id, updateproductDto);
|
|
}
|
|
|
|
@Delete('admin/products/:id')
|
|
@UseGuards(AdminAuthGuard)
|
|
@Permissions(Permission.MANAGE_PRODUCTS)
|
|
@ApiOperation({ summary: 'Delete (soft) a product' })
|
|
@ApiParam({ name: 'id', required: true })
|
|
remove(@Param('id') id: string,) {
|
|
return this.productService.remove(+id);
|
|
}
|
|
|
|
|
|
}
|