Skip to content

Commit 7b0a907

Browse files
committed
feat: Follow 생성, 취소 구현
1 parent 083b793 commit 7b0a907

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.example.log4u.domain.follow.controller;
2+
3+
import org.springframework.http.ResponseEntity;
4+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.web.bind.annotation.DeleteMapping;
7+
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.PostMapping;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
11+
import com.example.log4u.common.oauth2.dto.CustomOAuth2User;
12+
import com.example.log4u.domain.follow.service.FollowService;
13+
14+
import io.swagger.v3.oas.annotations.tags.Tag;
15+
import lombok.RequiredArgsConstructor;
16+
17+
@Tag(name = "팔로우 API")
18+
@Controller
19+
@RequiredArgsConstructor
20+
@RequestMapping("/users")
21+
public class FollowController {
22+
private final FollowService followService;
23+
24+
@PostMapping("/{nickname}/follow")
25+
public ResponseEntity<Void> createFollow(
26+
@AuthenticationPrincipal CustomOAuth2User customOAuth2User,
27+
@PathVariable("nickname") String nickname
28+
) {
29+
followService.createFollow(customOAuth2User.getUserId(), nickname);
30+
return ResponseEntity.ok().build();
31+
}
32+
33+
@DeleteMapping("/{nickname}/follow")
34+
public ResponseEntity<Void> deleteFollow(
35+
@AuthenticationPrincipal CustomOAuth2User customOAuth2User,
36+
@PathVariable("nickname") String nickname
37+
) {
38+
followService.deleteFollow(customOAuth2User.getUserId(), nickname);
39+
return ResponseEntity.ok().build();
40+
}
41+
42+
}

src/main/java/com/example/log4u/domain/follow/repository/FollowRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@
88
@Repository
99
public interface FollowRepository extends JpaRepository<Follow, Long> {
1010
boolean existsByInitiatorIdAndTargetId(Long initiatorId, Long targetId);
11+
12+
void deleteByInitiatorIdAndTargetId(Long initiatorId, Long targetId);
1113
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.example.log4u.domain.follow.service;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
import com.example.log4u.domain.follow.entitiy.Follow;
6+
import com.example.log4u.domain.follow.exception.FollowNotFoundException;
7+
import com.example.log4u.domain.follow.repository.FollowRepository;
8+
import com.example.log4u.domain.user.service.UserService;
9+
10+
import jakarta.transaction.Transactional;
11+
import lombok.RequiredArgsConstructor;
12+
13+
@Service
14+
@RequiredArgsConstructor
15+
public class FollowService {
16+
private final FollowRepository followRepository;
17+
private final UserService userService;
18+
19+
@Transactional
20+
public void createFollow(Long initiatorId, String nickname) {
21+
validateTargetUser(nickname);
22+
followRepository.save(Follow.of(
23+
initiatorId,
24+
userService.getUserIdByNickname(nickname)));
25+
}
26+
27+
@Transactional
28+
public void deleteFollow(Long userId, String nickname) {
29+
Long targetId = userService.getUserIdByNickname(nickname);
30+
validateFollow(userId, targetId);
31+
followRepository.deleteByInitiatorIdAndTargetId(userId, targetId);
32+
}
33+
34+
private void validateTargetUser(String nickname) {
35+
// USER NOT FOUND EXCEPTION
36+
userService.getUserByNickname(nickname);
37+
}
38+
39+
private void validateFollow(Long userId, Long targetId) {
40+
if (!followRepository.existsByInitiatorIdAndTargetId(userId, targetId)) {
41+
throw new FollowNotFoundException();
42+
}
43+
}
44+
45+
public Long getFollowerCount() {
46+
//TODO: 구현
47+
return 0L;
48+
}
49+
50+
public Long getFollowingCount() {
51+
//TODO: 구현
52+
return 0L;
53+
}
54+
}

0 commit comments

Comments
 (0)