Skip to content

Commit

Permalink
Refactorizado: Mapper
Browse files Browse the repository at this point in the history
- La creación de las clases "response" con paginación se mueve a las clases mapper.
  • Loading branch information
nmarulo committed Nov 8, 2024
1 parent 0154117 commit d0dce3c
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,12 @@ public static FindAllProductRes.Product toFindAllProductResProduct(final Product
return response;
}

public static FindAllShoppingListProductRes toFindAllShoppingListProductRes(final Page<Product> page) {
return pageTo(page, FindAllShoppingListProductRes::new, ProductMapper::toFindAllShoppingListProductResProduct);
}

public static FindAllProductRes toFindAllProductRes(final Page<Product> page) {
return pageTo(page, FindAllProductRes::new, ProductMapper::toFindAllProductResProduct);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public FindAllShoppingListProductRes findAllProductsByShoppingListId(Integer sho
boolean isExclude,
Pageable pageable,
User user) {
var response = new FindAllShoppingListProductRes();
Page<Product> pageFindAll;

if (isExclude) {
Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,8 @@ public static UpdateShoppingListRes toUpdateShoppingListRes(final ShoppingList u
return new UpdateShoppingListRes(update.getId());
}

public static FindAllShoppingListRes toFindAllShoppingListRes(final Page<ShoppingList> page) {
return pageTo(page, FindAllShoppingListRes::new, ShoppingListMapper::toFindAllShoppingListResShoppingList);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -184,5 +173,4 @@ private void updateProducts(Integer shoppingListId,
});
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ public static FindAllUnitTypeRes.UnitType toFindAllUnitTypeResUnitType(final Uni
return response;
}

public static FindAllUnitTypeRes toFindAllUnitTypeRes(final Page<UnitType> page) {
return pageTo(page, FindAllUnitTypeRes::new, UnitTypeMapper::toFindAllUnitTypeResUnitType);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down

0 comments on commit d0dce3c

Please sign in to comment.