Create the products module

master
Mahdi 2 weeks ago
parent 5c14ac394c
commit 5777f9f85e
  1. 2
      src/app.module.ts
  2. 1
      src/core/constants/index.ts
  3. 5
      src/core/database/database.providers.ts
  4. 5
      src/modules/products/dto/create-product.dto.ts
  5. 2
      src/modules/products/dto/index.ts
  6. 4
      src/modules/products/dto/update-product.dto.ts
  7. 24
      src/modules/products/entities/product.entity.ts
  8. 42
      src/modules/products/products.controller.ts
  9. 11
      src/modules/products/products.module.ts
  10. 9
      src/modules/products/products.providers.ts
  11. 41
      src/modules/products/products.service.ts
  12. 0
      src/modules/users/entities/user.entity.ts
  13. 10
      src/modules/users/users.controller.ts
  14. 2
      src/modules/users/users.providers.ts
  15. 23
      src/modules/users/users.service.ts

@ -5,12 +5,14 @@ import { DatabaseModule } from './core/database/database.module';
import { ConfigModule } from '@nestjs/config';
import { UsersModule } from './modules/users/users.module';
import { UsersController } from './modules/users/users.controller';
import { ProductsModule } from './modules/products/products.module';
@Module({
imports: [
DatabaseModule,
ConfigModule.forRoot({ isGlobal: true }),
UsersModule,
ProductsModule,
],
controllers: [AppController, UsersController],
providers: [AppService],

@ -3,3 +3,4 @@ export const DEVELOPMENT = 'development';
export const TEST = 'test';
export const PRODUCTION = 'production';
export const USER_REPOSITORY = 'USER_REPOSITORY';
export const PRODUCT_REPOSITORY = 'PRODUCT_REPOSITORY';

@ -1,7 +1,8 @@
import { Sequelize } from 'sequelize-typescript';
import { SEQUELIZE, DEVELOPMENT, TEST, PRODUCTION } from '../constants';
import { databaseConfig } from './database.config';
import { User } from 'src/modules/users/user.entity';
import { User } from 'src/modules/users/entities/user.entity';
import { Product } from 'src/modules/products/entities/product.entity';
export const databaseProviders = [
{
@ -22,7 +23,7 @@ export const databaseProviders = [
config = databaseConfig.development;
}
const sequelize = new Sequelize(config);
sequelize.addModels([User]);
sequelize.addModels([User, Product]);
await sequelize.sync();
return sequelize;
},

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

@ -26,13 +26,13 @@ export class UsersController {
}
@Post()
create(@Body() user: CreateUserDto) {
return this.usersService.create(user);
create(@Body() createUserDto: CreateUserDto) {
return this.usersService.create(createUserDto);
}
@Patch()
update(@Param('id') id: UUID, @Body() user: UpdateUserDto) {
return this.usersService.update(id, user);
@Patch(':id')
update(@Param('id') id: UUID, @Body() updateUserDto: UpdateUserDto) {
return this.usersService.update(id, updateUserDto);
}
@Delete(':id')

@ -1,4 +1,4 @@
import { User } from './user.entity';
import { User } from './entities/user.entity';
import { USER_REPOSITORY } from '../../core/constants';
export const usersProviders = [

@ -1,5 +1,5 @@
import { Injectable, Inject } from '@nestjs/common';
import { User } from './user.entity';
import { User } from './entities/user.entity';
import { CreateUserDto, UpdateUserDto } from './dto';
import { USER_REPOSITORY } from '../../core/constants';
import * as argon from 'argon2';
@ -20,11 +20,11 @@ export class UsersService {
return await this.userRepository.findAll({ where: { uuid: id } });
}
async create(user: CreateUserDto) {
const hashedPassword = await argon.hash(user.password);
user.password = hashedPassword;
async create(createUserDto: CreateUserDto) {
const hashedPassword = await argon.hash(createUserDto.password);
createUserDto.password = hashedPassword;
const newUser = await this.userRepository.create(user);
const newUser = await this.userRepository.create(createUserDto);
return _.pick(newUser, [
'firstName',
@ -36,10 +36,19 @@ export class UsersService {
]);
}
async update(id: UUID, user: UpdateUserDto) {
// async update(id: UUID, updateUserDto: UpdateUserDto) {
// const [numberOfAffectedRows, [updatedUser]] =
// await this.userRepository.update(
// { ...updateUserDto },
// { where: { uuid: id }, returning: true },
// );
// return { numberOfAffectedRows, updatedUser };
// }
async update(id: UUID, updateUserDto: UpdateUserDto) {
const [numberOfAffectedRows, [updatedUser]] =
await this.userRepository.update(
{ ...user },
{ ...updateUserDto },
{ where: { uuid: id }, returning: true },
);
return { numberOfAffectedRows, updatedUser };

Loading…
Cancel
Save