implementierung von Mappern
da wir sehr oft von Model zu DTO Mappen müssen, lohnt es sich eine seperate Mapperklasse zu erstellen
This commit is contained in:
1 parent
d4b5382f70
commit
e8d35385ca
3 files changed
+56
No files matched your search
@@ -0,0 +1,27 @@
|
||||
package de.timaldinger.einkaufsliste.Mapper;
|
||||
|
||||
import de.timaldinger.einkaufsliste.Dto.Get.ShoppingCategoryGetDTO;
|
||||
import de.timaldinger.einkaufsliste.Models.ShoppingCategory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class ShoppingCategoryMapper {
|
||||
|
||||
@Autowired
|
||||
ShoppingItemMapper shoppingItemMapper;
|
||||
|
||||
public ShoppingCategoryGetDTO toGetDTO(ShoppingCategory category){
|
||||
return new ShoppingCategoryGetDTO(
|
||||
category.getCategoryName(),
|
||||
shoppingItemMapper.toGetDTOs(category.getItemsInCategory()),
|
||||
category.getId()
|
||||
);
|
||||
}
|
||||
|
||||
public List<ShoppingCategoryGetDTO> toGetDTOs(List<ShoppingCategory> categories){
|
||||
return categories.stream().map(this::toGetDTO).toList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package de.timaldinger.einkaufsliste.Mapper;
|
||||
|
||||
import de.timaldinger.einkaufsliste.Dto.Get.ShoppingItemGetDTO;
|
||||
import de.timaldinger.einkaufsliste.Models.ShoppingItem;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class ShoppingItemMapper {
|
||||
public ShoppingItemGetDTO toGetDTO(ShoppingItem item){
|
||||
return new ShoppingItemGetDTO(
|
||||
item.isDone(),
|
||||
item.getItemName(),
|
||||
item.getItemAmount(),
|
||||
item.getList().getId(),
|
||||
item.getCategory() != null ? item.getCategory().getId() : null,
|
||||
item.getId()
|
||||
);
|
||||
}
|
||||
|
||||
public List<ShoppingItemGetDTO> toGetDTOs(List<ShoppingItem> items){
|
||||
return items.stream().map(this::toGetDTO).toList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package de.timaldinger.einkaufsliste.Mapper;
|
||||
|
||||
public class ShoppingListMapper {
|
||||
}
|
||||
Reference in new issue
Block a user