From 92da6139eabef138ad38d8b42dd4a95dfea9181c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=ED=98=84=EC=A3=BC?= Date: Fri, 5 Jul 2024 00:45:19 +0900 Subject: [PATCH] =?UTF-8?q?[feat/3]=20=EC=B9=9C=EA=B5=AC=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80,=20=EC=B9=9C=EA=B5=AC=20=EC=82=AD=EC=A0=9C,=20?= =?UTF-8?q?=EC=B9=9C=EA=B5=AC=20=EB=AA=A9=EB=A1=9D=20=EC=A1=B0=ED=9A=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/umc/dream/apiPayload/ApiResponse.java | 2 +- .../apiPayload/code/status/SuccessStatus.java | 4 +- .../dream/controller/FollowController.java | 39 ++++++++++++ .../umc/dream/converter/FollowConverter.java | 13 ++++ .../com/umc/dream/dto/FollowRequestDto.java | 19 ++++++ .../com/umc/dream/dto/FollowResponseDto.java | 16 +++++ .../umc/dream/dto/ViewFollowResponseDto.java | 15 +++++ .../dream/repository/FollowRepository.java | 13 ++++ .../umc/dream/repository/UserRepository.java | 7 +++ .../com/umc/dream/service/FollowService.java | 62 +++++++++++++++++++ 10 files changed, 187 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/umc/dream/controller/FollowController.java create mode 100644 src/main/java/com/umc/dream/converter/FollowConverter.java create mode 100644 src/main/java/com/umc/dream/dto/FollowRequestDto.java create mode 100644 src/main/java/com/umc/dream/dto/FollowResponseDto.java create mode 100644 src/main/java/com/umc/dream/dto/ViewFollowResponseDto.java create mode 100644 src/main/java/com/umc/dream/repository/FollowRepository.java create mode 100644 src/main/java/com/umc/dream/repository/UserRepository.java create mode 100644 src/main/java/com/umc/dream/service/FollowService.java diff --git a/src/main/java/com/umc/dream/apiPayload/ApiResponse.java b/src/main/java/com/umc/dream/apiPayload/ApiResponse.java index f02bec2..8b362c2 100644 --- a/src/main/java/com/umc/dream/apiPayload/ApiResponse.java +++ b/src/main/java/com/umc/dream/apiPayload/ApiResponse.java @@ -5,7 +5,7 @@ import com.fasterxml.jackson.annotation.JsonPropertyOrder; import lombok.AllArgsConstructor; import lombok.Getter; -import umc.spring.apiPayload.code.status.SuccessStatus; +import com.umc.dream.apiPayload.code.status.SuccessStatus; @Getter @AllArgsConstructor diff --git a/src/main/java/com/umc/dream/apiPayload/code/status/SuccessStatus.java b/src/main/java/com/umc/dream/apiPayload/code/status/SuccessStatus.java index f25f9bb..fcca4ff 100644 --- a/src/main/java/com/umc/dream/apiPayload/code/status/SuccessStatus.java +++ b/src/main/java/com/umc/dream/apiPayload/code/status/SuccessStatus.java @@ -3,8 +3,8 @@ import lombok.AllArgsConstructor; import lombok.Getter; import org.springframework.http.HttpStatus; -import umc.spring.apiPayload.code.BaseCode; -import umc.spring.apiPayload.code.ReasonDTO; +import com.umc.dream.apiPayload.code.BaseCode; +import com.umc.dream.apiPayload.code.ReasonDTO; @Getter @AllArgsConstructor diff --git a/src/main/java/com/umc/dream/controller/FollowController.java b/src/main/java/com/umc/dream/controller/FollowController.java new file mode 100644 index 0000000..09557f4 --- /dev/null +++ b/src/main/java/com/umc/dream/controller/FollowController.java @@ -0,0 +1,39 @@ +package com.umc.dream.controller; + +import com.umc.dream.apiPayload.ApiResponse; +import com.umc.dream.converter.FollowConverter; +import com.umc.dream.domain.Follow; +import com.umc.dream.dto.FollowRequestDto; +import com.umc.dream.dto.FollowResponseDto; +import com.umc.dream.dto.ViewFollowResponseDto; +import com.umc.dream.service.FollowService; +import lombok.AllArgsConstructor; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@AllArgsConstructor +public class FollowController { + + private final FollowService followService; + + @PostMapping("/user/follow") + public ApiResponse follow(@RequestBody FollowRequestDto followRequestDto) { + Follow f = followService.follow(followRequestDto); + FollowResponseDto followResponseDto = FollowConverter.toFollowResponse(f); + return ApiResponse.onSuccess(followResponseDto); + } + + @DeleteMapping("/user/followCancel") + public ApiResponse followCancel(@RequestBody FollowRequestDto followRequestDto) { + followService.followCancel(followRequestDto); + return ApiResponse.onSuccess("팔로우 취소됨"); + } + + @GetMapping("/user/{user_id}/following") + public ApiResponse> viewFollowings(@PathVariable Long user_id) { + List viewFollowResponseDtos = followService.viewFollowings(user_id); + return ApiResponse.onSuccess(viewFollowResponseDtos); + } +} diff --git a/src/main/java/com/umc/dream/converter/FollowConverter.java b/src/main/java/com/umc/dream/converter/FollowConverter.java new file mode 100644 index 0000000..3fdd716 --- /dev/null +++ b/src/main/java/com/umc/dream/converter/FollowConverter.java @@ -0,0 +1,13 @@ +package com.umc.dream.converter; + +import com.umc.dream.domain.Follow; +import com.umc.dream.dto.FollowResponseDto; + +public class FollowConverter { + + public static FollowResponseDto toFollowResponse(Follow follow) { + return FollowResponseDto.builder() + .updatedAt(follow.getFollower().getLastModifiedDate()) + .build(); + } +} diff --git a/src/main/java/com/umc/dream/dto/FollowRequestDto.java b/src/main/java/com/umc/dream/dto/FollowRequestDto.java new file mode 100644 index 0000000..5b66afd --- /dev/null +++ b/src/main/java/com/umc/dream/dto/FollowRequestDto.java @@ -0,0 +1,19 @@ +package com.umc.dream.dto; + +import com.umc.dream.domain.Follow; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +public class FollowRequestDto { + private Long followerId; + private Long acceptorId; + + @Builder + public FollowRequestDto(Follow follow) { + this.followerId = follow.getFollower().getId(); + this.acceptorId = follow.getAcceptor().getId(); + } +} diff --git a/src/main/java/com/umc/dream/dto/FollowResponseDto.java b/src/main/java/com/umc/dream/dto/FollowResponseDto.java new file mode 100644 index 0000000..2aec5c7 --- /dev/null +++ b/src/main/java/com/umc/dream/dto/FollowResponseDto.java @@ -0,0 +1,16 @@ +package com.umc.dream.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.time.LocalDateTime; + +@Builder +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class FollowResponseDto { + private LocalDateTime updatedAt; +} diff --git a/src/main/java/com/umc/dream/dto/ViewFollowResponseDto.java b/src/main/java/com/umc/dream/dto/ViewFollowResponseDto.java new file mode 100644 index 0000000..f08f128 --- /dev/null +++ b/src/main/java/com/umc/dream/dto/ViewFollowResponseDto.java @@ -0,0 +1,15 @@ +package com.umc.dream.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Builder +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class ViewFollowResponseDto { + private Long userId; + private String name; +} diff --git a/src/main/java/com/umc/dream/repository/FollowRepository.java b/src/main/java/com/umc/dream/repository/FollowRepository.java new file mode 100644 index 0000000..fdb71fe --- /dev/null +++ b/src/main/java/com/umc/dream/repository/FollowRepository.java @@ -0,0 +1,13 @@ +package com.umc.dream.repository; + +import com.umc.dream.domain.Follow; +import com.umc.dream.domain.User; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface FollowRepository extends JpaRepository { + List findAllByFollower_Id(Long memberId); + + Follow findByFollowerAndAcceptor(User follower, User acceptor); +} diff --git a/src/main/java/com/umc/dream/repository/UserRepository.java b/src/main/java/com/umc/dream/repository/UserRepository.java new file mode 100644 index 0000000..a48b292 --- /dev/null +++ b/src/main/java/com/umc/dream/repository/UserRepository.java @@ -0,0 +1,7 @@ +package com.umc.dream.repository; + +import com.umc.dream.domain.User; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UserRepository extends JpaRepository { +} diff --git a/src/main/java/com/umc/dream/service/FollowService.java b/src/main/java/com/umc/dream/service/FollowService.java new file mode 100644 index 0000000..dffa4c2 --- /dev/null +++ b/src/main/java/com/umc/dream/service/FollowService.java @@ -0,0 +1,62 @@ +package com.umc.dream.service; + +import com.umc.dream.apiPayload.code.status.ErrorStatus; +import com.umc.dream.apiPayload.exception.GeneralException; +import com.umc.dream.domain.Follow; +import com.umc.dream.domain.User; +import com.umc.dream.dto.FollowRequestDto; +import com.umc.dream.dto.ViewFollowResponseDto; +import com.umc.dream.repository.FollowRepository; +import com.umc.dream.repository.UserRepository; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +@Service +@RequiredArgsConstructor +public class FollowService { + + private final FollowRepository followRepository; + private final UserRepository userRepository; + @Transactional + public Follow follow(FollowRequestDto followRequestDto) { + User follower = userRepository.findById(followRequestDto.getFollowerId()) + .orElseThrow(() -> new GeneralException(ErrorStatus._BAD_REQUEST)); + User acceptor = userRepository.findById(followRequestDto.getAcceptorId()) + .orElseThrow(() -> new GeneralException(ErrorStatus._BAD_REQUEST)); + Follow f = Follow.builder() + .follower(follower) + .acceptor(acceptor) + .build(); + followRepository.save(f); + return f; + } + + @Transactional + public void followCancel(FollowRequestDto followRequestDto) { + User follower = userRepository.findById(followRequestDto.getFollowerId()) + .orElseThrow(() -> new GeneralException(ErrorStatus._BAD_REQUEST)); + User acceptor = userRepository.findById(followRequestDto.getAcceptorId()) + .orElseThrow(() -> new GeneralException(ErrorStatus._BAD_REQUEST)); + Follow f = followRepository.findByFollowerAndAcceptor(follower,acceptor); + followRepository.delete(f); + } + + @Transactional + public List viewFollowings(Long memberId) { + List follows = followRepository.findAllByFollower_Id(memberId); + List viewFollowResponseDtos = new ArrayList<>(); + for(Follow follow : follows) { + User user = follow.getAcceptor(); + ViewFollowResponseDto viewFollowResponseDto = ViewFollowResponseDto.builder() + .userId(user.getId()) + .name(user.getName()) + .build(); + viewFollowResponseDtos.add(viewFollowResponseDto); + } + return viewFollowResponseDtos; + } +}