Skip to content

Commit

Permalink
Product model (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarchois authored Jan 4, 2021
1 parent 8b57b19 commit ac6f2ca
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 0 deletions.
14 changes: 14 additions & 0 deletions api/migrations/1609755423492-Product.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {MigrationInterface, QueryRunner} from "typeorm";

export class Product1609755423492 implements MigrationInterface {
name = 'Product1609755423492'

public async up(queryRunner: QueryRunner): Promise<void> {
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<void> {
await queryRunner.query(`DROP TABLE "product"`);
}

}
15 changes: 15 additions & 0 deletions api/src/Domain/Product/Product.entity.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
38 changes: 38 additions & 0 deletions api/src/Domain/Product/Product.entity.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
5 changes: 5 additions & 0 deletions api/src/Domain/Product/Repository/IProductRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Product } from '../Product.entity';

export interface IProductRepository {
save(task: Product): Promise<Product>;
}
17 changes: 17 additions & 0 deletions api/src/Infrastructure/Product/Repository/ProductRepository.ts
Original file line number Diff line number Diff line change
@@ -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<Product>
) {}

public save(product: Product): Promise<Product> {
return this.repository.save(product);
}
}
14 changes: 14 additions & 0 deletions api/src/Infrastructure/Product/product.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}

0 comments on commit ac6f2ca

Please sign in to comment.