Skip to content

Commit

Permalink
feat: create factory to customer
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonardoCamargo31 committed Nov 4, 2023
1 parent ae70df8 commit 88f3c65
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/domain/customer/factory/customer-factory.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Address } from '../value-object/address'
import { CustomerFactory } from './customer-factory'

describe('Customer factory unit test', () => {
it('should create a customer', () => {
const customer = CustomerFactory.create('john')
expect(customer.id).toBeDefined()
expect(customer.name).toBe('john')
expect(customer.address).toBeUndefined()
expect(customer.constructor.name).toBe('Customer')
})

it('should create a customer with an address', () => {
const address = new Address('Street 1', 123, '12345-678', 'São Paulo', 'SP')
const customer = CustomerFactory.createWithAddress('john', address)
expect(customer.id).toBeDefined()
expect(customer.name).toBe('john')
expect(customer.address).toBeDefined()
expect(customer.constructor.name).toBe('Customer')
})
})
15 changes: 15 additions & 0 deletions src/domain/customer/factory/customer-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { v4 as uuid } from 'uuid'
import { Customer } from '../entity/customer'
import { Address } from '../value-object/address'

export class CustomerFactory {
static create (name: string): Customer {
return new Customer(uuid(), name)
}

static createWithAddress (name: string, address: Address): Customer {
const customer = new Customer(uuid(), name)
customer.changeAddress(address)
return customer
}
}
5 changes: 5 additions & 0 deletions src/domain/product/entity/product-interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class ProductInterface {
id: string
name: string
price: number
}
4 changes: 3 additions & 1 deletion src/domain/product/entity/product.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export class Product {
import { ProductInterface } from './product-interface'

export class Product implements ProductInterface {
private readonly _id: string
private _name: string
private _price: number
Expand Down
48 changes: 48 additions & 0 deletions src/domain/product/entity/productB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { ProductInterface } from './product-interface'

export class ProductB implements ProductInterface {
private readonly _id: string
private _name: string
private _price: number

constructor (id: string, name: string, price: number) {
this._id = id
this._name = name
this._price = price
this.validate()
}

get id (): string {
return this._id
}

get name (): string {
return this._name
}

get price (): number {
return this._price * 2
}

validate (): void {
if (this._id.length === 0) {
throw new Error('id is required')
}
if (this._name.length === 0) {
throw new Error('name is required')
}
if (this._price <= 0) {
throw new Error('price is less than 0')
}
}

changeName (name: string): void {
this._name = name
this.validate()
}

changePrice (price: number): void {
this._price = price
this.validate()
}
}
19 changes: 19 additions & 0 deletions src/domain/product/factory/product-factory.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ProductFactory } from './product-factory'

describe('Product factory unit test', () => {
it('should create a product type a', () => {
const product = ProductFactory.create('a', 'product a', 1)
expect(product.id).toBeDefined()
expect(product.name).toBe('product a')
expect(product.price).toBe(1)
expect(product.constructor.name).toBe('Product')
})

it('should create a product type b', () => {
const product = ProductFactory.create('b', 'product b', 1)
expect(product.id).toBeDefined()
expect(product.name).toBe('product b')
expect(product.price).toBe(2)
expect(product.constructor.name).toBe('ProductB')
})
})
17 changes: 17 additions & 0 deletions src/domain/product/factory/product-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Product } from '../entity/product'
import { ProductInterface } from '../entity/product-interface'
import { v4 as uuid } from 'uuid'
import { ProductB } from '../entity/productB'

export class ProductFactory {
static create (type: string, name: string, price: number): ProductInterface {
switch (type) {
case 'a':
return new Product(uuid(), name, price)
case 'b':
return new ProductB(uuid(), name, price)
default:
throw new Error('product type not supported')
}
}
}

0 comments on commit 88f3c65

Please sign in to comment.