Skip to content

Commit

Permalink
Fix: MercadoPagoConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavofg1pontes committed Oct 7, 2024
1 parent 218bff4 commit 109cda3
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ public interface ITicketSaleGateway {

Pagination<TicketSale> findAllByEventID(EventID id, SearchQuery sq);

Pagination<TicketSale> findAll(AdvancedSearchQuery sq);

TicketSale update(TicketSale event);

void delete(TicketSale ticketSale);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package br.com.ifsp.tickets.infra.config.app;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
public class MercadoPagoConfig {

@Value("${mercadopago.access.token}")
private String mercadoPagoAccessToken;

@PostConstruct
public void initialize() {
com.mercadopago.MercadoPagoConfig.setAccessToken(mercadoPagoAccessToken);
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,26 @@
package br.com.ifsp.tickets.infra.config.app;

import br.com.ifsp.tickets.app.event.EventService;
import br.com.ifsp.tickets.app.event.EventServiceFactory;
import br.com.ifsp.tickets.app.payment.PaymentService;
import br.com.ifsp.tickets.app.payment.PaymentServiceFactory;
import br.com.ifsp.tickets.domain.event.sale.ITicketSaleGateway;
import br.com.ifsp.tickets.domain.ticket.ITicketGateway;
import br.com.ifsp.tickets.domain.ticket.payment.IPaymentGateway;
import br.com.ifsp.tickets.infra.contexts.event.sale.payment.PaymentGateway;
import com.mercadopago.MercadoPagoConfig;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
@RequiredArgsConstructor(onConstructor_ = @__(@Autowired))
public class MercadoPagoConfiguration {
private final ITicketSaleGateway ticketSaleGateway;
public class PaymentConfig {
private final ITicketGateway ticketGateway;
private final ITicketSaleGateway ticketSaleGateway;
private final IPaymentGateway paymentGateway;

@Value("${mercadopago.access.token}")
private String mercadoPagoAccessToken;

@PostConstruct
public void initialize() {
MercadoPagoConfig.setAccessToken(mercadoPagoAccessToken);
}

@Bean
public PaymentService preferenceService() {
public PaymentService paymentService() {
return PaymentServiceFactory.create(ticketGateway, ticketSaleGateway, paymentGateway);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.time.LocalDateTime;

@Entity
@Table(name = "payments")
@NoArgsConstructor
@Getter
public class PaymentJpaEntity {
public class PaymentJpaEntity implements Serializable {
@Id
@Column(name = "id", nullable = false, unique = true, updatable = false)
private Long id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.UUID;

public interface PaymentRepository extends JpaRepository<PaymentJpaEntity, UUID> {
public interface PaymentRepository extends JpaRepository<PaymentJpaEntity, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@
import br.com.ifsp.tickets.domain.event.sale.ITicketSaleGateway;
import br.com.ifsp.tickets.domain.event.sale.TicketSale;
import br.com.ifsp.tickets.domain.event.sale.TicketSaleID;
import br.com.ifsp.tickets.domain.shared.search.AdvancedSearchQuery;
import br.com.ifsp.tickets.domain.shared.search.Pagination;
import br.com.ifsp.tickets.domain.shared.search.SearchQuery;
import br.com.ifsp.tickets.infra.contexts.event.sale.ticket.persistence.TicketSaleJpaEntity;
import br.com.ifsp.tickets.infra.contexts.event.sale.ticket.persistence.TicketSaleRepository;
import br.com.ifsp.tickets.infra.contexts.event.sale.ticket.persistence.spec.TicketSaleSpecificationBuilder;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import java.util.Optional;
Expand All @@ -24,7 +21,6 @@
@RequiredArgsConstructor(onConstructor_ = @__(@Autowired))
public class TicketSaleGateway implements ITicketSaleGateway {
private final TicketSaleRepository repository;
private final TicketSaleRepository ticketSaleRepository;

@Override
public TicketSale create(TicketSale ticketSale) {
Expand All @@ -44,29 +40,7 @@ public Pagination<TicketSale> findAllByEventID(EventID id, SearchQuery sq) {
Sort.by(Sort.Direction.fromString(sq.direction()), sq.sort())
);

final Page<TicketSale> page = this.repository.findAllByEventID(id.getValue(), request).map(ticketSaleJpaEntity -> ticketSaleJpaEntity.toAggregate());

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

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

final Page<TicketSale> page = this.repository.findAll(specification, request).map(ticketSaleJpaEntity -> ticketSaleJpaEntity.toAggregate());
final Page<TicketSale> page = this.repository.findAllByEventId(id.getValue(), request).map(TicketSaleJpaEntity::toAggregate);

return Pagination.of(
page.getNumber(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import com.fasterxml.jackson.annotation.JsonProperty;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.HashMap;

public record CreateTicketSaleRequest(
@JsonProperty("name") String name,
@JsonProperty("description") String description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class TicketSaleJpaEntity implements Serializable {
@Column(name = "id", nullable = false, unique = true, updatable = false)
private UUID id;
@Column(name = "event_id", nullable = false)
private UUID eventID;
private UUID eventId;
@Column(name = "name", nullable = false)
private String name; // example: Ingresso Unitário + Meia entrada
@Column(name = "description", nullable = false)
Expand All @@ -37,7 +37,7 @@ public class TicketSaleJpaEntity implements Serializable {

public TicketSaleJpaEntity(UUID id, UUID eventID, String name, String description, BigDecimal price, int entries, boolean active) {
this.id = id;
this.eventID = eventID;
this.eventId = eventID;
this.name = name;
this.description = description;
this.price = price;
Expand All @@ -60,7 +60,7 @@ public static TicketSaleJpaEntity from(TicketSale ticketSale) {
public TicketSale toAggregate() {
return new TicketSale(
TicketSaleID.with(this.getId()),
EventID.with(this.getEventID()),
EventID.with(this.getEventId()),
this.getName(),
this.getDescription(),
this.getPrice(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package br.com.ifsp.tickets.infra.contexts.event.sale.ticket.persistence;

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

import java.util.UUID;

public interface TicketSaleRepository extends JpaRepository<TicketSaleJpaEntity, UUID> {
Page<TicketSaleJpaEntity> findAllByEventID(UUID eventID, PageRequest request);
Page<TicketSaleJpaEntity> findAll(Specification<TicketSaleJpaEntity> specification, PageRequest request);
Page<TicketSaleJpaEntity> findAllByEventId(UUID eventID, Pageable pageable);
}
3 changes: 2 additions & 1 deletion infrastructure/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
Application: TicketsIFSP-API
Version: v1.0.0
Authors:
- Leonardo
- Leonardo
- Gustavo

0 comments on commit 109cda3

Please sign in to comment.