From d0dce3cb76306279272de68edfe771001771c716 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Marulanda=20P?= Date: Sat, 9 Nov 2024 00:57:17 +0100 Subject: [PATCH] =?UTF-8?q?Refactorizado:=20Mapper=20-=20La=20creaci=C3=B3?= =?UTF-8?q?n=20de=20las=20clases=20"response"=20con=20paginaci=C3=B3n=20se?= =?UTF-8?q?=20mueve=20a=20las=20clases=20mapper.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/products/ProductMapper.java | 8 ++++++ .../app/products/ProductService.java | 28 ++----------------- .../app/shoppinglist/ShoppingListMapper.java | 4 +++ .../app/shoppinglist/ShoppingListService.java | 14 +--------- .../app/unitytypes/UnitTypeMapper.java | 4 +++ .../app/unitytypes/UnitTypeService.java | 13 +-------- 6 files changed, 20 insertions(+), 51 deletions(-) diff --git a/src/main/java/dev/nmarulo/depensaapp/app/products/ProductMapper.java b/src/main/java/dev/nmarulo/depensaapp/app/products/ProductMapper.java index 690080f..ba39379 100644 --- a/src/main/java/dev/nmarulo/depensaapp/app/products/ProductMapper.java +++ b/src/main/java/dev/nmarulo/depensaapp/app/products/ProductMapper.java @@ -59,4 +59,12 @@ public static FindAllProductRes.Product toFindAllProductResProduct(final Product return response; } + public static FindAllShoppingListProductRes toFindAllShoppingListProductRes(final Page page) { + return pageTo(page, FindAllShoppingListProductRes::new, ProductMapper::toFindAllShoppingListProductResProduct); + } + + public static FindAllProductRes toFindAllProductRes(final Page page) { + return pageTo(page, FindAllProductRes::new, ProductMapper::toFindAllProductResProduct); + } + } diff --git a/src/main/java/dev/nmarulo/depensaapp/app/products/ProductService.java b/src/main/java/dev/nmarulo/depensaapp/app/products/ProductService.java index 70721f5..08f4869 100644 --- a/src/main/java/dev/nmarulo/depensaapp/app/products/ProductService.java +++ b/src/main/java/dev/nmarulo/depensaapp/app/products/ProductService.java @@ -38,7 +38,6 @@ public FindAllShoppingListProductRes findAllProductsByShoppingListId(Integer sho boolean isExclude, Pageable pageable, User user) { - var response = new FindAllShoppingListProductRes(); Page pageFindAll; if (isExclude) { @@ -48,20 +47,9 @@ public FindAllShoppingListProductRes findAllProductsByShoppingListId(Integer sho pageFindAll = this.productRepository.findAllByIdInShoppingListIdAndUser(shoppingListId, user, pageable); } - var products = pageFindAll.stream() - .map(ProductMapper::toFindAllShoppingListProductResProduct) - .toList(); - - response.setContent(products); - response.setCurrentPage(pageFindAll.getNumber()); - response.setPageSize(pageFindAll.getNumberOfElements()); - response.setTotalPages(pageFindAll.getTotalPages()); - response.setTotal(pageFindAll.getTotalElements()); - - return response; + return ProductMapper.toFindAllShoppingListProductRes(pageFindAll); } - public SaveShoppingListProductRes saveProductInShoppingList(SaveShoppingListProductReq request, User user) { var productOptional = this.productRepository.findById(request.getProductId()); var shoppingListOptional = this.shoppingListRepository.findByIdAndUser(request.getShoppingListId(), user); @@ -94,23 +82,11 @@ public SaveShoppingListProductRes saveProductInShoppingList(SaveShoppingListProd } public FindAllProductRes findAll(final Pageable pageable) { - var response = new FindAllProductRes(); var pageFindAll = this.productRepository.findAll(pageable); - var products = pageFindAll.stream() - .map(ProductMapper::toFindAllProductResProduct) - .toList(); - - response.setContent(products); - response.setCurrentPage(pageFindAll.getNumber()); - response.setPageSize(pageFindAll.getNumberOfElements()); - response.setTotalPages(pageFindAll.getTotalPages()); - response.setTotal(pageFindAll.getTotalElements()); - - return response; + return ProductMapper.toFindAllProductRes(pageFindAll); } - private ProductHasShoppingList getEntity(SaveShoppingListProductReq request, Product product) { var productHasShoppingListPK = new ProductHasShoppingListPK(); var productHasShoppingList = new ProductHasShoppingList(); diff --git a/src/main/java/dev/nmarulo/depensaapp/app/shoppinglist/ShoppingListMapper.java b/src/main/java/dev/nmarulo/depensaapp/app/shoppinglist/ShoppingListMapper.java index b5f0c31..237afbe 100644 --- a/src/main/java/dev/nmarulo/depensaapp/app/shoppinglist/ShoppingListMapper.java +++ b/src/main/java/dev/nmarulo/depensaapp/app/shoppinglist/ShoppingListMapper.java @@ -68,4 +68,8 @@ public static UpdateShoppingListRes toUpdateShoppingListRes(final ShoppingList u return new UpdateShoppingListRes(update.getId()); } + public static FindAllShoppingListRes toFindAllShoppingListRes(final Page page) { + return pageTo(page, FindAllShoppingListRes::new, ShoppingListMapper::toFindAllShoppingListResShoppingList); + } + } diff --git a/src/main/java/dev/nmarulo/depensaapp/app/shoppinglist/ShoppingListService.java b/src/main/java/dev/nmarulo/depensaapp/app/shoppinglist/ShoppingListService.java index 516d838..b42c9bd 100644 --- a/src/main/java/dev/nmarulo/depensaapp/app/shoppinglist/ShoppingListService.java +++ b/src/main/java/dev/nmarulo/depensaapp/app/shoppinglist/ShoppingListService.java @@ -29,20 +29,9 @@ public class ShoppingListService extends BasicServiceImp { private final UserRepository userRepository; public FindAllShoppingListRes findAll(final Pageable pageable, User user) { - var response = new FindAllShoppingListRes(); var pageFindAll = this.shoppingListRepository.findAllByUser(user, pageable); - var shoppingList = pageFindAll.stream() - .map(ShoppingListMapper::toFindAllShoppingListResShoppingList) - .toList(); - - response.setContent(shoppingList); - response.setCurrentPage(pageFindAll.getNumber()); - response.setPageSize(pageFindAll.getNumberOfElements()); - response.setTotalPages(pageFindAll.getTotalPages()); - response.setTotal(pageFindAll.getTotalElements()); - - return response; + return ShoppingListMapper.toFindAllShoppingListRes(pageFindAll); } public FindByIdShoppingListRes findById(Integer id, User user) { @@ -184,5 +173,4 @@ private void updateProducts(Integer shoppingListId, }); } - } diff --git a/src/main/java/dev/nmarulo/depensaapp/app/unitytypes/UnitTypeMapper.java b/src/main/java/dev/nmarulo/depensaapp/app/unitytypes/UnitTypeMapper.java index e7389ff..d9968e6 100644 --- a/src/main/java/dev/nmarulo/depensaapp/app/unitytypes/UnitTypeMapper.java +++ b/src/main/java/dev/nmarulo/depensaapp/app/unitytypes/UnitTypeMapper.java @@ -18,4 +18,8 @@ public static FindAllUnitTypeRes.UnitType toFindAllUnitTypeResUnitType(final Uni return response; } + public static FindAllUnitTypeRes toFindAllUnitTypeRes(final Page page) { + return pageTo(page, FindAllUnitTypeRes::new, UnitTypeMapper::toFindAllUnitTypeResUnitType); + } + } diff --git a/src/main/java/dev/nmarulo/depensaapp/app/unitytypes/UnitTypeService.java b/src/main/java/dev/nmarulo/depensaapp/app/unitytypes/UnitTypeService.java index e22f8c6..db258f4 100644 --- a/src/main/java/dev/nmarulo/depensaapp/app/unitytypes/UnitTypeService.java +++ b/src/main/java/dev/nmarulo/depensaapp/app/unitytypes/UnitTypeService.java @@ -15,20 +15,9 @@ public class UnitTypeService extends BasicServiceImp { private final UnitTypeRepository unitTypeRepository; public FindAllUnitTypeRes findAll(Pageable pageable) { - var response = new FindAllUnitTypeRes(); var pageFindAll = this.unitTypeRepository.findAll(pageable); - var unitTypeList = pageFindAll.stream() - .map(UnitTypeMapper::toFindAllUnitTypeResUnitType) - .toList(); - - response.setContent(unitTypeList); - response.setCurrentPage(pageFindAll.getNumber()); - response.setPageSize(pageFindAll.getNumberOfElements()); - response.setTotalPages(pageFindAll.getTotalPages()); - response.setTotal(pageFindAll.getTotalElements()); - - return response; + return UnitTypeMapper.toFindAllUnitTypeRes(pageFindAll); }