Skip to content

Commit 1ab3819

Browse files
Null value exception added in friend
1 parent 89f3a69 commit 1ab3819

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

src/main/java/org/spacehub/service/Friend/FriendService.java

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ public FriendService(FriendsRepository friendsRepository,
4343

4444
public String sendFriendRequest(String userEmail, String friendEmail) {
4545

46+
if (userEmail == null || userEmail.isBlank())
47+
throw new RuntimeException("UserEmail is required");
48+
49+
if (friendEmail == null || friendEmail.isBlank())
50+
throw new RuntimeException("FriendEmail is required");
51+
4652
if (userEmail.equalsIgnoreCase(friendEmail)) {
4753
return "You cannot send a friend request to yourself.";
4854
}
@@ -76,6 +82,13 @@ public String sendFriendRequest(String userEmail, String friendEmail) {
7682
}
7783

7884
public String respondFriendRequest(String userEmail, String requesterEmail, boolean accept) {
85+
86+
if (userEmail == null || userEmail.isBlank())
87+
throw new RuntimeException("UserEmail is required");
88+
89+
if (requesterEmail == null || requesterEmail.isBlank())
90+
throw new RuntimeException("RequesterEmail is required");
91+
7992
if (userEmail.equalsIgnoreCase(requesterEmail)) {
8093
return "Invalid operation.";
8194
}
@@ -133,6 +146,10 @@ public String respondFriendRequest(String userEmail, String requesterEmail, bool
133146
}
134147

135148
public List<UserOutput> getFriends(String userEmail) {
149+
150+
if (userEmail == null || userEmail.isBlank())
151+
throw new RuntimeException("UserEmail is required");
152+
136153
User user = userRepository.findByEmail(userEmail).orElseThrow(() -> new RuntimeException("User not found"));
137154

138155
List<Friends> sent = friendsRepository.findByUserAndStatus(user, "accepted");
@@ -148,8 +165,7 @@ public List<UserOutput> getFriends(String userEmail) {
148165
f.getEmail(),
149166
preview
150167
);
151-
})
152-
.collect(Collectors.toList());
168+
}).collect(Collectors.toList());
153169

154170
friendsList.addAll(received.stream()
155171
.map(friend -> {
@@ -161,13 +177,19 @@ public List<UserOutput> getFriends(String userEmail) {
161177
f.getEmail(),
162178
preview
163179
);
164-
})
165-
.toList());
180+
}).toList());
166181

167182
return friendsList;
168183
}
169184

170185
public String blockFriend(String userEmail, String friendEmail) {
186+
187+
if (userEmail == null || userEmail.isBlank())
188+
throw new RuntimeException("UserEmail is required");
189+
190+
if (friendEmail == null || friendEmail.isBlank())
191+
throw new RuntimeException("FriendEmail is required");
192+
171193
if (userEmail.equalsIgnoreCase(friendEmail)) {
172194
return "You cannot block yourself.";
173195
}
@@ -191,6 +213,10 @@ public String blockFriend(String userEmail, String friendEmail) {
191213

192214

193215
public List<UserOutput> getIncomingPendingRequests(String userEmail) {
216+
217+
if (userEmail == null || userEmail.isBlank())
218+
throw new RuntimeException("UserEmail is required");
219+
194220
User user = userRepository.findByEmail(userEmail)
195221
.orElseThrow(() -> new RuntimeException("User not found"));
196222

@@ -207,6 +233,10 @@ public List<UserOutput> getIncomingPendingRequests(String userEmail) {
207233
}
208234

209235
public List<UserOutput> getOutgoingPendingRequests(String userEmail) {
236+
237+
if (userEmail == null || userEmail.isBlank())
238+
throw new RuntimeException("UserEmail is required");
239+
210240
User user = userRepository.findByEmail(userEmail)
211241
.orElseThrow(() -> new RuntimeException("User not found"));
212242

@@ -217,12 +247,19 @@ public List<UserOutput> getOutgoingPendingRequests(String userEmail) {
217247
f.getFriend().getId(),
218248
f.getFriend().getUsername(),
219249
f.getFriend().getEmail(),
220-
f.getUser().getAvatarUrl()
250+
f.getFriend().getAvatarUrl()
221251
))
222252
.collect(Collectors.toList());
223253
}
224254

225255
public String unblockUser(String userEmail, String blockedUserEmail) {
256+
257+
if (userEmail == null || userEmail.isBlank())
258+
throw new RuntimeException("UserEmail is required");
259+
260+
if (blockedUserEmail == null || blockedUserEmail.isBlank())
261+
throw new RuntimeException("BlockedUserEmail is required");
262+
226263
if (userEmail.equalsIgnoreCase(blockedUserEmail)) {
227264
return "You cannot unblock yourself.";
228265
}
@@ -242,9 +279,12 @@ public String unblockUser(String userEmail, String blockedUserEmail) {
242279
}
243280

244281
public String removeFriend(String userEmail, String friendEmail) {
245-
if (userEmail == null || friendEmail == null) {
246-
throw new RuntimeException("Both userEmail and friendEmail are required");
247-
}
282+
283+
if (userEmail == null || userEmail.isBlank())
284+
throw new RuntimeException("UserEmail is required");
285+
286+
if (friendEmail == null || friendEmail.isBlank())
287+
throw new RuntimeException("FriendEmail is required");
248288

249289
if (userEmail.equalsIgnoreCase(friendEmail)) {
250290
return "Invalid operation: cannot remove yourself.";

src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ spring.datasource.url=${SPRING_DATASOURCE_URL}
22
spring.datasource.username=${SPRING_DATASOURCE_USERNAME}
33
spring.datasource.password=${SPRING_DATASOURCE_PASSWORD}
44
spring.datasource.driver-class-name=org.postgresql.Driver
5-
spring.jpa.hibernate.ddl-auto=create-drop
5+
spring.jpa.hibernate.ddl-auto=update
66
spring.jpa.show-sql=true
77
spring.jpa.properties.hibernate.format_sql=true
88
spring.jpa.database=postgresql

0 commit comments

Comments
 (0)