'I was trying to orient my repository in domain terms with mapstruct, but one method is giving me troubles
I was trying to convert some repositories to domain repositories, in a attempt of implementing a data mapper pattern but when i try to refartor some methods that implement crudRepository interfaces it say that the requiered value is list of products and im giving it a optional list of products but the value that i have to return in the method is Optional List of products this is the code of the method:
@Override
public Optional<List<Product>> getByCategory(int categoryId) {
Optional<Producto> productos = productocrudrepo.findById(categoryId);
return Optional.of(productMapper.toProducts(productos));
}
this is the code of the interface Productrepo:
Optional<List<Product>> getByCategory(int categoryId);
this is the code of the mapStruct of the class:
@Mapper(componentModel = "spring",uses = {CategoryMapper.class})
public interface ProductMapper {
@Mappings({
@Mapping(source = "idProducto",target = "productId"),
@Mapping(source = "nombre",target = "name"),
@Mapping(source = "idCategoria",target = "categoryId"),
@Mapping(source = "precioVenta",target = "sellPrice"),
@Mapping(source = "cantidadStock",target = "stock"),
@Mapping(source = "estado",target = "active"),
@Mapping(source = "categoria", target = "category"),
})
Product toProduct(Producto producto);
List<Product> toProducts(List<Producto> productos);
@InheritInverseConfiguration
@Mappings({
@Mapping(target = "codigoBarras", ignore = true),
@Mapping(target = "compraProductos",ignore = true)
})
Producto toProducto(Product product);
}
I also try modify the method in the following ways:
removing the Optional.of
public Optional<List<Product>> getByCategory(int categoryId) {
Optional<Producto> productos = productocrudrepo.findById(categoryId);
return (productMapper.toProducts(productos));
}
Solution 1:[1]
The Optional class has a map method which you can use to convert what it contains, without the need to extract the value from the optional. Basically it does the following: if the value is present then call the supplied function with it and return an Optional containing the function's result, otherwise return an empty Optional object.
for example with Optional<A>, Optional<B> and a method which can map A into B. What you can then do is the following:
Optional<B> mapOptionalAToOptionalB(Optional<A> optA){
return optA.map( a -> mapAtoB( a ) );
// can also be written as optA.map( this::mapAtoB );
}
// the available method to map A into B.
B mapAtoB(A a){
// do mapping
}
Concerning your repository, you probably want to use a method there that returns a list of Producto, based on the input categoryId. Currently you have findById in your code there which will only return a single item with that id and not all items with that categoryId. So you will probably also want to change that.
Here is an example of what you could end up with:
@Override
public Optional<List<Product>> getByCategory(int categoryId) {
Optional<List<Producto>> productos = productocrudrepo.getByCategory(categoryId);
return productos.map(productMapper::toProducts);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ben Zegveld |
