1
1
package com .vp .voicepocket .domain .friend .service ;
2
2
3
- import com .vp .voicepocket .domain .firebase .dto .FCMNotificationRequestDto ;
4
- import com .vp .voicepocket .domain .firebase .entity .FCMUserToken ;
5
- import com .vp .voicepocket .domain .firebase .exception .CFCMTokenNotFoundException ;
6
- import com .vp .voicepocket .domain .firebase .repository .FCMRepository ;
7
- import com .vp .voicepocket .domain .firebase .service .FCMNotificationService ;
8
3
import com .vp .voicepocket .domain .friend .dto .request .FriendRequestDto ;
9
4
import com .vp .voicepocket .domain .friend .dto .response .FriendResponseDto ;
10
5
import com .vp .voicepocket .domain .friend .entity .Friend ;
11
6
import com .vp .voicepocket .domain .friend .entity .Status ;
7
+ import com .vp .voicepocket .domain .friend .event .FriendAcceptPushEvent ;
8
+ import com .vp .voicepocket .domain .friend .event .FriendRequestPushEvent ;
12
9
import com .vp .voicepocket .domain .friend .exception .CFriendRequestNotExistException ;
13
10
import com .vp .voicepocket .domain .friend .exception .CFriendRequestOnGoingException ;
14
11
import com .vp .voicepocket .domain .friend .repository .FriendRepository ;
19
16
import com .vp .voicepocket .domain .user .exception .CUserNotFoundException ;
20
17
import com .vp .voicepocket .domain .user .repository .UserRepository ;
21
18
import lombok .RequiredArgsConstructor ;
19
+ import org .springframework .context .ApplicationEventPublisher ;
22
20
import org .springframework .security .core .Authentication ;
23
21
import org .springframework .stereotype .Service ;
24
22
import org .springframework .transaction .annotation .Transactional ;
25
23
26
- import java .util .HashMap ;
27
24
import java .util .List ;
28
25
import java .util .stream .Collectors ;
29
26
30
27
@ Service
31
28
@ RequiredArgsConstructor
32
29
public class FriendService {
33
30
private final UserRepository userRepository ;
34
- private final FCMRepository fcmRepository ;
35
31
private final FriendRepository friendRepository ;
36
- private final FCMNotificationService fcmNotificationService ;
32
+ private final ApplicationEventPublisher eventPublisher ;
37
33
38
34
private final JwtProvider jwtProvider ;
35
+
39
36
@ Transactional
40
37
public FriendResponseDto requestFriend (FriendRequestDto friendRequestDto , String accessToken ) {
41
- Authentication authentication = getAuthByAccessToken (accessToken );
38
+ Authentication authentication = getAuthByAccessToken (accessToken );
42
39
43
- User to_user =
44
- userRepository .findByEmail (friendRequestDto .getEmail ())
45
- .orElseThrow (CUserNotFoundException ::new );
40
+ User to_user = userRepository .findByEmail (friendRequestDto .getEmail ())
41
+ .orElseThrow (CUserNotFoundException ::new );
46
42
47
- User from_user =
48
- userRepository .findById (Long .parseLong (authentication .getName ()))
49
- .orElseThrow (CUserNotFoundException ::new );
43
+ User from_user = userRepository .findById (Long .parseLong (authentication .getName ()))
44
+ .orElseThrow (CUserNotFoundException ::new );
50
45
51
46
if (friendRepository .findByRequest (from_user , to_user , Status .ONGOING ).isPresent () ||
52
- friendRepository .findByRequest (from_user , to_user , Status .ACCEPT ).isPresent ()) {
47
+ friendRepository .findByRequest (from_user , to_user , Status .ACCEPT ).isPresent ()) {
53
48
throw new CFriendRequestOnGoingException ();
54
49
}
55
50
56
- Friend friend = friendRequestDto .toEntity (from_user , to_user , Status .ONGOING );
57
-
58
- HashMap <String , String > data = new HashMap <>();
59
- data .put ("ID" , "1" );
60
-
61
- FCMUserToken fcmEntity = fcmRepository .findByUserId (to_user )
62
- .orElseThrow (CFCMTokenNotFoundException ::new );
63
- String fcmToken = fcmEntity .getFireBaseToken ();
64
-
65
- FCMNotificationRequestDto fcmNotificationRequestDto = FCMNotificationRequestDto .builder ()
66
- .firebaseToken (fcmToken )
67
- .title ("Friend Request" )
68
- .body (from_user .getName () + " request Friend to you!" )
69
- .build ();
51
+ Friend friendRequest = friendRequestDto .toEntity (from_user , to_user , Status .ONGOING );
70
52
71
- fcmNotificationService . sendNotificationWithData ( fcmNotificationRequestDto , data );
53
+ eventPublisher . publishEvent ( new FriendRequestPushEvent ( friendRequest ) );
72
54
73
- return mapFriendEntityToFriendResponseDTO (friendRepository .save (friend ));
55
+ return mapFriendEntityToFriendResponseDTO (friendRepository .save (friendRequest ));
74
56
}
75
57
76
58
private Authentication getAuthByAccessToken (String accessToken ) {
@@ -83,73 +65,72 @@ private Authentication getAuthByAccessToken(String accessToken) {
83
65
return jwtProvider .getAuthentication (accessToken );
84
66
}
85
67
86
- private FriendResponseDto mapFriendEntityToFriendResponseDTO (Friend friend ){
68
+ private FriendResponseDto mapFriendEntityToFriendResponseDTO (Friend friend ) {
87
69
return FriendResponseDto .builder ()
88
- .id (friend .getId ())
89
- .request_from (new UserResponseDto (friend .getRequest_from ()))
90
- .request_to (new UserResponseDto (friend .getRequest_to ()))
91
- .status (friend .getStatus ())
92
- .build ();
70
+ .id (friend .getId ())
71
+ .request_from (new UserResponseDto (friend .getRequest_from ()))
72
+ .request_to (new UserResponseDto (friend .getRequest_to ()))
73
+ .status (friend .getStatus ())
74
+ .build ();
93
75
}
94
76
95
77
@ Transactional
96
78
public List <FriendResponseDto > checkRequest (String accessToken ) {
97
- Authentication authentication = getAuthByAccessToken (accessToken );
98
- User to_user = userRepository .findById (Long .parseLong (authentication .getName ())).orElseThrow (CUserNotFoundException ::new );
79
+ Authentication authentication = getAuthByAccessToken (accessToken );
80
+
81
+ User to_user = userRepository .findById (Long .parseLong (authentication .getName ()))
82
+ .orElseThrow (CUserNotFoundException ::new );
83
+
99
84
return friendRepository .findByToUser (to_user , Status .ONGOING ) // 없을 때 공백 리스트를 반환하기
100
- .stream ()
101
- .map (this ::mapFriendEntityToFriendResponseDTO )
102
- .collect (Collectors .toList ());
85
+ .stream ()
86
+ .map (this ::mapFriendEntityToFriendResponseDTO )
87
+ .collect (Collectors .toList ());
103
88
}
104
89
105
90
@ Transactional
106
91
public List <FriendResponseDto > checkResponse (String accessToken ) {
107
- Authentication authentication = getAuthByAccessToken (accessToken );
108
- User from_user = userRepository .findById (Long .parseLong (authentication .getName ())).orElseThrow (CUserNotFoundException ::new );
92
+ Authentication authentication = getAuthByAccessToken (accessToken );
93
+
94
+ User from_user = userRepository .findById (Long .parseLong (authentication .getName ()))
95
+ .orElseThrow (CUserNotFoundException ::new );
96
+
109
97
return friendRepository .findByFromUser (from_user , Status .ACCEPT ) // 없을 때 공백 리스트를 반환하기
110
- .stream ()
111
- .map (this ::mapFriendEntityToFriendResponseDTO )
112
- .collect (Collectors .toList ());
98
+ .stream ()
99
+ .map (this ::mapFriendEntityToFriendResponseDTO )
100
+ .collect (Collectors .toList ());
113
101
}
114
102
115
103
116
104
@ Transactional
117
- public void update (FriendRequestDto friendRequestDto , String accessToken , Status status ){
118
- Authentication authentication = getAuthByAccessToken (accessToken );
105
+ public void update (FriendRequestDto friendRequestDto , String accessToken , Status status ) {
106
+ Authentication authentication = getAuthByAccessToken (accessToken );
119
107
120
108
User from_user = userRepository .findByEmail (friendRequestDto .getEmail ())
121
- .orElseThrow (CUserNotFoundException ::new );
109
+ .orElseThrow (CUserNotFoundException ::new );
122
110
User to_user = userRepository .findById (Long .parseLong (authentication .getName ()))
123
- .orElseThrow (CUserNotFoundException ::new );
124
-
125
- Friend modifiedFriend = friendRepository .findByRequest (from_user , to_user , Status .ONGOING )
126
- .orElseThrow (CFriendRequestNotExistException ::new );
127
- modifiedFriend .updateStatus (status );
128
-
129
- if (status .equals (Status .ACCEPT )){
130
- HashMap <String , String > data = new HashMap <>();
131
- data .put ("ID" , "2" );
132
-
133
- FCMUserToken fcmEntity = fcmRepository .findByUserId (from_user )
134
- .orElseThrow (CFCMTokenNotFoundException ::new );
135
- String fcmToken = fcmEntity .getFireBaseToken ();
111
+ .orElseThrow (CUserNotFoundException ::new );
136
112
137
- FCMNotificationRequestDto fcmNotificationRequestDto = FCMNotificationRequestDto .builder ()
138
- .firebaseToken (fcmToken )
139
- .title ("Friend Accept" )
140
- .body (to_user .getName () + " Accept your Friend Request!" )
141
- .build ();
113
+ Friend friendRequest = friendRepository .findByRequest (from_user , to_user , Status .ONGOING )
114
+ .orElseThrow (CFriendRequestNotExistException ::new );
115
+ friendRequest .updateStatus (status );
142
116
143
- fcmNotificationService .sendNotificationWithData (fcmNotificationRequestDto , data );
117
+ if (status .equals (Status .ACCEPT )) {
118
+ eventPublisher .publishEvent (new FriendAcceptPushEvent (friendRequest ));
144
119
}
145
120
}
146
121
147
122
@ Transactional
148
- public void delete (FriendRequestDto friendRequestDto , String accessToken , Status status ){
149
- Authentication authentication = getAuthByAccessToken (accessToken );
150
- User from_user = userRepository .findById (Long .parseLong (authentication .getName ())).orElseThrow (CUserNotFoundException ::new );
151
- User to_user = userRepository .findByEmail (friendRequestDto .getEmail ()).orElseThrow (CUserNotFoundException ::new );
152
- Friend friend = friendRepository .findByRequest (from_user , to_user , status ).orElseThrow (CFriendRequestNotExistException ::new );
123
+ public void delete (FriendRequestDto friendRequestDto , String accessToken , Status status ) {
124
+ Authentication authentication = getAuthByAccessToken (accessToken );
125
+
126
+ User from_user = userRepository .findById (Long .parseLong (authentication .getName ()))
127
+ .orElseThrow (CUserNotFoundException ::new );
128
+ User to_user = userRepository .findByEmail (friendRequestDto .getEmail ())
129
+ .orElseThrow (CUserNotFoundException ::new );
130
+
131
+ Friend friend = friendRepository .findByRequest (from_user , to_user , status )
132
+ .orElseThrow (CFriendRequestNotExistException ::new );
133
+
153
134
friendRepository .delete (friend );
154
135
}
155
136
}
0 commit comments