1
+ package com .omatheusmesmo .shoppmate .list .mapper ;
2
+
3
+ import com .omatheusmesmo .shoppmate .list .entity .Permission ;
4
+ import com .omatheusmesmo .shoppmate .user .dtos .UserResponseDTO ;
5
+ import com .omatheusmesmo .shoppmate .user .entity .User ;
6
+ import com .omatheusmesmo .shoppmate .user .repository .UserRepository ;
7
+ import com .omatheusmesmo .shoppmate .list .dtos .ListPermissionRequestDTO ;
8
+ import com .omatheusmesmo .shoppmate .list .dtos .ListPermissionResponseDTO ;
9
+ import com .omatheusmesmo .shoppmate .list .entity .ListPermission ;
10
+ import com .omatheusmesmo .shoppmate .list .entity .ShoppingList ;
11
+ import com .omatheusmesmo .shoppmate .list .repository .ShoppingListRepository ;
12
+ import org .springframework .beans .factory .annotation .Autowired ;
13
+ import org .springframework .stereotype .Component ;
14
+
15
+ import java .util .NoSuchElementException ;
16
+
17
+ @ Component
18
+ public class ListPermissionMapper {
19
+
20
+ @ Autowired
21
+ private ShoppingListRepository shoppingListRepository ;
22
+
23
+ @ Autowired
24
+ private UserRepository userRepository ;
25
+
26
+ @ Autowired
27
+ private ListMapper listMapper ;
28
+
29
+ public ListPermission toEntity (ListPermissionRequestDTO dto ) {
30
+ ShoppingList shoppingList = shoppingListRepository .findById (dto .idList ())
31
+ .orElseThrow (() -> new NoSuchElementException ("ShoppingList not found with ID: " + dto .idList ()));
32
+
33
+ User user = userRepository .findById (dto .idUser ())
34
+ .orElseThrow (() -> new NoSuchElementException ("User not found with ID: " + dto .idUser ()));
35
+
36
+ ListPermission listPermission = new ListPermission ();
37
+ listPermission .setShoppingList (shoppingList );
38
+ listPermission .setPermission (dto .permission ());
39
+ listPermission .setUser (user );
40
+ return listPermission ;
41
+ }
42
+
43
+ public ListPermissionResponseDTO toResponseDTO (ListPermission listPermission ) {
44
+ UserResponseDTO userResponseDTO = new UserResponseDTO (
45
+ listPermission .getUser ().getId (),
46
+ listPermission .getUser ().getFullName (),
47
+ listPermission .getUser ().getEmail ());
48
+
49
+ return new ListPermissionResponseDTO (
50
+ listPermission .getId (),
51
+ listMapper .toResponseDTO (listPermission .getShoppingList ()),
52
+ userResponseDTO ,
53
+ listPermission .getPermission ()
54
+ );
55
+ }
56
+
57
+ // public void updateEntityFromDto(ListPermissionRequestDTO dto, ListPermission entity) {
58
+ // ShoppingList shoppingList = shoppingListRepository.findById(dto.listId())
59
+ // .orElseThrow(() -> new NoSuchElementException("ShoppingList not found with ID: " + dto.listId()));
60
+ // Permission permission = userRepository.findById(dto.permissionId())
61
+ // .orElseThrow(() -> new NoSuchElementException("Permission not found with ID: " + dto.permissionId()));
62
+ // entity.setShoppList(shoppingList);
63
+ // entity.setPermission(permission);
64
+ //
65
+ // entity.setQuantity(dto.quantity());
66
+ // }
67
+
68
+ }
0 commit comments