Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿ› [Fix]: ํŒ”๋กœ์šฐ ์ด๋ฉ”์ผ ๋กœ์ง ์ˆ˜์ • #41

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/main/java/dongguk/osori/domain/follow/FollowController.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,18 @@ public ResponseEntity<MyFollowResponse> getMyFollowerList(@SessionAttribute(name
@ApiResponse(responseCode = "404", description = "ํ•ด๋‹น ์ด๋ฉ”์ผ์˜ ์‚ฌ์šฉ์ž๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Œ")
})
@PostMapping()
public ResponseEntity<Void> followUserByEmail(@SessionAttribute(name = "userId", required = false) Long userId, @RequestBody FollowRequestDto followRequestDto) {
public ResponseEntity<Void> followUserByEmail(@SessionAttribute(name = "userId", required = false) Long userId,
@RequestBody FollowRequestDto followRequestDto) {
if (userId == null) {
return ResponseEntity.status(401).build();
}

User userToFollow = userService.findUserByEmail(followRequestDto.getEmail());
if (userToFollow == null) {
return ResponseEntity.status(404).build();
try {
followService.followUserByEmail(userId, followRequestDto.getEmail());
return ResponseEntity.ok().build();
} catch (IllegalArgumentException e) {
return ResponseEntity.status(404).body(null);
}

followService.followUser(userId, userToFollow.getUserId());
return ResponseEntity.ok().build();
}

@Operation(summary = "์–ธํŒ”๋กœ์šฐ", description = "์‚ฌ์šฉ์ž๋ฅผ ์–ธํŒ”๋กœ์šฐํ•ฉ๋‹ˆ๋‹ค.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,23 @@ public void followUser(Long userId, Long followingUserId) {
followRepository.save(follow);
}

@Transactional
public void followUserByEmail(Long userId, String email) {
User follower = userRepository.findById(userId)
.orElseThrow(() -> new IllegalArgumentException("Logged-in user not found with id: " + userId));

User following = userRepository.findByEmail(email)
.orElseThrow(() -> new IllegalArgumentException("User not found with email: " + email));

boolean alreadyFollowing = followRepository.findByFollowerAndFollowing(follower, following).isPresent();
if (alreadyFollowing) {
throw new IllegalArgumentException("You are already following this user.");
}

followRepository.save(new Follow(follower, following));
}


// ์–ธํŒ”๋กœ์šฐ
@Transactional
public void unfollowUser(Long userId, Long followingId) {
Expand Down
Loading