-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
280 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ICommand } from 'src/Application/ICommand'; | ||
|
||
export class CreateProductCommand implements ICommand { | ||
constructor( | ||
public readonly title: string, | ||
public readonly description: string, | ||
public readonly unitPrice: number | ||
) {} | ||
} |
83 changes: 83 additions & 0 deletions
83
api/src/Application/Product/Command/CreateProductCommandHandler.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { mock, instance, when, verify, deepEqual, anything } from 'ts-mockito'; | ||
import { ProductRepository } from 'src/Infrastructure/Product/Repository/ProductRepository'; | ||
import { IsProductAlreadyExist } from 'src/Domain/Product/Specification/IsProductAlreadyExist'; | ||
import { Product } from 'src/Domain/Product/Product.entity'; | ||
import { CreateProductCommandHandler } from 'src/Application/Product/Command/CreateProductCommandHandler'; | ||
import { CreateProductCommand } from 'src/Application/Product/Command/CreateProductCommand'; | ||
import { ProductAlreadyExistException } from 'src/Domain/Product/Exception/ProductAlreadyExistException'; | ||
import { Photographer } from 'src/Domain/User/Photographer.entity'; | ||
|
||
describe('CreateProductCommandHandler', () => { | ||
let productRepository: ProductRepository; | ||
let isProductAlreadyExist: IsProductAlreadyExist; | ||
let createdProduct: Product; | ||
let handler: CreateProductCommandHandler; | ||
|
||
const photographer = mock(Photographer); | ||
const command = new CreateProductCommand( | ||
'Mug', | ||
'Mug portrait enfant', | ||
9.99, | ||
); | ||
|
||
beforeEach(() => { | ||
productRepository = mock(ProductRepository); | ||
isProductAlreadyExist = mock(IsProductAlreadyExist); | ||
createdProduct = mock(Product); | ||
|
||
handler = new CreateProductCommandHandler( | ||
instance(productRepository), | ||
instance(isProductAlreadyExist) | ||
); | ||
}); | ||
|
||
it('testProductCreatedSuccessfully', async () => { | ||
when(isProductAlreadyExist.isSatisfiedBy('Mug')).thenResolve(false); | ||
when(createdProduct.getId()).thenReturn( | ||
'2d5fb4da-12c2-11ea-8d71-362b9e155667' | ||
); | ||
when( | ||
productRepository.save( | ||
deepEqual( | ||
new Product( | ||
'Mug', | ||
'Mug portrait enfant', | ||
999, | ||
) | ||
) | ||
) | ||
).thenResolve(instance(createdProduct)); | ||
|
||
expect(await handler.execute(command)).toBe( | ||
'2d5fb4da-12c2-11ea-8d71-362b9e155667' | ||
); | ||
|
||
verify(isProductAlreadyExist.isSatisfiedBy('Mug')).once(); | ||
verify( | ||
productRepository.save( | ||
deepEqual( | ||
new Product( | ||
'Mug', | ||
'Mug portrait enfant', | ||
999, | ||
) | ||
) | ||
) | ||
).once(); | ||
verify(createdProduct.getId()).once(); | ||
}); | ||
|
||
it('testProductAlreadyExist', async () => { | ||
when(isProductAlreadyExist.isSatisfiedBy('Mug')).thenResolve(true); | ||
|
||
try { | ||
await handler.execute(command); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(ProductAlreadyExistException); | ||
expect(e.message).toBe('products.errors.already_exist'); | ||
verify(isProductAlreadyExist.isSatisfiedBy('Mug')).once(); | ||
verify(productRepository.save(anything())).never(); | ||
verify(createdProduct.getId()).never(); | ||
} | ||
}); | ||
}); |
30 changes: 30 additions & 0 deletions
30
api/src/Application/Product/Command/CreateProductCommandHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Inject } from '@nestjs/common'; | ||
import { CommandHandler } from '@nestjs/cqrs'; | ||
import { ProductAlreadyExistException } from 'src/Domain/Product/Exception/ProductAlreadyExistException'; | ||
import { IProductRepository } from 'src/Domain/Product/Repository/IProductRepository'; | ||
import { Product } from 'src/Domain/Product/Product.entity'; | ||
import { IsProductAlreadyExist } from 'src/Domain/Product/Specification/IsProductAlreadyExist'; | ||
import { CreateProductCommand } from './CreateProductCommand'; | ||
|
||
@CommandHandler(CreateProductCommand) | ||
export class CreateProductCommandHandler { | ||
constructor( | ||
@Inject('IProductRepository') | ||
private readonly productRepository: IProductRepository, | ||
private readonly isProductAlreadyExist: IsProductAlreadyExist | ||
) {} | ||
|
||
public async execute(command: CreateProductCommand): Promise<string> { | ||
const { title, description, unitPrice } = command; | ||
|
||
if (true === (await this.isProductAlreadyExist.isSatisfiedBy(title))) { | ||
throw new ProductAlreadyExistException(); | ||
} | ||
|
||
const product = await this.productRepository.save( | ||
new Product(title, description, Math.round(unitPrice * 100)) | ||
); | ||
|
||
return product.getId(); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
api/src/Domain/Product/Exception/ProductAlreadyExistException.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export class ProductAlreadyExistException extends Error { | ||
constructor() { | ||
super('products.errors.already_exist'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
api/src/Domain/Product/Specification/IsProductAlreadyExist.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { mock, instance, when, verify, anything } from 'ts-mockito'; | ||
import { ProductRepository } from 'src/Infrastructure/Product/Repository/ProductRepository'; | ||
import { IsProductAlreadyExist } from 'src/Domain/Product/Specification/IsProductAlreadyExist'; | ||
import { Product } from 'src/Domain/Product/Product.entity'; | ||
|
||
describe('IsProductAlreadyExist', () => { | ||
let productRepository: ProductRepository; | ||
let isProductAlreadyExist: IsProductAlreadyExist; | ||
|
||
beforeEach(() => { | ||
productRepository = mock(ProductRepository); | ||
isProductAlreadyExist = new IsProductAlreadyExist( | ||
instance(productRepository) | ||
); | ||
}); | ||
|
||
it('testProductAlreadyExist', async () => { | ||
when(productRepository.findOneByTitle('Mug')).thenResolve( | ||
new Product('Mug', anything(), anything()) | ||
); | ||
expect(await isProductAlreadyExist.isSatisfiedBy('Mug')).toBe( | ||
true | ||
); | ||
verify(productRepository.findOneByTitle('Mug')).once(); | ||
}); | ||
|
||
it('testProductDontExist', async () => { | ||
when(productRepository.findOneByTitle('Mug')).thenResolve(null); | ||
expect(await isProductAlreadyExist.isSatisfiedBy('Mug')).toBe( | ||
false | ||
); | ||
verify(productRepository.findOneByTitle('Mug')).once(); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
api/src/Domain/Product/Specification/IsProductAlreadyExist.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Inject } from '@nestjs/common'; | ||
import { IProductRepository } from '../Repository/IProductRepository'; | ||
import { Product } from '../Product.entity'; | ||
|
||
export class IsProductAlreadyExist { | ||
constructor( | ||
@Inject('IProductRepository') | ||
private readonly productRepository: IProductRepository | ||
) {} | ||
|
||
public async isSatisfiedBy(title: string): Promise<boolean> { | ||
return ( | ||
(await this.productRepository.findOneByTitle(title)) instanceof Product | ||
); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
api/src/Infrastructure/Product/Action/CreateProductAction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { | ||
Controller, | ||
Inject, | ||
Post, | ||
Body, | ||
BadRequestException, | ||
UseGuards | ||
} from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger'; | ||
import { ICommandBus } from 'src/Application/ICommandBus'; | ||
import { CreateProductCommand } from 'src/Application/Product/Command/CreateProductCommand'; | ||
import { ProductDTO } from '../DTO/ProductDTO'; | ||
|
||
@Controller('products') | ||
@ApiTags('Product') | ||
@ApiBearerAuth() | ||
@UseGuards(AuthGuard('bearer')) | ||
export class CreateProductAction { | ||
constructor( | ||
@Inject('ICommandBus') | ||
private readonly commandBus: ICommandBus | ||
) {} | ||
|
||
@Post() | ||
@ApiOperation({ summary: 'Create new product' }) | ||
public async index(@Body() dto: ProductDTO) { | ||
const { title, description, unitPrice } = dto; | ||
|
||
try { | ||
const id = await this.commandBus.execute( | ||
new CreateProductCommand(title, description, unitPrice) | ||
); | ||
|
||
return { id }; | ||
} catch (e) { | ||
throw new BadRequestException(e.message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { ProductDTO } from './ProductDTO'; | ||
import { validate } from 'class-validator'; | ||
|
||
describe('ProductDTO', () => { | ||
it('testValidDTO', async () => { | ||
const dto = new ProductDTO(); | ||
dto.title = 'Mug'; | ||
dto.description = 'Mug portrait enfant'; | ||
dto.unitPrice = 999; | ||
|
||
const validation = await validate(dto); | ||
expect(validation).toHaveLength(0); | ||
}); | ||
|
||
it('testInvalidDTO', async () => { | ||
const dto = new ProductDTO(); | ||
|
||
const validation = await validate(dto); | ||
expect(validation).toHaveLength(2); | ||
expect(validation[0].constraints).toMatchObject({ | ||
isNotEmpty: "title should not be empty" | ||
}); | ||
expect(validation[1].constraints).toMatchObject({ | ||
isPositive: 'unitPrice must be a positive number' | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsNotEmpty, IsPositive } from 'class-validator'; | ||
|
||
export class ProductDTO { | ||
@IsNotEmpty() | ||
@ApiProperty() | ||
public title: string; | ||
|
||
@ApiProperty() | ||
public description: string; | ||
|
||
@ApiProperty() | ||
@IsPositive() | ||
@IsNotEmpty() | ||
public unitPrice: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters