parent
5c14ac394c
commit
5777f9f85e
15 changed files with 166 additions and 15 deletions
@ -0,0 +1,5 @@ |
||||
export class CreateProductDto { |
||||
productName: string; |
||||
quantityInStock: number; |
||||
pricePerUnit: number; |
||||
} |
@ -0,0 +1,2 @@ |
||||
export * from './create-product.dto'; |
||||
export * from './update-product.dto'; |
@ -0,0 +1,4 @@ |
||||
import { PartialType } from '@nestjs/mapped-types'; |
||||
import { CreateProductDto } from './create-product.dto'; |
||||
|
||||
export class UpdateProductDto extends PartialType(CreateProductDto) {} |
@ -0,0 +1,24 @@ |
||||
import { Table, Column, Model, DataType } from 'sequelize-typescript'; |
||||
|
||||
@Table({ tableName: 'products' }) |
||||
export class Product extends Model<Product> { |
||||
@Column({ |
||||
type: DataType.STRING, |
||||
allowNull: false, |
||||
}) |
||||
productName: string; |
||||
|
||||
@Column({ |
||||
type: DataType.INTEGER, |
||||
allowNull: false, |
||||
defaultValue: 0, |
||||
}) |
||||
quantityInStock: number; |
||||
|
||||
@Column({ |
||||
type: DataType.FLOAT, |
||||
allowNull: false, |
||||
defaultValue: 0, |
||||
}) |
||||
pricePerUnit: number; |
||||
} |
@ -0,0 +1,42 @@ |
||||
import { |
||||
Controller, |
||||
Get, |
||||
Post, |
||||
Body, |
||||
Patch, |
||||
Param, |
||||
Delete, |
||||
} from '@nestjs/common'; |
||||
import { ProductsService } from './products.service'; |
||||
import { CreateProductDto, UpdateProductDto } from './dto'; |
||||
import { UUID } from 'crypto'; |
||||
|
||||
@Controller('products') |
||||
export class ProductsController { |
||||
constructor(private readonly productsService: ProductsService) {} |
||||
|
||||
@Post() |
||||
create(@Body() createProductDto: CreateProductDto) { |
||||
return this.productsService.create(createProductDto); |
||||
} |
||||
|
||||
@Get() |
||||
findAll() { |
||||
return this.productsService.findAll(); |
||||
} |
||||
|
||||
@Get(':id') |
||||
findOne(@Param('id') id: string) { |
||||
return this.productsService.findOne(+id); |
||||
} |
||||
|
||||
@Patch(':id') |
||||
update(@Param('id') id: string, @Body() updateProductDto: UpdateProductDto) { |
||||
return this.productsService.update(+id, updateProductDto); |
||||
} |
||||
|
||||
@Delete(':id') |
||||
remove(@Param('id') id: string) { |
||||
return this.productsService.remove(+id); |
||||
} |
||||
} |
@ -0,0 +1,11 @@ |
||||
import { Module } from '@nestjs/common'; |
||||
import { ProductsService } from './products.service'; |
||||
import { ProductsController } from './products.controller'; |
||||
import { productsProviders } from './products.providers'; |
||||
|
||||
@Module({ |
||||
controllers: [ProductsController], |
||||
exports: [ProductsService], |
||||
providers: [ProductsService, ...productsProviders], |
||||
}) |
||||
export class ProductsModule {} |
@ -0,0 +1,9 @@ |
||||
import { Product } from './entities/product.entity'; |
||||
import { PRODUCT_REPOSITORY } from '../../core/constants'; |
||||
|
||||
export const productsProviders = [ |
||||
{ |
||||
provide: PRODUCT_REPOSITORY, |
||||
useValue: Product, |
||||
}, |
||||
]; |
@ -0,0 +1,41 @@ |
||||
import { Inject, Injectable } from '@nestjs/common'; |
||||
import { CreateProductDto, UpdateProductDto } from './dto'; |
||||
import { PRODUCT_REPOSITORY } from 'src/core/constants'; |
||||
import { Product } from './entities/product.entity'; |
||||
|
||||
@Injectable() |
||||
export class ProductsService { |
||||
constructor( |
||||
@Inject(PRODUCT_REPOSITORY) |
||||
private readonly productRepository: typeof Product, |
||||
) {} |
||||
|
||||
async create(createProductDto: CreateProductDto) { |
||||
return await this.productRepository.create(createProductDto); |
||||
} |
||||
|
||||
async findAll() { |
||||
return await this.productRepository.findAll(); |
||||
} |
||||
|
||||
async findOne(id: number) { |
||||
return await this.productRepository.findAll({ where: { id } }); |
||||
} |
||||
|
||||
async update(id: number, updateProductDto: UpdateProductDto) { |
||||
const [numberOfAffectedRows, [updatedProduct]] = |
||||
await this.productRepository.update( |
||||
{ ...updateProductDto }, |
||||
{ where: { id }, returning: true }, |
||||
); |
||||
return { numberOfAffectedRows, updatedProduct }; |
||||
} |
||||
|
||||
async remove(id: number) { |
||||
const deletedProduct = await this.findOne(id); |
||||
|
||||
await this.productRepository.destroy({ where: { id } }); |
||||
|
||||
return deletedProduct; |
||||
} |
||||
} |
Loading…
Reference in new issue