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