-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e69dda
commit ddef521
Showing
13 changed files
with
199 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/infrastructure/database/sequelize/model/order-item-model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
src/infrastructure/database/sequelize/model/order-model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}] | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }] | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters