Skip to content

Commit

Permalink
Merge pull request #27 from vanderleik/PPI-27-Adicionar-QueryDSL
Browse files Browse the repository at this point in the history
feat: PPI-27 - adicionando QueryDsl ao projeto
  • Loading branch information
vanderleik authored Jul 12, 2024
2 parents c29a015 + 25ee37f commit 05f9dcc
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 1 deletion.
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>${querydsl.version}</version>
<classifier>jakarta</classifier>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
<classifier>jakarta</classifier>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.produtopedidoitens.api.adapters.config;

import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QueryDslConfiguration {

@PersistenceContext
private EntityManager em;

@Bean
public JPAQueryFactory jpaQueryFactory() {
return new JPAQueryFactory(em);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import com.produtopedidoitens.api.application.domain.entities.OrderItemEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.UUID;

public interface OrderItemRepository extends JpaRepository<OrderItemEntity, UUID> {
@Repository
public interface OrderItemRepository extends JpaRepository<OrderItemEntity, UUID>, OrderItemRepositoryCustom {

@Query("SELECT CASE WHEN COUNT(o) > 0 THEN TRUE ELSE FALSE END FROM OrderItemEntity o WHERE o.product.id = :productId")
Boolean existsByProductId(UUID productId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.produtopedidoitens.api.adapters.persistence.repositories;

import com.produtopedidoitens.api.adapters.web.projections.OrderByOrderNumber;

import java.util.List;

public interface OrderItemRepositoryCustom {

List<OrderByOrderNumber> findOrdersByOrderNumber(String orderNumber);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.produtopedidoitens.api.adapters.persistence.repositories;

import com.produtopedidoitens.api.adapters.web.projections.OrderByOrderNumber;
import com.produtopedidoitens.api.application.domain.entities.QOrderEntity;
import com.produtopedidoitens.api.application.domain.entities.QOrderItemEntity;
import com.produtopedidoitens.api.application.domain.entities.QProductEntity;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class OrderItemRepositoryCustomImpl implements OrderItemRepositoryCustom {

@PersistenceContext
private EntityManager entityManager;

@Override
public List<OrderByOrderNumber> findOrdersByOrderNumber(String orderNumber) {
JPAQueryFactory queryFactory = new JPAQueryFactory(entityManager);
QOrderEntity order = QOrderEntity.orderEntity;
QOrderItemEntity orderItem = QOrderItemEntity.orderItemEntity;
QProductEntity product = QProductEntity.productEntity;

return queryFactory
.select(Projections.constructor(OrderByOrderNumber.class,
product.productName,
order.orderNumber,
order.status.stringValue(),
orderItem.quantity,
product.price))
.from(orderItem)
.innerJoin(orderItem.product, product)
.innerJoin(orderItem.order, order)
.where(order.orderNumber.eq(orderNumber))
.fetch();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.produtopedidoitens.api.adapters.web.controllers;

import com.produtopedidoitens.api.adapters.web.projections.OrderByOrderNumber;
import com.produtopedidoitens.api.adapters.web.projections.OrderItemProjection;
import com.produtopedidoitens.api.adapters.web.requests.OrderItemRequest;
import com.produtopedidoitens.api.adapters.web.responses.OrderItemResponse;
Expand Down Expand Up @@ -39,6 +40,12 @@ public ResponseEntity<OrderItemProjection> read(@PathVariable String id) {
return ResponseEntity.ok(orderItemInputPort.read(UUID.fromString(id)));
}

@GetMapping("/order/{orderNumber}")
public ResponseEntity<List<OrderByOrderNumber>> getOrdersByOrderNumber(@PathVariable String orderNumber) {
log.info("getOrdersByOrderNumber:: Recebendo requisição para buscar itens de pedido pelo número do pedido: {}", orderNumber);
return ResponseEntity.ok(orderItemInputPort.getOrdersByOrderNumber(orderNumber));
}

@PutMapping("/{id}")
public ResponseEntity<OrderItemResponse> update(@PathVariable String id, @Valid @RequestBody OrderItemRequest orderItemRequest) {
log.info("update:: Recebendo requisição para atualizar um item de pedido com os dados: {}", orderItemRequest);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.produtopedidoitens.api.adapters.web.projections;

import lombok.Builder;

import java.math.BigDecimal;

@Builder
public record OrderByOrderNumber (

String productName,
String orderNumber,
String status,
Integer quantity,
BigDecimal price
){
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.produtopedidoitens.api.application.port;

import com.produtopedidoitens.api.adapters.web.projections.OrderByOrderNumber;
import com.produtopedidoitens.api.adapters.web.projections.OrderItemProjection;
import com.produtopedidoitens.api.adapters.web.requests.OrderItemRequest;
import com.produtopedidoitens.api.adapters.web.responses.OrderItemResponse;
Expand All @@ -12,6 +13,7 @@ public interface OrderItemInputPort {
OrderItemResponse create(OrderItemRequest orderItemRequest);
List<OrderItemProjection> list();
OrderItemProjection read(UUID id);
List<OrderByOrderNumber> getOrdersByOrderNumber(String orderNumber);
OrderItemResponse update(UUID id, OrderItemRequest orderItemRequest);
void delete(UUID id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.produtopedidoitens.api.adapters.persistence.repositories.OrderItemRepository;
import com.produtopedidoitens.api.adapters.persistence.repositories.OrderRepository;
import com.produtopedidoitens.api.adapters.persistence.repositories.ProductRepository;
import com.produtopedidoitens.api.adapters.web.projections.OrderByOrderNumber;
import com.produtopedidoitens.api.adapters.web.projections.OrderItemProjection;
import com.produtopedidoitens.api.adapters.web.requests.OrderItemRequest;
import com.produtopedidoitens.api.adapters.web.responses.OrderItemResponse;
Expand Down Expand Up @@ -74,6 +75,18 @@ public OrderItemProjection read(UUID id) {
return orderItemConverter.toProjection(entity);
}

@Override
public List<OrderByOrderNumber> getOrdersByOrderNumber(String orderNumber) {
log.info("getOrdersByOrderNumber:: Buscando itens do pedido pelo número do pedido: {}", orderNumber);
List<OrderByOrderNumber> ordersByOrderNumber = orderItemRepository.findOrdersByOrderNumber(orderNumber);
if (ordersByOrderNumber.isEmpty()) {
log.error("getOrdersByOrderNumber:: Nenhum item do pedido encontrado pelo número do pedido: {}", orderNumber);
throw new OrderItemNotFoundException(MessagesConstants.ERROR_ORDER_ITEM_NOT_FOUND_BY_ORDER_NUMBER);
}
log.info("getOrdersByOrderNumber:: Itens do pedido encontrados pelo número do pedido: {}", ordersByOrderNumber);
return ordersByOrderNumber;
}

@Transactional(rollbackFor = Exception.class)
@Override
public OrderItemResponse update(UUID id, OrderItemRequest orderItemRequest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class MessagesConstants {
public static final String ERROR_DELETE_ORDER_ITEM = "Erro ao deletar item do pedido";

public static final String ERROR_PRODUCT_ASSOCIATED_ORDER_ITEM = "O produto/serviço não pode ser deletado pois está associado a um item de pedido";
public static final String ERROR_ORDER_ITEM_NOT_FOUND_BY_ORDER_NUMBER = "Nenhum item de pedido encontrado para o número do pedido informado";

private MessagesConstants() {
}
Expand Down

0 comments on commit 05f9dcc

Please sign in to comment.