256 lines
6.3 KiB
TypeScript
256 lines
6.3 KiB
TypeScript
import {
|
|
Controller,
|
|
Get,
|
|
Post,
|
|
Body,
|
|
Put,
|
|
Param,
|
|
Delete,
|
|
ParseUUIDPipe,
|
|
HttpStatus,
|
|
Query,
|
|
Request,
|
|
} from '@nestjs/common';
|
|
import { ProductService } from './product.service';
|
|
import { ProductCategoriesService } from './product-categories.service';
|
|
import { CreateCategoriesProductDto } from './dto/categories/create-categories-product.dto';
|
|
import { UpdateCategoriesProductDto } from '../product/dto/categories/update-categories-product.dto';
|
|
import { UpdateSubCategoriesProductDto } from '../product/dto/sub-categories/update-sub-categories-product.dto';
|
|
import { ProductSubCategoriesService } from './product-sub-categories.service';
|
|
import { CreateSubCategoriesProductDto } from './dto/sub-categories/create-sub-categories-product.dto';
|
|
import { CreateProductDto } from './dto/product/create-product.dto';
|
|
import { UpdateProductDto } from './dto/product/update-product.dto';
|
|
import { ProductHistoryPriceService } from './history-price/history-price.service';
|
|
|
|
@Controller({
|
|
path: 'product',
|
|
version: '1',
|
|
})
|
|
export class ProductController {
|
|
constructor(
|
|
private readonly productService: ProductService,
|
|
private readonly productCategoriesService: ProductCategoriesService,
|
|
private readonly productSubCategoriesService: ProductSubCategoriesService,
|
|
private readonly productHistoryPriceService: ProductHistoryPriceService,
|
|
) {}
|
|
|
|
@Post()
|
|
async create(@Body() createProductDto: CreateProductDto) {
|
|
return {
|
|
data: await this.productService.create(createProductDto),
|
|
statusCode: HttpStatus.CREATED,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Post('categories')
|
|
async createCategories(
|
|
@Body() createCategoriesProductDto: CreateCategoriesProductDto,
|
|
) {
|
|
return {
|
|
data: await this.productCategoriesService.create(
|
|
createCategoriesProductDto,
|
|
),
|
|
statusCode: HttpStatus.CREATED,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Post('sub-categories')
|
|
async createSubCategories(
|
|
@Body() createSubCategoriesProductDto: CreateSubCategoriesProductDto,
|
|
) {
|
|
return {
|
|
data: await this.productSubCategoriesService.create(
|
|
createSubCategoriesProductDto,
|
|
),
|
|
statusCode: HttpStatus.CREATED,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Get()
|
|
async findAll(@Query('page') page: number) {
|
|
const [data, count] = await this.productService.findAll(page);
|
|
|
|
return {
|
|
data,
|
|
count,
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Get('test')
|
|
async test(@Request() req) {
|
|
const data = await this.productHistoryPriceService.findOne(
|
|
'4d3b328c-3ce4-4c84-84bb-cd9c36e85a45',
|
|
'27effb3e-0351-428a-ba7f-08a21c54e16a',
|
|
);
|
|
|
|
return {
|
|
data,
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Get('by-categories-all')
|
|
async findByCategoriesAll(
|
|
@Query('page') page: number,
|
|
@Query('categories') categories: string,
|
|
@Query('supplier') supplier: string,
|
|
) {
|
|
const data = await this.productService.findAllBySubCategories(
|
|
page,
|
|
categories,
|
|
supplier,
|
|
);
|
|
|
|
return {
|
|
...data,
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Get('by-categories')
|
|
async findByCategories(
|
|
@Query('page') page: number,
|
|
@Query('categories') categories: string,
|
|
@Request() req,
|
|
) {
|
|
const data = await this.productService.findAllForPartner(
|
|
page,
|
|
categories,
|
|
req.user.username,
|
|
);
|
|
|
|
return {
|
|
...data,
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Get('categories')
|
|
async findAllCategories(@Query('page') page: number) {
|
|
const [data, count] = await this.productCategoriesService.findAll(page);
|
|
|
|
return {
|
|
data,
|
|
count,
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Get('sub-categories')
|
|
async findAllSubCategories(@Query('page') page: number) {
|
|
const [data, count] = await this.productSubCategoriesService.findAll(page);
|
|
|
|
return {
|
|
data,
|
|
count,
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Get(':id')
|
|
async findOne(@Param('id', ParseUUIDPipe) id: string) {
|
|
return {
|
|
data: await this.productService.findOne(id),
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Get('categories/:id')
|
|
async findOneCategories(@Param('id', ParseUUIDPipe) id: string) {
|
|
return {
|
|
data: await this.productCategoriesService.findOne(id),
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Get('sub-categories/:id')
|
|
async findOneSubCategories(@Param('id', ParseUUIDPipe) id: string) {
|
|
return {
|
|
data: await this.productSubCategoriesService.findOne(id),
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Put(':id')
|
|
async update(
|
|
@Param('id', ParseUUIDPipe) id: string,
|
|
@Body() updateProductDto: UpdateProductDto,
|
|
) {
|
|
return {
|
|
data: await this.productService.update(id, updateProductDto),
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Put('categories/:id')
|
|
async updateCategories(
|
|
@Param('id', ParseUUIDPipe) id: string,
|
|
@Body() updateCategoriesDto: UpdateCategoriesProductDto,
|
|
) {
|
|
return {
|
|
data: await this.productCategoriesService.update(id, updateCategoriesDto),
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Put('sub-categories/:id')
|
|
async updateSubCategories(
|
|
@Param('id', ParseUUIDPipe) id: string,
|
|
@Body() updateSubCategoriesDto: UpdateSubCategoriesProductDto,
|
|
) {
|
|
return {
|
|
data: await this.productSubCategoriesService.update(
|
|
id,
|
|
updateSubCategoriesDto,
|
|
),
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Delete(':id')
|
|
async remove(@Param('id', ParseUUIDPipe) id: string) {
|
|
await this.productService.remove(id);
|
|
|
|
return {
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Delete('categories/:id')
|
|
async removeCategories(@Param('id', ParseUUIDPipe) id: string) {
|
|
await this.productCategoriesService.remove(id);
|
|
|
|
return {
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
|
|
@Delete('sub-categories/:id')
|
|
async removeSubCategories(@Param('id', ParseUUIDPipe) id: string) {
|
|
await this.productSubCategoriesService.remove(id);
|
|
|
|
return {
|
|
statusCode: HttpStatus.OK,
|
|
message: 'success',
|
|
};
|
|
}
|
|
}
|