Skip to content

Commit

Permalink
feat: order infra
Browse files Browse the repository at this point in the history
  • Loading branch information
oproprioleonardo committed Dec 14, 2024
1 parent 0937cd2 commit d7daa6a
Show file tree
Hide file tree
Showing 5 changed files with 262 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package br.com.ifsp.tickets.infra.contexts.financial.order;

import br.com.ifsp.tickets.domain.administrative.user.vo.Document;
import br.com.ifsp.tickets.domain.financial.order.IOrderGateway;
import br.com.ifsp.tickets.domain.financial.order.Order;
import br.com.ifsp.tickets.domain.financial.order.OrderID;
import br.com.ifsp.tickets.domain.shared.search.AdvancedSearchQuery;
import br.com.ifsp.tickets.domain.shared.search.Pagination;
import br.com.ifsp.tickets.infra.contexts.financial.order.persistence.OrderJpaEntity;
import br.com.ifsp.tickets.infra.contexts.financial.order.persistence.OrderRepository;
import br.com.ifsp.tickets.infra.contexts.financial.order.persistence.spec.OrderSpecificationBuilder;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
@RequiredArgsConstructor(onConstructor_ = @__(@Autowired))
public class OrderGateway implements IOrderGateway {

private final OrderRepository orderRepository;

@Override
public Order create(Order order) {
return this.orderRepository.save(OrderJpaEntity.from(order)).toAggregate();
}

@Override
public Optional<Order> findById(OrderID id) {
return this.orderRepository.findById(id.getValue()).map(OrderJpaEntity::toAggregate);
}

@Override
public Optional<Order> findByIdAndDocument(OrderID id, Document document) {
return this.orderRepository.findByIdAndDocument(id.getValue(), document.toString()).map(OrderJpaEntity::toAggregate);
}

@Override
public Pagination<Order> findAll(AdvancedSearchQuery advancedSearchQuery) {
final OrderSpecificationBuilder specificationBuilder = new OrderSpecificationBuilder();
advancedSearchQuery.filters().forEach(specificationBuilder::with);
final var specification = specificationBuilder.build();
final var orders = advancedSearchQuery.sorts().stream()
.map(sort -> Sort.by(Sort.Direction.fromString(sort.direction()), sort.sort()))
.reduce(Sort::and).orElse(Sort.by(Sort.Order.asc("id")));
final var request = PageRequest.of(
advancedSearchQuery.page(),
advancedSearchQuery.perPage(),
orders
);

final var page = this.orderRepository.findAll(specification, request).map(OrderJpaEntity::toAggregate);

return Pagination.of(
page.getNumber(),
page.getSize(),
page.getTotalElements(),
page.getContent()
);
}

@Override
public Order update(Order order) {
return this.orderRepository.save(OrderJpaEntity.from(order)).toAggregate();
}

@Override
public void delete(Order order) {
this.orderRepository.delete(OrderJpaEntity.from(order));
}

@Override
public boolean exists(OrderID id) {
return this.orderRepository.existsById(id.getValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package br.com.ifsp.tickets.infra.contexts.financial.order.persistence;

import br.com.ifsp.tickets.domain.administrative.user.vo.EmailAddress;
import br.com.ifsp.tickets.domain.administrative.user.vo.PhoneNumber;
import br.com.ifsp.tickets.domain.administrative.user.vo.RG;
import br.com.ifsp.tickets.domain.financial.order.Order;
import br.com.ifsp.tickets.domain.financial.order.OrderID;
import br.com.ifsp.tickets.domain.financial.order.OrderStatus;
import br.com.ifsp.tickets.infra.contexts.administrative.user.persistence.UserJpaEntity;
import br.com.ifsp.tickets.infra.contexts.financial.order.persistence.item.OrderItemJpaEntity;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Entity
@Table(name = "orders")
@Getter
@NoArgsConstructor
public class OrderJpaEntity implements Serializable {

@Id
@Column(name = "id", nullable = false, unique = true, updatable = false)
private Long id;
@OneToOne(cascade = {CascadeType.DETACH, CascadeType.REFRESH})
@JoinColumn(name = "customer_id", nullable = false)
private UserJpaEntity customer;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "email", nullable = false)
private String email;
@Column(name = "phone_number", nullable = false)
private String phoneNumber;
@Column(name = "document", nullable = false)
private String document;
@Column(name = "birth_date", nullable = false)
private LocalDate birthDate;
@Column(name = "payment_url", nullable = false)
private String paymentUrl;
@Column(name = "status", nullable = false)
private OrderStatus status;
@Column(name = "created_at", nullable = false)
private LocalDateTime createdAt;
@Column(name = "updated_at", nullable = false)
private LocalDateTime updatedAt;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "order_id")
private List<OrderItemJpaEntity> items;

public OrderJpaEntity(Long id, UserJpaEntity customer, String name, String email, String phoneNumber, String document, LocalDate birthDate, String paymentUrl, OrderStatus status, LocalDateTime createdAt, LocalDateTime updatedAt, List<OrderItemJpaEntity> items) {
this.id = id;
this.customer = customer;
this.name = name;
this.email = email;
this.phoneNumber = phoneNumber;
this.document = document;
this.birthDate = birthDate;
this.paymentUrl = paymentUrl;
this.status = status;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
this.items = items;
}

public static OrderJpaEntity from(Order order) {
return new OrderJpaEntity(
order.getId().getValue(),
order.getCustomer().map(UserJpaEntity::from).orElse(null),
order.getName(),
order.getEmail().getValue(),
order.getPhoneNumber().getValue(),
order.getDocument().getValue(),
order.getBirthDate(),
order.getPaymentUrl(),
order.getStatus(),
order.getCreatedAt(),
order.getUpdatedAt(),
order.getItems().stream().map(OrderItemJpaEntity::from).toList()
);
}

public Order toAggregate() {
return new Order(
OrderID.with(this.getId()),
this.getCustomer().toAggregate(),
this.getName(),
new EmailAddress(this.getEmail()),
new PhoneNumber(this.getPhoneNumber()),
new RG(this.getDocument()),
this.getBirthDate(),
this.getItems().stream().map(OrderItemJpaEntity::toAggregate).toList(),
this.getPaymentUrl(),
this.getStatus(),
this.getCreatedAt(),
this.getUpdatedAt()
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package br.com.ifsp.tickets.infra.contexts.financial.order.persistence;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface OrderRepository extends JpaRepository<OrderJpaEntity, Long> {

Optional<OrderJpaEntity> findByIdAndDocument(Long id, String document);

Page<OrderJpaEntity> findAll(Specification<OrderJpaEntity> spec, Pageable pageable);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package br.com.ifsp.tickets.infra.contexts.financial.order.persistence.item;

import br.com.ifsp.tickets.domain.financial.order.OrderID;
import br.com.ifsp.tickets.domain.financial.order.item.OrderItem;
import br.com.ifsp.tickets.domain.financial.order.item.OrderItemID;
import br.com.ifsp.tickets.infra.contexts.financial.product.persistence.TicketSaleJpaEntity;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Entity
@Table(name = "order_items")
@NoArgsConstructor
@Getter
public class OrderItemJpaEntity implements Serializable {

@Id
@Column(name = "id", nullable = false, unique = true, updatable = false)
private Long id;
@Column(name = "order_id", nullable = false)
private Long orderId;
@OneToOne(cascade = {CascadeType.DETACH, CascadeType.REFRESH})
@JoinColumn(name = "ticket_id", nullable = false)
private TicketSaleJpaEntity ticket;
@Column(name = "quantity", nullable = false)
private int quantity;

public OrderItemJpaEntity(Long id, Long orderId, TicketSaleJpaEntity ticket, int quantity) {
this.id = id;
this.orderId = orderId;
this.ticket = ticket;
this.quantity = quantity;
}

public static OrderItemJpaEntity from(OrderItem orderItem) {
return new OrderItemJpaEntity(
orderItem.getId().getValue(),
orderItem.getOrderID().getValue(),
TicketSaleJpaEntity.from(orderItem.getTicket()),
orderItem.getQuantity()
);
}

public OrderItem toAggregate() {
return new OrderItem(
OrderItemID.with(this.getId()),
OrderID.with(this.getOrderId()),
this.getTicket().toAggregate(),
this.getQuantity()
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package br.com.ifsp.tickets.infra.contexts.financial.order.persistence.spec;

import br.com.ifsp.tickets.infra.contexts.financial.order.persistence.OrderJpaEntity;
import br.com.ifsp.tickets.infra.shared.search.SpecificationBuilder;

public class OrderSpecificationBuilder extends SpecificationBuilder<OrderJpaEntity> {
}

0 comments on commit d7daa6a

Please sign in to comment.