Skip to content

Commit

Permalink
feat: create order repository
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonardoCamargo31 committed Oct 25, 2023
1 parent 0e69dda commit ddef521
Show file tree
Hide file tree
Showing 13 changed files with 199 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/domain/entity/customer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Customer unit tests', () => {
it('should activate customer', () => {
const customer = new Customer('123', 'John')
const address = new Address('Street 1', 123, '12345-678', 'São Paulo', 'SP')
customer.address = address
customer.changeAddress(address)
customer.activate()
expect(customer.isActive()).toBe(true)
})
Expand Down
4 changes: 0 additions & 4 deletions src/domain/entity/customer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ export class Customer {
return this._address
}

set address (address: Address) {
this._address = address
}

isActive (): boolean {
return this._active
}
Expand Down
12 changes: 12 additions & 0 deletions src/domain/entity/order-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ export class OrderItem {
this._total = this.orderItemTotal()
}

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

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

get productId (): string {
return this._productId
}

get price (): number {
return this._price
}
Expand Down
12 changes: 12 additions & 0 deletions src/domain/entity/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ export class Order {
this.validate()
}

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

get customerId (): string {
return this._customerId
}

get items (): OrderItem[] {
return this._items
}

validate (): void {
if (this._id.length === 0) {
throw new Error('id is required')
Expand Down
4 changes: 2 additions & 2 deletions src/infrastructure/database/sequelize/model/customer-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Column, Model, PrimaryKey, Table } from 'sequelize-typescript'
export class CustomerModel extends Model {
@PrimaryKey
@Column
declare id: String
declare id: string

@Column({
allowNull:false
Expand Down Expand Up @@ -47,5 +47,5 @@ export class CustomerModel extends Model {
@Column({
allowNull:false
})
declare rewardPoints: number
declare reward_points: number
}
39 changes: 39 additions & 0 deletions src/infrastructure/database/sequelize/model/order-item-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { BelongsTo, Column, ForeignKey, Model, PrimaryKey, Table } from 'sequelize-typescript'
import { ProductModel } from './product-model'
import { OrderItem } from '../../../../domain/entity/order-item'
import { OrderModel } from './order-model'

@Table({
tableName: 'order_items',
timestamps: false
})
export class OrderItemModel extends Model {
@PrimaryKey
@Column
declare id: string

@ForeignKey(() => ProductModel)
@Column({ allowNull:false })
declare product_id: string

// caso queira todos os dados do produto
@BelongsTo(() => ProductModel)
declare product: ProductModel

@ForeignKey(() => OrderModel)
@Column({ allowNull:false })
declare order_id: string

// caso queira todos os dados da order
@BelongsTo(() => OrderModel)
declare order: OrderItem

@Column({ allowNull:false })
declare quantity: number

@Column({ allowNull:false })
declare name: number

@Column({ allowNull:false })
declare price: number
}
28 changes: 28 additions & 0 deletions src/infrastructure/database/sequelize/model/order-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { BelongsTo, Column, ForeignKey, HasMany, Model, PrimaryKey, Table } from 'sequelize-typescript'
import { CustomerModel } from './customer-model'
import { OrderItemModel } from './order-item-model'

@Table({
tableName: "orders",
timestamps: false,
})
export class OrderModel extends Model {
@PrimaryKey
@Column
declare id: string

@ForeignKey(() => CustomerModel)
@Column({ allowNull: false })
declare customer_id: string

// caso queira todos os dados do cliente
@BelongsTo(() => CustomerModel)
declare customer: CustomerModel

// minha order tem muitos itens, has many
@HasMany(() => OrderItemModel)
declare items: OrderItemModel[]

@Column({ allowNull: false })
declare total: number
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Column, Model, PrimaryKey, Table } from 'sequelize-typescript'
export class ProductModel extends Model {
@PrimaryKey
@Column
declare id: String
declare id: string

@Column({
allowNull:false
Expand Down
14 changes: 7 additions & 7 deletions src/infrastructure/repository/customer-repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('Customer repository test', () => {
const customerRepository = new CustomerRepository()
const customer = new Customer('123', 'customer 1')
const address = new Address('street 1', 1, 'zip 1', 'city 1', 'state 1')
customer.address = address
customer.changeAddress(address)
await customerRepository.create(customer)

const customerModel = await CustomerModel.findOne({ where: { id: '123' } })
Expand All @@ -36,7 +36,7 @@ describe('Customer repository test', () => {
id: '123',
name: customer.name,
active: customer.isActive(),
rewardPoints: customer.rewardPoints,
reward_points: customer.rewardPoints,
street: address.street,
number: address.number,
zip: address.zip,
Expand All @@ -49,7 +49,7 @@ describe('Customer repository test', () => {
const customerRepository = new CustomerRepository()
const customer = new Customer('123', 'customer 1')
const address = new Address('street 1', 1, 'zip 1', 'city 1', 'state 1')
customer.address = address
customer.changeAddress(address)
await customerRepository.create(customer)

customer.changeName('customer 2')
Expand All @@ -60,7 +60,7 @@ describe('Customer repository test', () => {
id: '123',
name: customer.name,
active: customer.isActive(),
rewardPoints: customer.rewardPoints,
reward_points: customer.rewardPoints,
street: address.street,
number: address.number,
zip: address.zip,
Expand All @@ -73,7 +73,7 @@ describe('Customer repository test', () => {
const customerRepository = new CustomerRepository()
const customer = new Customer('123', 'customer 1')
const address = new Address('street 1', 1, 'zip 1', 'city 1', 'state 1')
customer.address = address
customer.changeAddress(address)
await customerRepository.create(customer)

const customerResult = await customerRepository.find(customer.id)
Expand All @@ -93,13 +93,13 @@ describe('Customer repository test', () => {
const customerRepository = new CustomerRepository()
const customer1 = new Customer('123', 'Customer 1')
const address1 = new Address('street 1', 1, 'zip 1', 'city 1', 'state 1')
customer1.address = address1
customer1.changeAddress(address1)
customer1.addRewardPoints(10)
customer1.activate()

const customer2 = new Customer('456', 'Customer 2')
const address2 = new Address('street 2', 2, 'zip 2', 'city 2', 'state 2')
customer2.address = address2
customer2.changeAddress(address2)
customer2.addRewardPoints(20)

await customerRepository.create(customer1)
Expand Down
8 changes: 4 additions & 4 deletions src/infrastructure/repository/customer-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class CustomerRepository implements CustomerRepositoryInterface {
city: entity.address.city,
state: entity.address.state,
active: entity.isActive(),
rewardPoints: entity.rewardPoints
reward_points: entity.rewardPoints
})
}

Expand All @@ -28,7 +28,7 @@ export default class CustomerRepository implements CustomerRepositoryInterface {
city: entity.address.city,
state: entity.address.state,
active: entity.isActive(),
rewardPoints: entity.rewardPoints
reward_points: entity.rewardPoints
},
{
where: {
Expand Down Expand Up @@ -66,8 +66,8 @@ export default class CustomerRepository implements CustomerRepositoryInterface {
const customerModels = await CustomerModel.findAll()

const customers = customerModels.map((customerModels) => {
const customer = new Customer(customerModels.id as string, customerModels.name)
customer.addRewardPoints(customerModels.rewardPoints)
const customer = new Customer(customerModels.id, customerModels.name)
customer.addRewardPoints(customerModels.reward_points)
const address = new Address(
customerModels.street,
customerModels.number,
Expand Down
69 changes: 69 additions & 0 deletions src/infrastructure/repository/order-repository.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Sequelize } from 'sequelize-typescript'
import { CustomerModel } from '../database/sequelize/model/customer-model'
import CustomerRepository from './customer-repository'
import { Customer } from '../../domain/entity/customer'
import { Address } from '../../domain/entity/address'
import { OrderModel } from '../database/sequelize/model/order-model'
import { OrderItemModel } from '../database/sequelize/model/order-item-model'
import { ProductModel } from '../database/sequelize/model/product-model'
import { ProductRepository } from './product-repository'
import { Product } from '../../domain/entity/product'
import { OrderItem } from '../../domain/entity/order-item'
import { Order } from '../../domain/entity/order'
import OrderRepository from './order-repository'

describe('Order repository test', () => {
let sequelize: Sequelize

beforeEach(async () => {
sequelize = new Sequelize({
dialect: 'sqlite',
storage: ':memory:',
logging: false,
sync: { force: true }
})

await sequelize.addModels([CustomerModel, OrderModel, OrderItemModel, ProductModel])
await sequelize.sync()
})

afterEach(async () => {
await sequelize.close()
})

it('should create a new order', async () => {
const customerRepository = new CustomerRepository()
const customer = new Customer('123', 'customer 1')
const address = new Address('Street 1', 123, '12345-678', 'São Paulo', 'SP')
customer.changeAddress(address)
await customerRepository.create(customer)

const productRepository = new ProductRepository()
const product = new Product('1', 'product 1', 100)
await productRepository.create(product)

const item = new OrderItem('123', product.name, product.price, product.id, 2)
const order = new Order('123', '123', [item])
const orderRepository = new OrderRepository()
await orderRepository.create(order)

const orderModel = await OrderModel.findOne({
where: { id: order.id },
include: ['items']
})

expect(orderModel.toJSON()).toStrictEqual({
id: order.id,
customer_id: customer.id,
total: order.total(),
items: [{
id: item.id,
name: item.name,
price: item.price,
quantity: item.quantity,
order_id: order.id,
product_id: item.productId
}]
})
})
})
22 changes: 22 additions & 0 deletions src/infrastructure/repository/order-repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Order } from '../../domain/entity/order'
import { OrderItemModel } from '../database/sequelize/model/order-item-model'
import { OrderModel } from '../database/sequelize/model/order-model'

export default class OrderRepository {
async create (entity: Order): Promise<void> {
await OrderModel.create({
id: entity.id,
customer_id: entity.customerId,
total: entity.total(),
items: entity.items.map((item) => ({
id: item.id,
name: item.name,
price: item.price,
product_id: item.productId,
quantity: item.quantity
}))
}, {
include: [{ model: OrderItemModel }]
})
}
}
4 changes: 2 additions & 2 deletions src/infrastructure/repository/product-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class ProductRepository implements ProductRepositoryInterface {
})

return new Product(
productModel.id as string,
productModel.id,
productModel.name,
productModel.price
)
Expand All @@ -38,7 +38,7 @@ export class ProductRepository implements ProductRepositoryInterface {
const productModels = await ProductModel.findAll()
return productModels.map(productModel =>
new Product(
productModel.id as string,
productModel.id,
productModel.name,
productModel.price
)
Expand Down

0 comments on commit ddef521

Please sign in to comment.