Skip to content

Commit 67627dd

Browse files
committed
feat: add ListItemRequestDTO and ListItemResponseDTO for improved data handling and mapping
1 parent f872cc6 commit 67627dd

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.omatheusmesmo.shoppmate.list.dtos;
2+
3+
import jakarta.validation.constraints.Min;
4+
import jakarta.validation.constraints.NotNull;
5+
6+
public record ListItemRequestDTO(
7+
@NotNull(message = "List ID cannot be null")
8+
Long listId,
9+
@NotNull(message = "Item ID cannot be null")
10+
Long itemId,
11+
@Min(1)
12+
Integer quantity
13+
) {
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.omatheusmesmo.shoppmate.list.dtos;
2+
3+
import com.omatheusmesmo.shoppmate.item.dto.ItemResponseDTO;
4+
import jakarta.validation.constraints.Min;
5+
import jakarta.validation.constraints.NotNull;
6+
7+
public record ListItemResponseDTO(
8+
ShoppingListResponseDTO shoppingList,
9+
ItemResponseDTO item,
10+
Long idListItem,
11+
Integer quantity,
12+
Boolean purchased
13+
) {
14+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.omatheusmesmo.shoppmate.list.mapper;
2+
3+
import com.omatheusmesmo.shoppmate.item.entity.Item;
4+
import com.omatheusmesmo.shoppmate.item.mapper.ItemMapper; // Supondo que você tenha/crie um
5+
import com.omatheusmesmo.shoppmate.item.repository.ItemRepository;
6+
import com.omatheusmesmo.shoppmate.list.dtos.ListItemRequestDTO;
7+
import com.omatheusmesmo.shoppmate.list.dtos.ListItemResponseDTO;
8+
import com.omatheusmesmo.shoppmate.list.entity.ListItem;
9+
import com.omatheusmesmo.shoppmate.list.entity.ShoppingList;
10+
import com.omatheusmesmo.shoppmate.list.repository.ShoppingListRepository; // Precisa do repositório
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.stereotype.Component;
13+
14+
import java.util.NoSuchElementException;
15+
16+
@Component
17+
public class ListItemMapper {
18+
19+
@Autowired
20+
private ShoppingListRepository shoppingListRepository;
21+
22+
@Autowired
23+
private ItemRepository itemRepository;
24+
25+
@Autowired
26+
private ListMapper listMapper;
27+
28+
@Autowired
29+
private ItemMapper itemMapper;
30+
31+
public ListItem toEntity(ListItemRequestDTO dto) {
32+
ShoppingList shoppingList = shoppingListRepository.findById(dto.listId())
33+
.orElseThrow(() -> new NoSuchElementException("ShoppingList not found with ID: " + dto.listId()));
34+
35+
Item item = itemRepository.findById(dto.itemId())
36+
.orElseThrow(() -> new NoSuchElementException("Item not found with ID: " + dto.itemId()));
37+
38+
ListItem listItem = new ListItem();
39+
listItem.setShoppList(shoppingList);
40+
listItem.setItem(item);
41+
listItem.setQuantity(dto.quantity());
42+
return listItem;
43+
}
44+
45+
public ListItemResponseDTO toResponseDTO(ListItem listItem) {
46+
return new ListItemResponseDTO(
47+
listMapper.toResponseDTO(listItem.getShoppList()),
48+
itemMapper.toResponseDTO(listItem.getItem()),
49+
listItem.getId(),
50+
listItem.getQuantity(),
51+
listItem.getPurchased()
52+
);
53+
}
54+
55+
public void updateEntityFromDto(ListItemRequestDTO dto, ListItem entity) {
56+
ShoppingList shoppingList = shoppingListRepository.findById(dto.listId())
57+
.orElseThrow(() -> new NoSuchElementException("ShoppingList not found with ID: " + dto.listId()));
58+
Item item = itemRepository.findById(dto.itemId())
59+
.orElseThrow(() -> new NoSuchElementException("Item not found with ID: " + dto.itemId()));
60+
entity.setShoppList(shoppingList);
61+
entity.setItem(item);
62+
63+
entity.setQuantity(dto.quantity());
64+
}
65+
66+
}

0 commit comments

Comments
 (0)