add: create crud product
This commit is contained in:
parent
1fc5706e47
commit
682b973329
|
@ -0,0 +1,6 @@
|
||||||
|
import { IsNotEmpty, IsUUID } from 'class-validator';
|
||||||
|
|
||||||
|
export class CreateCategoriesProductDto {
|
||||||
|
@IsNotEmpty()
|
||||||
|
name: string;
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
import { PartialType } from '@nestjs/mapped-types';
|
||||||
|
import { CreateCategoriesProductDto } from './create-categories-product.dto';
|
||||||
|
|
||||||
|
export class UpdateCategoriesProductDto extends PartialType(
|
||||||
|
CreateCategoriesProductDto,
|
||||||
|
) {}
|
15
src/product/dto/create-product.dto.ts
Normal file
15
src/product/dto/create-product.dto.ts
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
import { IsNotEmpty, IsUUID } from 'class-validator';
|
||||||
|
|
||||||
|
export class CreateProductDto {
|
||||||
|
@IsNotEmpty()
|
||||||
|
name: string;
|
||||||
|
|
||||||
|
@IsNotEmpty()
|
||||||
|
code: string;
|
||||||
|
|
||||||
|
@IsNotEmpty()
|
||||||
|
status: string;
|
||||||
|
|
||||||
|
@IsUUID()
|
||||||
|
subCategoriesId: string;
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
import { IsNotEmpty, IsUUID } from 'class-validator';
|
||||||
|
import { CreateCategoriesProductDto } from '../categories/create-categories-product.dto';
|
||||||
|
|
||||||
|
export class CreateSubCategoriesProductDto extends CreateCategoriesProductDto {
|
||||||
|
@IsUUID()
|
||||||
|
categoryId: string;
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
import { PartialType } from '@nestjs/mapped-types';
|
||||||
|
import { CreateSubCategoriesProductDto } from '../sub-categories/create-sub-categories-product.dto';
|
||||||
|
|
||||||
|
export class UpdateSubCategoriesProductDto extends PartialType(
|
||||||
|
CreateSubCategoriesProductDto,
|
||||||
|
) {}
|
|
@ -8,7 +8,7 @@ import {
|
||||||
CreateDateColumn,
|
CreateDateColumn,
|
||||||
OneToMany,
|
OneToMany,
|
||||||
} from 'typeorm';
|
} from 'typeorm';
|
||||||
import { ProductSubCategories } from './productSubCategory.entity';
|
import { ProductSubCategories } from './product-sub-category.entity';
|
||||||
import { BaseModel } from '../../config/basemodel.entity';
|
import { BaseModel } from '../../config/basemodel.entity';
|
||||||
|
|
||||||
@Entity()
|
@Entity()
|
|
@ -8,7 +8,7 @@ import {
|
||||||
CreateDateColumn,
|
CreateDateColumn,
|
||||||
ManyToOne,
|
ManyToOne,
|
||||||
} from 'typeorm';
|
} from 'typeorm';
|
||||||
import { ProductCategories } from './productCategory.entity';
|
import { ProductCategories } from './product-category.entity';
|
||||||
import { BaseModel } from '../../config/basemodel.entity';
|
import { BaseModel } from '../../config/basemodel.entity';
|
||||||
|
|
||||||
@Entity()
|
@Entity()
|
|
@ -7,8 +7,9 @@ import {
|
||||||
VersionColumn,
|
VersionColumn,
|
||||||
CreateDateColumn,
|
CreateDateColumn,
|
||||||
OneToMany,
|
OneToMany,
|
||||||
|
ManyToOne,
|
||||||
} from 'typeorm';
|
} from 'typeorm';
|
||||||
import { ProductSubCategories } from './productSubCategory.entity';
|
import { ProductSubCategories } from './product-sub-category.entity';
|
||||||
import { BaseModel } from '../../config/basemodel.entity';
|
import { BaseModel } from '../../config/basemodel.entity';
|
||||||
|
|
||||||
@Entity()
|
@Entity()
|
||||||
|
@ -25,7 +26,7 @@ export class Product extends BaseModel{
|
||||||
@Column()
|
@Column()
|
||||||
status: string;
|
status: string;
|
||||||
|
|
||||||
@OneToMany(
|
@ManyToOne(
|
||||||
() => ProductSubCategories,
|
() => ProductSubCategories,
|
||||||
(subCategories) => subCategories.category,
|
(subCategories) => subCategories.category,
|
||||||
)
|
)
|
||||||
|
|
100
src/product/product-categories.service.ts
Normal file
100
src/product/product-categories.service.ts
Normal file
|
@ -0,0 +1,100 @@
|
||||||
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||||
|
import { EntityNotFoundError, Repository } from 'typeorm';
|
||||||
|
import { ProductCategories } from './entities/product-category.entity';
|
||||||
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
|
import { CreateCategoriesProductDto } from './dto/categories/create-categories-product.dto';
|
||||||
|
import { UpdateCategoriesProductDto } from './dto/categories/update-categories-product.dto';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class ProductCategoriesService {
|
||||||
|
constructor(
|
||||||
|
@InjectRepository(ProductCategories)
|
||||||
|
private productCategoriesRepository: Repository<ProductCategories>,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
async create(CreateCategoriesProductDto: CreateCategoriesProductDto) {
|
||||||
|
const result = await this.productCategoriesRepository.insert(
|
||||||
|
CreateCategoriesProductDto,
|
||||||
|
);
|
||||||
|
|
||||||
|
return this.productCategoriesRepository.findOneOrFail(
|
||||||
|
result.identifiers[0].id,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
findAll(page) {
|
||||||
|
return this.productCategoriesRepository.findAndCount({
|
||||||
|
skip: page * 10,
|
||||||
|
take: 10,
|
||||||
|
order: {
|
||||||
|
version: 'DESC',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async findOne(id: string) {
|
||||||
|
try {
|
||||||
|
return await this.productCategoriesRepository.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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async update(
|
||||||
|
id: string,
|
||||||
|
updateCategoriesProductDto: UpdateCategoriesProductDto,
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
await this.productCategoriesRepository.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 result = await this.productCategoriesRepository.update(
|
||||||
|
id,
|
||||||
|
updateCategoriesProductDto,
|
||||||
|
);
|
||||||
|
|
||||||
|
return this.productCategoriesRepository.findOneOrFail(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
async remove(id: string) {
|
||||||
|
try {
|
||||||
|
await this.productCategoriesRepository.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.productCategoriesRepository.delete(id);
|
||||||
|
}
|
||||||
|
}
|
100
src/product/product-sub-categories.service.ts
Normal file
100
src/product/product-sub-categories.service.ts
Normal file
|
@ -0,0 +1,100 @@
|
||||||
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||||
|
import { EntityNotFoundError, Repository } from 'typeorm';
|
||||||
|
import { ProductSubCategories } from './entities/product-sub-category.entity';
|
||||||
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
|
import { CreateSubCategoriesProductDto } from './dto/sub-categories/create-sub-categories-product.dto';
|
||||||
|
import { UpdateSubCategoriesProductDto } from './dto/sub-categories/update-sub-categories-product.dto';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class ProductSubCategoriesService {
|
||||||
|
constructor(
|
||||||
|
@InjectRepository(ProductSubCategories)
|
||||||
|
private productSubCategoriesRepository: Repository<ProductSubCategories>,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
async create(CreateCategoriesProductDto: CreateSubCategoriesProductDto) {
|
||||||
|
const result = await this.productSubCategoriesRepository.insert(
|
||||||
|
CreateCategoriesProductDto,
|
||||||
|
);
|
||||||
|
|
||||||
|
return this.productSubCategoriesRepository.findOneOrFail(
|
||||||
|
result.identifiers[0].id,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
findAll(page) {
|
||||||
|
return this.productSubCategoriesRepository.findAndCount({
|
||||||
|
skip: page * 10,
|
||||||
|
take: 10,
|
||||||
|
order: {
|
||||||
|
version: 'DESC',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async findOne(id: string) {
|
||||||
|
try {
|
||||||
|
return await this.productSubCategoriesRepository.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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async update(
|
||||||
|
id: string,
|
||||||
|
updateCategoriesProductDto: UpdateSubCategoriesProductDto,
|
||||||
|
) {
|
||||||
|
try {
|
||||||
|
await this.productSubCategoriesRepository.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 result = await this.productSubCategoriesRepository.update(
|
||||||
|
id,
|
||||||
|
updateCategoriesProductDto,
|
||||||
|
);
|
||||||
|
|
||||||
|
return this.productSubCategoriesRepository.findOneOrFail(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
async remove(id: string) {
|
||||||
|
try {
|
||||||
|
await this.productSubCategoriesRepository.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.productSubCategoriesRepository.delete(id);
|
||||||
|
}
|
||||||
|
}
|
20
src/product/product.controller.spec.ts
Normal file
20
src/product/product.controller.spec.ts
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
|
import { ProductController } from './product.controller';
|
||||||
|
import { ProductService } from './product.service';
|
||||||
|
|
||||||
|
describe('ProductController', () => {
|
||||||
|
let controller: ProductController;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
|
controllers: [ProductController],
|
||||||
|
providers: [ProductService],
|
||||||
|
}).compile();
|
||||||
|
|
||||||
|
controller = module.get<ProductController>(ProductController);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be defined', () => {
|
||||||
|
expect(controller).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
60
src/product/product.controller.ts
Normal file
60
src/product/product.controller.ts
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
import {
|
||||||
|
Controller,
|
||||||
|
Get,
|
||||||
|
Post,
|
||||||
|
Body,
|
||||||
|
Put,
|
||||||
|
Param,
|
||||||
|
Delete,
|
||||||
|
ParseUUIDPipe,
|
||||||
|
HttpStatus, Query,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { ProductService } from './product.service';
|
||||||
|
import { ProductCategoriesService } from './product-categories.service';
|
||||||
|
import { CreateCategoriesProductDto } from './dto/categories/create-categories-product.dto';
|
||||||
|
|
||||||
|
@Controller({
|
||||||
|
path: 'product',
|
||||||
|
version: '1',
|
||||||
|
})
|
||||||
|
export class ProductController {
|
||||||
|
constructor(
|
||||||
|
private readonly productService: ProductService,
|
||||||
|
private readonly productCategoriesService: ProductCategoriesService,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
@Post('categories')
|
||||||
|
async createCategories(
|
||||||
|
@Body() createCategoriesProductDto: CreateCategoriesProductDto,
|
||||||
|
) {
|
||||||
|
return {
|
||||||
|
data: await this.productCategoriesService.create(
|
||||||
|
createCategoriesProductDto,
|
||||||
|
),
|
||||||
|
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(':id')
|
||||||
|
async findOne(@Param('id', ParseUUIDPipe) id: string) {
|
||||||
|
return {
|
||||||
|
data: await this.productService.findOne(id),
|
||||||
|
statusCode: HttpStatus.OK,
|
||||||
|
message: 'success',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
10
src/product/product.module.ts
Normal file
10
src/product/product.module.ts
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { ProductService } from './product.service';
|
||||||
|
import { ProductController } from './product.controller';
|
||||||
|
import { ProductCategoriesService } from './product-categories.service';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
controllers: [ProductController],
|
||||||
|
providers: [ProductService, ProductCategoriesService],
|
||||||
|
})
|
||||||
|
export class ProductModule {}
|
18
src/product/product.service.spec.ts
Normal file
18
src/product/product.service.spec.ts
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
|
import { ProductService } from './product.service';
|
||||||
|
|
||||||
|
describe('ProductService', () => {
|
||||||
|
let service: ProductService;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
|
providers: [ProductService],
|
||||||
|
}).compile();
|
||||||
|
|
||||||
|
service = module.get<ProductService>(ProductService);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be defined', () => {
|
||||||
|
expect(service).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
51
src/product/product.service.ts
Normal file
51
src/product/product.service.ts
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||||
|
import { EntityNotFoundError, Repository } from 'typeorm';
|
||||||
|
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 { CreateProductDto } from '../product/dto/create-product.dto';
|
||||||
|
import { CreateCategoriesProductDto } from './dto/categories/create-categories-product.dto';
|
||||||
|
import { CreateSubCategoriesProductDto } from './dto/sub-categories/create-sub-categories-product.dto';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class ProductService {
|
||||||
|
constructor(
|
||||||
|
@InjectRepository(Product)
|
||||||
|
private productRepository: Repository<Product>,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
async create(createProductDto: CreateProductDto) {
|
||||||
|
const result = await this.productRepository.insert(createProductDto);
|
||||||
|
|
||||||
|
return this.productRepository.findOneOrFail(result.identifiers[0].id);
|
||||||
|
}
|
||||||
|
|
||||||
|
findAll(page) {
|
||||||
|
return this.productRepository.findAndCount({
|
||||||
|
skip: page * 10,
|
||||||
|
take: 10,
|
||||||
|
order: {
|
||||||
|
version: 'DESC',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async findOne(id: string) {
|
||||||
|
try {
|
||||||
|
return 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user