Skip to content

Commit

Permalink
Feature/add sequence to order request (#30)
Browse files Browse the repository at this point in the history
* add sequence id to create request order
return saveOrUpdate to CategoryOutputAdapter, ProductOutputAdapter to maintain idempotency
improvements unit tests to 100% coverage pom referenced

* add handler advice pre condition failed for DataIntegrityViolationException

* remove active attr from category

* remove active attr from product

* add order dtos to response

* add order dtos to response

* move package mappers to infrastructure

* add payment link rule to core/output adapter

* review swagger properties

---------

Co-authored-by: Myller Sakaguchi Lobo <myller.lobo@omni.com.br>
  • Loading branch information
msakaguchi and Myller Sakaguchi Lobo authored Aug 10, 2024
1 parent 27d865a commit cce7fa6
Show file tree
Hide file tree
Showing 80 changed files with 586 additions and 309 deletions.
7 changes: 3 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,11 @@
<version>${jacoco.version}</version>
<configuration>
<includes>
<include>com/fiap/tc/core/usecase/**/*</include>
<include>com/fiap/tc/adapter/repository/output/**</include>
<include>com/fiap/tc/core/application/usecase/**/*</include>
<include>com/fiap/tc/adapters/driven/infrastructure/outputs/**</include>
</includes>
<excludes>
<exclude>com/fiap/tc/adapter/repository/output/validator/upload/ProductImageValidatorWrapper*
</exclude>
<exclude>com/fiap/tc/adapters/driven/infrastructure/outputs/validators/upload/ProductImageValidatorWrapper*</exclude>
</excludes>
</configuration>
<executions>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.fiap.tc.adapters.driven.infrastructure.persistence.mappers;
package com.fiap.tc.adapters.driven.infrastructure.mappers;

import com.fiap.tc.adapters.driven.infrastructure.persistence.entities.CategoryEntity;
import com.fiap.tc.adapters.driven.infrastructure.persistence.mappers.base.MapperEntity;
import com.fiap.tc.adapters.driven.infrastructure.mappers.base.MapperEntity;
import com.fiap.tc.core.domain.entities.Category;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
Expand All @@ -10,12 +10,10 @@
public interface CategoryMapper extends MapperEntity<CategoryEntity, Category> {

@Override
@Mapping(target = "active", source = "audit.active")
@Mapping(target = "id", source = "uuid")
Category fromEntity(CategoryEntity categoryEntity);

@Override
@Mapping(source = "active", target = "audit.active")
@Mapping(target = "id", ignore = true)
CategoryEntity toEntity(Category category);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.fiap.tc.adapters.driven.infrastructure.persistence.mappers;
package com.fiap.tc.adapters.driven.infrastructure.mappers;

import com.fiap.tc.adapters.driven.infrastructure.persistence.entities.CustomerEntity;
import com.fiap.tc.adapters.driven.infrastructure.persistence.mappers.base.MapperEntity;
import com.fiap.tc.adapters.driven.infrastructure.mappers.base.MapperEntity;
import com.fiap.tc.core.domain.entities.Customer;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.fiap.tc.adapters.driven.infrastructure.persistence.mappers;
package com.fiap.tc.adapters.driven.infrastructure.mappers;

import com.fiap.tc.adapters.driven.infrastructure.persistence.entities.OrderHistoricEntity;
import com.fiap.tc.adapters.driven.infrastructure.persistence.mappers.base.MapperEntity;
import com.fiap.tc.adapters.driven.infrastructure.mappers.base.MapperEntity;
import com.fiap.tc.core.domain.entities.OrderHistoric;
import org.mapstruct.Mapper;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.fiap.tc.adapters.driven.infrastructure.persistence.mappers;
package com.fiap.tc.adapters.driven.infrastructure.mappers;

import com.fiap.tc.adapters.driven.infrastructure.persistence.entities.OrderItemEntity;
import com.fiap.tc.adapters.driven.infrastructure.persistence.mappers.base.MapperEntity;
import com.fiap.tc.adapters.driven.infrastructure.mappers.base.MapperEntity;
import com.fiap.tc.core.domain.entities.OrderItem;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.fiap.tc.adapters.driven.infrastructure.persistence.mappers;
package com.fiap.tc.adapters.driven.infrastructure.mappers;

import com.fiap.tc.adapters.driven.infrastructure.persistence.entities.OrderEntity;
import com.fiap.tc.adapters.driven.infrastructure.persistence.entities.OrderHistoricEntity;
import com.fiap.tc.adapters.driven.infrastructure.persistence.mappers.base.MapperEntity;
import com.fiap.tc.adapters.driven.infrastructure.mappers.base.MapperEntity;
import com.fiap.tc.core.domain.entities.OrderList;
import com.fiap.tc.core.domain.enums.OrderStatus;
import org.mapstruct.Mapper;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.fiap.tc.adapters.driven.infrastructure.persistence.mappers;
package com.fiap.tc.adapters.driven.infrastructure.mappers;

import com.fiap.tc.adapters.driven.infrastructure.persistence.entities.OrderEntity;
import com.fiap.tc.adapters.driven.infrastructure.persistence.mappers.base.MapperConstants;
import com.fiap.tc.adapters.driven.infrastructure.persistence.mappers.base.MapperEntity;
import com.fiap.tc.adapters.driven.infrastructure.mappers.base.MapperConstants;
import com.fiap.tc.adapters.driven.infrastructure.mappers.base.MapperEntity;
import com.fiap.tc.core.domain.entities.Order;
import com.fiap.tc.core.domain.entities.OrderHistoric;
import com.fiap.tc.core.domain.entities.OrderItem;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.fiap.tc.adapters.driven.infrastructure.persistence.mappers;
package com.fiap.tc.adapters.driven.infrastructure.mappers;

import com.fiap.tc.adapters.driven.infrastructure.mappers.base.MapperEntity;
import com.fiap.tc.adapters.driven.infrastructure.persistence.entities.ProductImageEntity;
import com.fiap.tc.adapters.driven.infrastructure.persistence.mappers.base.MapperEntity;
import com.fiap.tc.core.domain.entities.ProductImage;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.fiap.tc.adapters.driven.infrastructure.persistence.mappers;
package com.fiap.tc.adapters.driven.infrastructure.mappers;

import com.fiap.tc.adapters.driven.infrastructure.persistence.entities.ProductEntity;
import com.fiap.tc.adapters.driven.infrastructure.persistence.mappers.base.MapperEntity;
import com.fiap.tc.adapters.driven.infrastructure.mappers.base.MapperEntity;
import com.fiap.tc.core.domain.entities.Product;
import com.fiap.tc.core.domain.entities.ProductImage;
import org.mapstruct.Mapper;
Expand All @@ -11,7 +11,7 @@
import java.util.Collections;
import java.util.List;

import static com.fiap.tc.adapters.driven.infrastructure.persistence.mappers.base.MapperConstants.PRODUCT_IMAGE_MAPPER;
import static com.fiap.tc.adapters.driven.infrastructure.mappers.base.MapperConstants.PRODUCT_IMAGE_MAPPER;
import static org.springframework.util.CollectionUtils.isEmpty;

@Mapper
Expand All @@ -21,15 +21,12 @@ public interface ProductMapper extends MapperEntity<ProductEntity, Product> {
@Mapping(target = "id", source = "uuid")
@Mapping(target = "images", source = "productEntity", qualifiedByName = "buildImages")
@Mapping(target = "idCategory", source = "category.uuid")
@Mapping(target = "active", source = "audit.active")
@Mapping(target = "categoryName", source = "category.name")
Product fromEntity(ProductEntity productEntity);

@Override
@Mapping(target = "id", ignore = true)
@Mapping(target = "uuid", source = "id")
@Mapping(target = "images", ignore = true)
@Mapping(target = "audit.active", source = "active")
ProductEntity toEntity(Product product);


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.fiap.tc.adapters.driven.infrastructure.persistence.mappers;
package com.fiap.tc.adapters.driven.infrastructure.mappers;

import com.fiap.tc.adapters.driven.infrastructure.persistence.entities.security.UserEntity;
import com.fiap.tc.adapters.driven.infrastructure.persistence.mappers.base.MapperEntity;
import com.fiap.tc.adapters.driven.infrastructure.mappers.base.MapperEntity;
import com.fiap.tc.core.domain.entities.UserSystem;
import org.mapstruct.Mapper;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.fiap.tc.adapters.driven.infrastructure.persistence.mappers.base;
package com.fiap.tc.adapters.driven.infrastructure.mappers.base;

import com.fiap.tc.adapters.driven.infrastructure.persistence.mappers.*;
import com.fiap.tc.adapters.driven.infrastructure.mappers.*;
import org.mapstruct.factory.Mappers;

public class MapperConstants {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fiap.tc.adapters.driven.infrastructure.persistence.mappers.base;
package com.fiap.tc.adapters.driven.infrastructure.mappers.base;

import org.mapstruct.InheritInverseConfiguration;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package com.fiap.tc.adapters.driven.infrastructure.outputs;

import com.fiap.tc.adapters.driven.infrastructure.persistence.repositories.CategoryRepository;
import com.fiap.tc.adapters.driven.infrastructure.persistence.entities.CategoryEntity;
import com.fiap.tc.adapters.driven.infrastructure.persistence.entities.embeddable.Audit;
import com.fiap.tc.adapters.driven.infrastructure.persistence.repositories.CategoryRepository;
import com.fiap.tc.core.application.ports.out.category.*;
import com.fiap.tc.core.domain.entities.Category;
import com.fiap.tc.core.domain.exceptions.BadRequestException;
import com.fiap.tc.core.domain.exceptions.NotFoundException;
import com.fiap.tc.core.domain.entities.Category;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.UUID;

import static com.fiap.tc.adapters.driven.infrastructure.persistence.mappers.base.MapperConstants.CATEGORY_MAPPER;
import static com.fiap.tc.adapters.driven.infrastructure.mappers.base.MapperConstants.CATEGORY_MAPPER;
import static java.lang.String.format;

@Service
public class CategoryOutputAdapter implements SaveCategoryOutputPort, LoadCategoryOutputPort,
public class CategoryOutputAdapter implements RegisterCategoryOutputPort, LoadCategoryOutputPort,
ListCategoriesOutputPort, DeleteCategoryOutputPort, UpdateCategoryOutputPort {
private final CategoryRepository repository;

Expand Down Expand Up @@ -48,35 +48,33 @@ public Category load(UUID uuid) {
}

@Override
public Category saveOrUpdate(String name, String description, boolean active) {
public Category saveOrUpdate(String name, String description) {
var categoryEntityOptional = repository.findByName(name);

if (categoryEntityOptional.isPresent()) {
var categoryEntity = categoryEntityOptional.get();
categoryEntity.setName(name);
categoryEntity.setDescription(description);
categoryEntity.getAudit().setActive(active);
categoryEntity.getAudit().setUpdatedDate(LocalDateTime.now());
return CATEGORY_MAPPER.fromEntity(repository.save(categoryEntity));

}
return CATEGORY_MAPPER.fromEntity(repository.save(buildCategoryEntity(name, description, active)));
return CATEGORY_MAPPER.fromEntity(repository.save(buildCategoryEntity(name, description)));

}

private CategoryEntity buildCategoryEntity(String name, String description, boolean active) {
private CategoryEntity buildCategoryEntity(String name, String description) {
var newCategory = new CategoryEntity();
newCategory.setName(name);
newCategory.setDescription(description);
newCategory.setUuid(UUID.randomUUID());
newCategory.setAudit(Audit.builder().active(active).registerDate(LocalDateTime.now()).build());
newCategory.setAudit(Audit.builder().active(true).registerDate(LocalDateTime.now()).build());
return newCategory;
}

@Override
public Category update(UUID idCategory, String name, String description, boolean active) {
public Category update(UUID idCategory, String name, String description) {
var categoryEntity = validate(idCategory, name);
return updateCategory(name, description, active, categoryEntity);
return updateCategory(name, description, categoryEntity);
}

private CategoryEntity validate(UUID idCategory, String name) {
Expand All @@ -87,21 +85,23 @@ private CategoryEntity validate(UUID idCategory, String name) {
}

var categoryExpectedEntityOptional = repository.findByName(name);

if(categoryExpectedEntityOptional.isEmpty()) return categoryEntityOptional.get();

if (idCategory != categoryExpectedEntityOptional.get().getUuid()) {
throw new BadRequestException(format("Category with expected name %s already exists!", name));

var categoryEntity = categoryEntityOptional.get();
var notSameCategoryName = !(categoryEntity.getName().equals(name));

var hasInvalidCategoryName = notSameCategoryName && categoryExpectedEntityOptional.isPresent();

if (hasInvalidCategoryName) {
throw new BadRequestException(format("Category with expected name '%s' already exists!", name));
}

return categoryEntityOptional.get();
}

private Category updateCategory(String name, String description, boolean active, CategoryEntity categoryEntity) {
private Category updateCategory(String name, String description, CategoryEntity categoryEntity) {
categoryEntity.setName(name);
categoryEntity.setDescription(description);
categoryEntity.getAudit().setUpdatedDate(LocalDateTime.now());
categoryEntity.getAudit().setActive(active);

return CATEGORY_MAPPER.fromEntity(repository.save(categoryEntity));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import java.time.LocalDateTime;
import java.util.UUID;

import static com.fiap.tc.adapters.driven.infrastructure.persistence.mappers.base.MapperConstants.CUSTOMER_MAPPER;
import static com.fiap.tc.adapters.driven.infrastructure.mappers.base.MapperConstants.CUSTOMER_MAPPER;
import static java.lang.String.format;

@Service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.fiap.tc.adapters.driven.infrastructure.outputs;

import com.fiap.tc.adapters.driven.infrastructure.persistence.builders.OrderHistoricBuilder;
import com.fiap.tc.adapters.driven.infrastructure.persistence.mappers.base.MapperConstants;
import com.fiap.tc.adapters.driven.infrastructure.mappers.base.MapperConstants;
import com.fiap.tc.adapters.driven.infrastructure.persistence.repositories.OrderRepository;
import com.fiap.tc.core.application.ports.out.order.ListOrdersReadyPreparingOutputPort;
import com.fiap.tc.core.application.ports.out.order.LoadOrderOutputPort;
Expand All @@ -21,7 +21,8 @@
import static java.lang.String.format;

@Service
public class OrderOutputAdapter implements UpdateStatusOrderOutputPort, LoadOrderOutputPort, ListOrdersReadyPreparingOutputPort {
public class OrderOutputAdapter implements UpdateStatusOrderOutputPort, LoadOrderOutputPort,
ListOrdersReadyPreparingOutputPort {

private final OrderRepository orderRepository;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.fiap.tc.adapters.driven.infrastructure.outputs;

import com.fiap.tc.adapters.driven.infrastructure.utils.QRCodeGenerator;
import com.fiap.tc.core.application.ports.out.payment.PaymentLinkOutputPort;
import com.fiap.tc.core.domain.entities.Order;
import org.springframework.stereotype.Service;

import java.util.Optional;

import static com.fiap.tc.core.domain.constants.OrderConstants.PAYMENT_LINK_STATUS;

@Service
public class PaymentLinkOutputAdapter implements PaymentLinkOutputPort {

private final QRCodeGenerator qrCodeGenerator;

public PaymentLinkOutputAdapter(QRCodeGenerator qrCodeGenerator) {
this.qrCodeGenerator = qrCodeGenerator;
}

@Override
public Optional<String> generate(Order order) {
if (PAYMENT_LINK_STATUS.contains(order.getStatus())) {
return Optional.ofNullable(qrCodeGenerator.generate(order.orderWithTotalAsText()));
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.fiap.tc.adapters.driven.infrastructure.persistence.entities.ProductEntity;
import com.fiap.tc.adapters.driven.infrastructure.persistence.entities.ProductImageEntity;
import com.fiap.tc.adapters.driven.infrastructure.persistence.entities.embeddable.Audit;
import com.fiap.tc.adapters.driven.infrastructure.persistence.mappers.base.MapperConstants;
import com.fiap.tc.adapters.driven.infrastructure.mappers.base.MapperConstants;
import com.fiap.tc.core.domain.exceptions.NotFoundException;
import com.fiap.tc.core.domain.entities.Product;
import com.fiap.tc.core.domain.entities.ProductImage;
Expand Down
Loading

0 comments on commit cce7fa6

Please sign in to comment.