add: crud all product module
This commit is contained in:
parent
659c7e4de8
commit
6622501b61
4
src/product/dto/product/update-product.dto.ts
Normal file
4
src/product/dto/product/update-product.dto.ts
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
import { PartialType } from '@nestjs/mapped-types';
|
||||||
|
import { CreateProductDto } from './create-product.dto';
|
||||||
|
|
||||||
|
export class UpdateProductDto extends PartialType(CreateProductDto) {}
|
|
@ -2,6 +2,9 @@ import { IsNotEmpty, IsUUID } from 'class-validator';
|
||||||
import { CreateCategoriesProductDto } from '../categories/create-categories-product.dto';
|
import { CreateCategoriesProductDto } from '../categories/create-categories-product.dto';
|
||||||
|
|
||||||
export class CreateSubCategoriesProductDto extends CreateCategoriesProductDto {
|
export class CreateSubCategoriesProductDto extends CreateCategoriesProductDto {
|
||||||
|
@IsNotEmpty()
|
||||||
|
name: string;
|
||||||
|
|
||||||
@IsUUID()
|
@IsUUID()
|
||||||
categoryId: string;
|
categoryId: string;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,19 +4,26 @@ import { ProductSubCategories } from './entities/product-sub-category.entity';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { CreateSubCategoriesProductDto } from './dto/sub-categories/create-sub-categories-product.dto';
|
import { CreateSubCategoriesProductDto } from './dto/sub-categories/create-sub-categories-product.dto';
|
||||||
import { UpdateSubCategoriesProductDto } from './dto/sub-categories/update-sub-categories-product.dto';
|
import { UpdateSubCategoriesProductDto } from './dto/sub-categories/update-sub-categories-product.dto';
|
||||||
|
import { ProductCategoriesService } from './product-categories.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ProductSubCategoriesService {
|
export class ProductSubCategoriesService {
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(ProductSubCategories)
|
@InjectRepository(ProductSubCategories)
|
||||||
private productSubCategoriesRepository: Repository<ProductSubCategories>,
|
private productSubCategoriesRepository: Repository<ProductSubCategories>,
|
||||||
|
private productCategoriesService: ProductCategoriesService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async create(CreateCategoriesProductDto: CreateSubCategoriesProductDto) {
|
async create(createSubCategoriesProductDto: CreateSubCategoriesProductDto) {
|
||||||
const result = await this.productSubCategoriesRepository.insert(
|
const categories = await this.productCategoriesService.findOne(
|
||||||
CreateCategoriesProductDto,
|
createSubCategoriesProductDto.categoryId,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const result = await this.productSubCategoriesRepository.insert({
|
||||||
|
name: createSubCategoriesProductDto.name,
|
||||||
|
category: categories,
|
||||||
|
});
|
||||||
|
|
||||||
return this.productSubCategoriesRepository.findOneOrFail(
|
return this.productSubCategoriesRepository.findOneOrFail(
|
||||||
result.identifiers[0].id,
|
result.identifiers[0].id,
|
||||||
);
|
);
|
||||||
|
@ -70,11 +77,15 @@ export class ProductSubCategoriesService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await this.productSubCategoriesRepository.update(
|
const categories = await this.productCategoriesService.findOne(
|
||||||
id,
|
updateCategoriesProductDto.categoryId,
|
||||||
updateCategoriesProductDto,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const result = await this.productSubCategoriesRepository.update(id, {
|
||||||
|
name: updateCategoriesProductDto.name,
|
||||||
|
category: categories,
|
||||||
|
});
|
||||||
|
|
||||||
return this.productSubCategoriesRepository.findOneOrFail(id);
|
return this.productSubCategoriesRepository.findOneOrFail(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,11 +7,18 @@ import {
|
||||||
Param,
|
Param,
|
||||||
Delete,
|
Delete,
|
||||||
ParseUUIDPipe,
|
ParseUUIDPipe,
|
||||||
HttpStatus, Query,
|
HttpStatus,
|
||||||
|
Query,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ProductService } from './product.service';
|
import { ProductService } from './product.service';
|
||||||
import { ProductCategoriesService } from './product-categories.service';
|
import { ProductCategoriesService } from './product-categories.service';
|
||||||
import { CreateCategoriesProductDto } from './dto/categories/create-categories-product.dto';
|
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';
|
||||||
|
|
||||||
@Controller({
|
@Controller({
|
||||||
path: 'product',
|
path: 'product',
|
||||||
|
@ -21,8 +28,18 @@ export class ProductController {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly productService: ProductService,
|
private readonly productService: ProductService,
|
||||||
private readonly productCategoriesService: ProductCategoriesService,
|
private readonly productCategoriesService: ProductCategoriesService,
|
||||||
|
private readonly productSubCategoriesService: ProductSubCategoriesService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
@Post()
|
||||||
|
async create(@Body() createProductDto: CreateProductDto) {
|
||||||
|
return {
|
||||||
|
data: await this.productService.create(createProductDto),
|
||||||
|
statusCode: HttpStatus.CREATED,
|
||||||
|
message: 'success',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
@Post('categories')
|
@Post('categories')
|
||||||
async createCategories(
|
async createCategories(
|
||||||
@Body() createCategoriesProductDto: CreateCategoriesProductDto,
|
@Body() createCategoriesProductDto: CreateCategoriesProductDto,
|
||||||
|
@ -36,6 +53,19 @@ export class ProductController {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Post('sub-categories')
|
||||||
|
async createSubCategories(
|
||||||
|
@Body() createSubCategoriesProductDto: CreateSubCategoriesProductDto,
|
||||||
|
) {
|
||||||
|
return {
|
||||||
|
data: await this.productSubCategoriesService.create(
|
||||||
|
createSubCategoriesProductDto,
|
||||||
|
),
|
||||||
|
statusCode: HttpStatus.CREATED,
|
||||||
|
message: 'success',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
async findAll(@Query('page') page: number) {
|
async findAll(@Query('page') page: number) {
|
||||||
const [data, count] = await this.productService.findAll(page);
|
const [data, count] = await this.productService.findAll(page);
|
||||||
|
@ -48,6 +78,30 @@ export class ProductController {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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')
|
@Get(':id')
|
||||||
async findOne(@Param('id', ParseUUIDPipe) id: string) {
|
async findOne(@Param('id', ParseUUIDPipe) id: string) {
|
||||||
return {
|
return {
|
||||||
|
@ -57,4 +111,90 @@ export class ProductController {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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',
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import { Product } from './entities/product.entity';
|
||||||
import { ProductCategories } from './entities/product-category.entity';
|
import { ProductCategories } from './entities/product-category.entity';
|
||||||
import { ProductHistoryPrice } from './entities/product-history-price.entity';
|
import { ProductHistoryPrice } from './entities/product-history-price.entity';
|
||||||
import { ProductSubCategories } from './entities/product-sub-category.entity';
|
import { ProductSubCategories } from './entities/product-sub-category.entity';
|
||||||
|
import { ProductSubCategoriesService } from './product-sub-categories.service';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
|
@ -18,6 +19,10 @@ import { ProductSubCategories } from './entities/product-sub-category.entity';
|
||||||
]),
|
]),
|
||||||
],
|
],
|
||||||
controllers: [ProductController],
|
controllers: [ProductController],
|
||||||
providers: [ProductService, ProductCategoriesService],
|
providers: [
|
||||||
|
ProductService,
|
||||||
|
ProductCategoriesService,
|
||||||
|
ProductSubCategoriesService,
|
||||||
|
],
|
||||||
})
|
})
|
||||||
export class ProductModule {}
|
export class ProductModule {}
|
||||||
|
|
|
@ -1,22 +1,29 @@
|
||||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||||
import { EntityNotFoundError, Repository } from 'typeorm';
|
import { EntityNotFoundError, Repository } from 'typeorm';
|
||||||
import { Product } from './entities/product.entity';
|
import { Product } from './entities/product.entity';
|
||||||
import { ProductCategories } from './entities/product-category.entity';
|
|
||||||
import { ProductSubCategories } from './entities/product-sub-category.entity';
|
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { CreateProductDto } from '../product/dto/create-product.dto';
|
import { CreateProductDto } from '../product/dto/product/create-product.dto';
|
||||||
import { CreateCategoriesProductDto } from './dto/categories/create-categories-product.dto';
|
import { ProductSubCategoriesService } from './product-sub-categories.service';
|
||||||
import { CreateSubCategoriesProductDto } from './dto/sub-categories/create-sub-categories-product.dto';
|
import { UpdateProductDto } from './dto/product/update-product.dto';
|
||||||
|
|
||||||
@Injectable()
|
|
||||||
export class ProductService {
|
export class ProductService {
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(Product)
|
@InjectRepository(Product)
|
||||||
private productRepository: Repository<Product>,
|
private productRepository: Repository<Product>,
|
||||||
|
private productSubCategoriesService: ProductSubCategoriesService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async create(createProductDto: CreateProductDto) {
|
async create(createProductDto: CreateProductDto) {
|
||||||
const result = await this.productRepository.insert(createProductDto);
|
const subCategories = await this.productSubCategoriesService.findOne(
|
||||||
|
createProductDto.subCategoriesId,
|
||||||
|
);
|
||||||
|
|
||||||
|
const result = await this.productRepository.insert({
|
||||||
|
name: createProductDto.name,
|
||||||
|
code: createProductDto.code,
|
||||||
|
status: createProductDto.status,
|
||||||
|
subCategories: subCategories,
|
||||||
|
});
|
||||||
|
|
||||||
return this.productRepository.findOneOrFail(result.identifiers[0].id);
|
return this.productRepository.findOneOrFail(result.identifiers[0].id);
|
||||||
}
|
}
|
||||||
|
@ -31,9 +38,9 @@ export class ProductService {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async findOne(id: string) {
|
async findOne(code: string) {
|
||||||
try {
|
try {
|
||||||
return await this.productRepository.findOneOrFail(id);
|
return await this.productRepository.findOneOrFail({ code: code });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof EntityNotFoundError) {
|
if (e instanceof EntityNotFoundError) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
|
@ -48,4 +55,54 @@ export class ProductService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async update(id: string, updateProductDto: UpdateProductDto) {
|
||||||
|
try {
|
||||||
|
await this.productRepository.findOneOrFail(id);
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof EntityNotFoundError) {
|
||||||
|
throw new HttpException(
|
||||||
|
{
|
||||||
|
statusCode: HttpStatus.NOT_FOUND,
|
||||||
|
error: 'Data not found',
|
||||||
|
},
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const subCategories = await this.productSubCategoriesService.findOne(
|
||||||
|
updateProductDto.subCategoriesId,
|
||||||
|
);
|
||||||
|
|
||||||
|
const result = await this.productRepository.update(id, {
|
||||||
|
name: updateProductDto.name,
|
||||||
|
code: updateProductDto.code,
|
||||||
|
status: updateProductDto.status,
|
||||||
|
subCategories: subCategories,
|
||||||
|
});
|
||||||
|
|
||||||
|
return this.productRepository.findOneOrFail(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
async remove(id: string) {
|
||||||
|
try {
|
||||||
|
await this.productRepository.findOneOrFail(id);
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof EntityNotFoundError) {
|
||||||
|
throw new HttpException(
|
||||||
|
{
|
||||||
|
statusCode: HttpStatus.NOT_FOUND,
|
||||||
|
error: 'Data not found',
|
||||||
|
},
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.productRepository.delete(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user