5
5
import com .solucitation .midpoint_backend .domain .FavPlace .repository .FavPlaceRepository ;
6
6
import com .solucitation .midpoint_backend .domain .member .entity .Member ;
7
7
import com .solucitation .midpoint_backend .domain .member .repository .MemberRepository ;
8
+ import lombok .AllArgsConstructor ;
9
+ import lombok .Getter ;
8
10
import lombok .RequiredArgsConstructor ;
11
+ import lombok .Setter ;
9
12
import org .springframework .stereotype .Service ;
10
13
import org .springframework .transaction .annotation .Transactional ;
11
14
15
+ import java .util .HashMap ;
12
16
import java .util .List ;
17
+ import java .util .Map ;
18
+ import java .util .stream .Collectors ;
13
19
14
20
@ Service
15
21
@ RequiredArgsConstructor
@@ -39,11 +45,11 @@ public FavPlace saveFavoritePlace(String addr, Float latitude, Float longitude,
39
45
}
40
46
41
47
@ Transactional
42
- public void deleteFavoritePlace (Long favPlaceId , String email ) {
48
+ public void deleteFavoritePlace (FavPlace . AddrType addrType , String email ) {
43
49
Member member = memberRepository .findByEmail (email )
44
50
.orElseThrow (() -> new IllegalArgumentException ("해당 이메일의 회원이 존재하지 않습니다." ));
45
51
46
- FavPlace favPlace = favPlaceRepository .findById ( favPlaceId )
52
+ FavPlace favPlace = favPlaceRepository .findByAddrTypeAndMemberId ( addrType , member . getId () )
47
53
.orElseThrow (() -> new RuntimeException ("존재하지 않는 장소입니다." ));
48
54
49
55
if (!favPlace .getMember ().equals (member )) {
@@ -54,11 +60,11 @@ public void deleteFavoritePlace(Long favPlaceId, String email) {
54
60
}
55
61
56
62
@ Transactional (readOnly = true )
57
- public FavPlace getFavoritePlaceDetails (Long favPlaceId , String email ) {
63
+ public FavPlace getFavoritePlaceDetails (FavPlace . AddrType addrType , String email ) {
58
64
Member member = memberRepository .findByEmail (email )
59
65
.orElseThrow (() -> new IllegalArgumentException ("해당 이메일의 회원이 존재하지 않습니다." ));
60
66
61
- FavPlace favPlace = favPlaceRepository .findById ( favPlaceId )
67
+ FavPlace favPlace = favPlaceRepository .findByAddrTypeAndMemberId ( addrType , member . getId () )
62
68
.orElseThrow (() -> new RuntimeException ("존재하지 않는 장소입니다." ));
63
69
64
70
if (!favPlace .getMember ().equals (member )) {
@@ -73,36 +79,36 @@ public List<FavPlaceResponse> getAllFavoritePlaces(String email) {
73
79
Member member = memberRepository .findByEmail (email )
74
80
.orElseThrow (() -> new IllegalArgumentException ("해당 이메일의 회원이 존재하지 않습니다." ));
75
81
76
- FavPlaceResponse homeResponse = new FavPlaceResponse ("HOME" );
77
- FavPlaceResponse workResponse = new FavPlaceResponse ("WORK" );
78
-
79
82
List <FavPlace > favPlaces = favPlaceRepository .findAllByMemberId (member .getId ());
80
83
84
+ Map <FavPlace .AddrType , FavPlaceResponse > responseMap = new HashMap <>();
85
+
86
+ responseMap .put (FavPlace .AddrType .HOME , new FavPlaceResponse (null , null , FavPlace .AddrType .HOME .name ()));
87
+ responseMap .put (FavPlace .AddrType .WORK , new FavPlaceResponse (null , null , FavPlace .AddrType .WORK .name ()));
88
+
81
89
for (FavPlace favPlace : favPlaces ) {
82
- if (favPlace .getAddrType () == FavPlace .AddrType .HOME ) {
83
- homeResponse = new FavPlaceResponse (
90
+ FavPlace .AddrType addrType = favPlace .getAddrType ();
91
+ if (responseMap .containsKey (addrType )) {
92
+ responseMap .put (addrType , new FavPlaceResponse (
84
93
favPlace .getFavPlaceId (),
85
94
favPlace .getAddr (),
86
- favPlace .getAddrType ().name ()
87
- );
88
- } else if (favPlace .getAddrType () == FavPlace .AddrType .WORK ) {
89
- workResponse = new FavPlaceResponse (
90
- favPlace .getFavPlaceId (),
91
- favPlace .getAddr (),
92
- favPlace .getAddrType ().name ()
93
- );
95
+ addrType .name ()
96
+ ));
94
97
}
95
98
}
96
99
97
- return List .of (homeResponse , workResponse );
100
+ List <FavPlaceResponse > responses = responseMap .values ().stream ()
101
+ .collect (Collectors .toList ());
102
+
103
+ return responses ;
98
104
}
99
105
100
106
@ Transactional
101
- public FavPlace updateFavoritePlace (Long favPlaceId , String addr , Float latitude , Float longitude , String email ) {
107
+ public FavPlace updateFavoritePlace (FavPlace . AddrType addrType , String addr , Float latitude , Float longitude , String email ) {
102
108
Member member = memberRepository .findByEmail (email )
103
109
.orElseThrow (() -> new IllegalArgumentException ("해당 이메일의 회원이 존재하지 않습니다." ));
104
110
105
- FavPlace favPlace = favPlaceRepository .findById ( favPlaceId )
111
+ FavPlace favPlace = favPlaceRepository .findByAddrTypeAndMemberId ( addrType , member . getId () )
106
112
.orElseThrow (() -> new RuntimeException ("존재하지 않는 장소입니다." ));
107
113
108
114
if (!favPlace .getMember ().equals (member )) {
0 commit comments