Skip to content

Commit

Permalink
feat: implement order repository
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonardoCamargo31 committed Oct 25, 2023
1 parent ddef521 commit 609b6e9
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 19 deletions.
6 changes: 6 additions & 0 deletions src/domain/repository/order-repository.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Order } from '../entity/order'
import { RepositoryInterface } from './repository-interface'

export interface OrderRepositoryInterface extends RepositoryInterface<Order> {

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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({
Expand All @@ -26,13 +25,13 @@ export class OrderItemModel extends Model {

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

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

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

@Column({ allowNull:false })
declare price: number
Expand Down
18 changes: 9 additions & 9 deletions src/infrastructure/repository/customer-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,18 @@ export default class CustomerRepository implements CustomerRepositoryInterface {
async findAll (): Promise<Customer[]> {
const customerModels = await CustomerModel.findAll()

const customers = customerModels.map((customerModels) => {
const customer = new Customer(customerModels.id, customerModels.name)
customer.addRewardPoints(customerModels.reward_points)
const customers = customerModels.map((customerModel) => {
const customer = new Customer(customerModel.id, customerModel.name)
customer.addRewardPoints(customerModel.reward_points)
const address = new Address(
customerModels.street,
customerModels.number,
customerModels.zip,
customerModels.city,
customerModels.state
customerModel.street,
customerModel.number,
customerModel.zip,
customerModel.city,
customerModel.state
)
customer.changeAddress(address)
if (customerModels.active) {
if (customerModel.active) {
customer.activate()
}
return customer
Expand Down
90 changes: 84 additions & 6 deletions src/infrastructure/repository/order-repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ import { OrderItem } from '../../domain/entity/order-item'
import { Order } from '../../domain/entity/order'
import OrderRepository from './order-repository'

const makeCustomer = (): Customer => {
const customer = new Customer('c1', 'customer 1')
const address = new Address('Street 1', 123, '12345-678', 'São Paulo', 'SP')
customer.changeAddress(address)
return customer
}

const makeProduct = (): Product => {
return new Product('p1', 'product 1', 100)
}

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

Expand All @@ -33,17 +44,15 @@ describe('Order repository test', () => {

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)
const customer = makeCustomer()
await customerRepository.create(customer)

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

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

Expand All @@ -66,4 +75,73 @@ describe('Order repository test', () => {
}]
})
})

it('should throw an error when calling the update method', async () => {
const customerRepository = new CustomerRepository()
const customer = makeCustomer()
await customerRepository.create(customer)

const productRepository = new ProductRepository()
const product = makeProduct()
await productRepository.create(product)

const item = new OrderItem('i1', product.name, product.price, product.id, 2)
const order = new Order('o1', customer.id, [item])
const orderRepository = new OrderRepository()

void expect(async () => {
await orderRepository.update(order)
}).rejects.toThrow('method not implemented')
})

it('should find a order', async () => {
const customerRepository = new CustomerRepository()
const customer = makeCustomer()
await customerRepository.create(customer)

const productRepository = new ProductRepository()
const product = makeProduct()
await productRepository.create(product)

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

const orderResult = await orderRepository.find(order.id)
expect(order).toStrictEqual(orderResult)
})

it('should throw an error when order is not found', async () => {
const orderRepository = new OrderRepository()

void expect(async () => {
await orderRepository.find('456ABC')
}).rejects.toThrow('order not found')
})

it('should find all orders', async () => {
const customerRepository = new CustomerRepository()
const customer = makeCustomer()
await customerRepository.create(customer)

const productRepository = new ProductRepository()
const product = makeProduct()
await productRepository.create(product)

const item1 = new OrderItem('i1', product.name, product.price, product.id, 2)
const item2 = new OrderItem('i2', product.name, product.price, product.id, 2)

const order1 = new Order('o1', customer.id, [item1])
const order2 = new Order('o2', customer.id, [item2])

const orderRepository = new OrderRepository()
await orderRepository.create(order1)
await orderRepository.create(order2)

const orders = await orderRepository.findAll()
expect(orders).toHaveLength(2)
expect(orders).toContainEqual(order1)
expect(orders).toContainEqual(order2)
})
})
51 changes: 50 additions & 1 deletion src/infrastructure/repository/order-repository.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Order } from '../../domain/entity/order'
import { OrderItem } from '../../domain/entity/order-item'
import { OrderRepositoryInterface } from '../../domain/repository/order-repository.interface'
import { OrderItemModel } from '../database/sequelize/model/order-item-model'
import { OrderModel } from '../database/sequelize/model/order-model'

export default class OrderRepository {
export default class OrderRepository implements OrderRepositoryInterface {
async create (entity: Order): Promise<void> {
await OrderModel.create({
id: entity.id,
Expand All @@ -19,4 +21,51 @@ export default class OrderRepository {
include: [{ model: OrderItemModel }]
})
}

async update (entity: Order): Promise<void> {
throw new Error('method not implemented')
}

async find (id: string): Promise<Order> {
try {
const orderModel = await OrderModel.findOne({
where: { id },
include: ['items'],
rejectOnEmpty: true
})

const items = orderModel.items.map(item => {
return new OrderItem(
item.id,
item.name,
item.price,
item.product_id,
item.quantity
)
})
return new Order(id, orderModel.customer_id, items)
} catch (error) {
throw new Error('order not found')
}
}

async findAll (): Promise<any> {
const orderModels = await OrderModel.findAll({
include: ['items']
})
const orders = orderModels.map((orderModel) => {
const items = orderModel.items.map(item => {
return new OrderItem(
item.id,
item.name,
item.price,
item.product_id,
item.quantity
)
})
return new Order(orderModel.id, orderModel.customer_id, items)
})

return orders
}
}

0 comments on commit 609b6e9

Please sign in to comment.