add: create crud product

This commit is contained in:
ilham 2021-12-06 00:07:22 +07:00
parent 1fc5706e47
commit 682b973329
16 changed files with 404 additions and 4 deletions

View File

@ -0,0 +1,6 @@
import { IsNotEmpty, IsUUID } from 'class-validator';
export class CreateCategoriesProductDto {
@IsNotEmpty()
name: string;
}

View File

@ -0,0 +1,6 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreateCategoriesProductDto } from './create-categories-product.dto';
export class UpdateCategoriesProductDto extends PartialType(
CreateCategoriesProductDto,
) {}

View 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;
}

View File

@ -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;
}

View File

@ -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,
) {}

View File

@ -8,7 +8,7 @@ import {
CreateDateColumn,
OneToMany,
} from 'typeorm';
import { ProductSubCategories } from './productSubCategory.entity';
import { ProductSubCategories } from './product-sub-category.entity';
import { BaseModel } from '../../config/basemodel.entity';
@Entity()

View File

@ -8,7 +8,7 @@ import {
CreateDateColumn,
ManyToOne,
} from 'typeorm';
import { ProductCategories } from './productCategory.entity';
import { ProductCategories } from './product-category.entity';
import { BaseModel } from '../../config/basemodel.entity';
@Entity()

View File

@ -7,8 +7,9 @@ import {
VersionColumn,
CreateDateColumn,
OneToMany,
ManyToOne,
} from 'typeorm';
import { ProductSubCategories } from './productSubCategory.entity';
import { ProductSubCategories } from './product-sub-category.entity';
import { BaseModel } from '../../config/basemodel.entity';
@Entity()
@ -25,7 +26,7 @@ export class Product extends BaseModel{
@Column()
status: string;
@OneToMany(
@ManyToOne(
() => ProductSubCategories,
(subCategories) => subCategories.category,
)

View 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);
}
}

View 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);
}
}

View 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();
});
});

View 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',
};
}
}

View 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 {}

View 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();
});
});

View 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;
}
}
}
}