diff --git a/api/migrations/1609755423492-Product.ts b/api/migrations/1609755423492-Product.ts new file mode 100644 index 0000000..845a518 --- /dev/null +++ b/api/migrations/1609755423492-Product.ts @@ -0,0 +1,14 @@ +import {MigrationInterface, QueryRunner} from "typeorm"; + +export class Product1609755423492 implements MigrationInterface { + name = 'Product1609755423492' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`CREATE TABLE "product" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "title" character varying NOT NULL, "description" character varying, "unitPrice" integer NOT NULL DEFAULT 0, CONSTRAINT "PK_bebc9158e480b949565b4dc7a82" PRIMARY KEY ("id"))`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP TABLE "product"`); + } + +} diff --git a/api/src/Domain/Product/Product.entity.spec.ts b/api/src/Domain/Product/Product.entity.spec.ts new file mode 100644 index 0000000..42eb734 --- /dev/null +++ b/api/src/Domain/Product/Product.entity.spec.ts @@ -0,0 +1,15 @@ +import { Product } from './Product.entity'; + +describe('Product', () => { + it('testGetters', () => { + const product = new Product( + 'Mug', + 'Mug avec le portrait de l\'enfant', + 999 + ); + expect(product.getId()).toBeUndefined(); + expect(product.getTitle()).toBe('Mug'); + expect(product.getDescription()).toBe('Mug avec le portrait de l\'enfant'); + expect(product.getUnitPrice()).toBe(999); + }); +}); diff --git a/api/src/Domain/Product/Product.entity.ts b/api/src/Domain/Product/Product.entity.ts new file mode 100644 index 0000000..d40956a --- /dev/null +++ b/api/src/Domain/Product/Product.entity.ts @@ -0,0 +1,38 @@ +import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; + +@Entity() +export class Product { + @PrimaryGeneratedColumn('uuid') + private id: string; + + @Column({ type: 'varchar', nullable: false }) + private title: string; + + @Column({ type: 'varchar', nullable: true }) + private description: string; + + @Column({ type: 'integer', nullable: false, default: 0 }) + private unitPrice: number; + + constructor(title: string, description: string, unitPrice: number) { + this.title = title; + this.description = description; + this.unitPrice = unitPrice; + } + + public getId(): string { + return this.id; + } + + public getTitle(): string { + return this.title; + } + + public getDescription(): string { + return this.description; + } + + public getUnitPrice(): number { + return this.unitPrice; + } +} diff --git a/api/src/Domain/Product/Repository/IProductRepository.ts b/api/src/Domain/Product/Repository/IProductRepository.ts new file mode 100644 index 0000000..40a4615 --- /dev/null +++ b/api/src/Domain/Product/Repository/IProductRepository.ts @@ -0,0 +1,5 @@ +import { Product } from '../Product.entity'; + +export interface IProductRepository { + save(task: Product): Promise; +} diff --git a/api/src/Infrastructure/Product/Repository/ProductRepository.ts b/api/src/Infrastructure/Product/Repository/ProductRepository.ts new file mode 100644 index 0000000..e27c12d --- /dev/null +++ b/api/src/Infrastructure/Product/Repository/ProductRepository.ts @@ -0,0 +1,17 @@ +import { Injectable } from '@nestjs/common'; +import { Repository } from 'typeorm'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Product } from 'src/Domain/Product/Product.entity'; +import { IProductRepository } from 'src/Domain/Product/Repository/IProductRepository'; + +@Injectable() +export class ProductRepository implements IProductRepository { + constructor( + @InjectRepository(Product) + private readonly repository: Repository + ) {} + + public save(product: Product): Promise { + return this.repository.save(product); + } +} diff --git a/api/src/Infrastructure/Product/product.module.ts b/api/src/Infrastructure/Product/product.module.ts new file mode 100644 index 0000000..de166af --- /dev/null +++ b/api/src/Infrastructure/Product/product.module.ts @@ -0,0 +1,14 @@ +import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { BusModule } from '../bus.module'; +import { Product } from 'src/Domain/Product/Product.entity'; +import { ProductRepository } from './Repository/ProductRepository'; + +@Module({ + imports: [BusModule, TypeOrmModule.forFeature([Product])], + controllers: [], + providers: [ + { provide: 'IProductRepository', useClass: ProductRepository } + ] +}) +export class ProductModule {}