From 860a707f5b4fd8cf3921c17b2b9fa6386e8f4e52 Mon Sep 17 00:00:00 2001 From: Jeongyeon Park Date: Mon, 29 Apr 2024 23:50:30 +0900 Subject: [PATCH] =?UTF-8?q?follow=20service,=20controller=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wedstagram/Converter/FollowConverter.java | 14 ++++ .../controller/FollowController.java | 80 +++++++++++++++++++ .../backend/wedstagram/domain/Member.java | 13 ++- .../wedstagram/dto/FollowDto/FollowDto.java | 19 +++++ .../dto/FollowDto/FollowResponseDto.java | 16 ++++ .../repository/FollowRepository.java | 8 ++ .../repository/MemberRepository.java | 2 + .../service/FollowService/FollowService.java | 16 ++++ .../FollowService/FollowServiceImpl.java | 67 ++++++++++++++++ .../service/MemberService/MemberService.java | 2 + .../MemberService/MemberServiceImpl.java | 5 ++ .../service/MyPageService/MyPageService.java | 4 + .../MyPageService/MyPageServiceImpl.java | 4 + 13 files changed, 246 insertions(+), 4 deletions(-) create mode 100644 src/main/java/backend/wedstagram/Converter/FollowConverter.java create mode 100644 src/main/java/backend/wedstagram/controller/FollowController.java create mode 100644 src/main/java/backend/wedstagram/dto/FollowDto/FollowDto.java create mode 100644 src/main/java/backend/wedstagram/dto/FollowDto/FollowResponseDto.java create mode 100644 src/main/java/backend/wedstagram/service/FollowService/FollowService.java create mode 100644 src/main/java/backend/wedstagram/service/FollowService/FollowServiceImpl.java create mode 100644 src/main/java/backend/wedstagram/service/MyPageService/MyPageService.java create mode 100644 src/main/java/backend/wedstagram/service/MyPageService/MyPageServiceImpl.java diff --git a/src/main/java/backend/wedstagram/Converter/FollowConverter.java b/src/main/java/backend/wedstagram/Converter/FollowConverter.java new file mode 100644 index 0000000..e8262f3 --- /dev/null +++ b/src/main/java/backend/wedstagram/Converter/FollowConverter.java @@ -0,0 +1,14 @@ +package backend.wedstagram.Converter; + +import backend.wedstagram.domain.Follow; +import backend.wedstagram.domain.Member; + +public class FollowConverter { + + public static Follow toFollow(Member follower, Member following){ + return Follow.builder() + .follower(follower) + .following(following) + .build(); + } +} diff --git a/src/main/java/backend/wedstagram/controller/FollowController.java b/src/main/java/backend/wedstagram/controller/FollowController.java new file mode 100644 index 0000000..3588279 --- /dev/null +++ b/src/main/java/backend/wedstagram/controller/FollowController.java @@ -0,0 +1,80 @@ +package backend.wedstagram.controller; + +import backend.wedstagram.Converter.FollowConverter; +import backend.wedstagram.domain.Follow; +import backend.wedstagram.domain.Member; +import backend.wedstagram.dto.FollowDto.FollowDto; +import backend.wedstagram.dto.FollowDto.FollowResponseDto; +import backend.wedstagram.service.FollowService.FollowService; +import backend.wedstagram.service.MemberService.MemberService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequiredArgsConstructor +public class FollowController { + + private final FollowService followService; + private final MemberService memberService; + + // 팔로우하기 + @PostMapping("{userName}/follow") + public ResponseEntity followUser(@PathVariable(value = "userName") String userName, @RequestBody String recieveUserName) { + + Member follower = memberService.getMemberByUsername(userName); + Member Following = memberService.getMemberByUsername(recieveUserName); + + Follow follow = FollowConverter.toFollow(follower, Following); + + followService.saveFollow(follow); + return ResponseEntity.status(HttpStatus.OK).body("팔로우 완료"); + } + + // 언팔로우하기 + @DeleteMapping("{userName}/unfollow") + public ResponseEntity unfollowUser(@PathVariable(value = "userName") String userName, @RequestBody String recieveUserName) { + + followService.deleteFollow(userName, recieveUserName); + return ResponseEntity.status(HttpStatus.OK).body("언팔로우 완료"); + } + + // 내 팔로워 보기 + @GetMapping("{userName}/followers") + public ResponseEntity getFollowers(@PathVariable(value = "userName") String userName) { + + Member member = memberService.getMemberByUsername(userName); + List followers = followService.getFollowers(member.getUserName()); + + FollowResponseDto followResponseDto = FollowResponseDto.builder() + .followList(followers) + .build(); + + return ResponseEntity.status(HttpStatus.OK).body(followResponseDto); + } + + // 내 팔로잉 보기 + @GetMapping("{userName}/followings") + public ResponseEntity getFollowings(@PathVariable(value = "userName") String userName) { + + Member member = memberService.getMemberByUsername(userName); + List followings = followService.getFollowings(member.getUserName()); + + FollowResponseDto followResponseDto = FollowResponseDto.builder() + .followList(followings) + .build(); + + return ResponseEntity.status(HttpStatus.OK).body(followResponseDto); + } + + // 팔로워 삭제 + @DeleteMapping("{userName}/followers") + public ResponseEntity deleteFollowers(@PathVariable(value = "userName") String userName, @RequestBody String recieveUserName) { + + followService.deleteFollow(recieveUserName, userName); + return ResponseEntity.status(HttpStatus.OK).body("팔로워 삭제 완료"); + } +} diff --git a/src/main/java/backend/wedstagram/domain/Member.java b/src/main/java/backend/wedstagram/domain/Member.java index 11671bb..1c3f791 100644 --- a/src/main/java/backend/wedstagram/domain/Member.java +++ b/src/main/java/backend/wedstagram/domain/Member.java @@ -1,14 +1,13 @@ package backend.wedstagram.domain; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; +import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import java.util.List; + @Entity @Getter @Builder @@ -28,4 +27,10 @@ public class Member { //S3 사용 예정 private String imageUrl; + @OneToMany(mappedBy = "follower") + private List followers; + + @OneToMany(mappedBy = "following") + private List followings; + } diff --git a/src/main/java/backend/wedstagram/dto/FollowDto/FollowDto.java b/src/main/java/backend/wedstagram/dto/FollowDto/FollowDto.java new file mode 100644 index 0000000..8ee7866 --- /dev/null +++ b/src/main/java/backend/wedstagram/dto/FollowDto/FollowDto.java @@ -0,0 +1,19 @@ +package backend.wedstagram.dto.FollowDto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Builder +@Data +@AllArgsConstructor +@NoArgsConstructor +public class FollowDto { + + private Long id; + private String userName; + private String name; + private String profileImage; + +} diff --git a/src/main/java/backend/wedstagram/dto/FollowDto/FollowResponseDto.java b/src/main/java/backend/wedstagram/dto/FollowDto/FollowResponseDto.java new file mode 100644 index 0000000..f42a20e --- /dev/null +++ b/src/main/java/backend/wedstagram/dto/FollowDto/FollowResponseDto.java @@ -0,0 +1,16 @@ +package backend.wedstagram.dto.FollowDto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Builder +@Data +@AllArgsConstructor +@NoArgsConstructor +public class FollowResponseDto { + private List followList; +} diff --git a/src/main/java/backend/wedstagram/repository/FollowRepository.java b/src/main/java/backend/wedstagram/repository/FollowRepository.java index c0b479c..2275e78 100644 --- a/src/main/java/backend/wedstagram/repository/FollowRepository.java +++ b/src/main/java/backend/wedstagram/repository/FollowRepository.java @@ -3,5 +3,13 @@ import backend.wedstagram.domain.Follow; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; +import java.util.Optional; + public interface FollowRepository extends JpaRepository { + Follow findByFollowerAndFollowing(String followerUserName, String followingUserName); + + List findAllByFollowingId(Long followingId); + + Follow deleteFollowByFollowerAndFollowing(String followerUserName, String followingUserName); } diff --git a/src/main/java/backend/wedstagram/repository/MemberRepository.java b/src/main/java/backend/wedstagram/repository/MemberRepository.java index 395d289..e557942 100644 --- a/src/main/java/backend/wedstagram/repository/MemberRepository.java +++ b/src/main/java/backend/wedstagram/repository/MemberRepository.java @@ -7,4 +7,6 @@ public interface MemberRepository extends JpaRepository { Optional findById(Long memberId); + + Optional findByUserName(String UserName); } diff --git a/src/main/java/backend/wedstagram/service/FollowService/FollowService.java b/src/main/java/backend/wedstagram/service/FollowService/FollowService.java new file mode 100644 index 0000000..46ae66f --- /dev/null +++ b/src/main/java/backend/wedstagram/service/FollowService/FollowService.java @@ -0,0 +1,16 @@ +package backend.wedstagram.service.FollowService; + +import backend.wedstagram.domain.Follow; +import backend.wedstagram.dto.FollowDto.FollowDto; + +import java.util.List; + +public interface FollowService { + + public void saveFollow(Follow follow); + public void deleteFollow(String followerUserName, String followingUserName); + + public List getFollowers(String userName); + public List getFollowings(String userName); + +} diff --git a/src/main/java/backend/wedstagram/service/FollowService/FollowServiceImpl.java b/src/main/java/backend/wedstagram/service/FollowService/FollowServiceImpl.java new file mode 100644 index 0000000..c9cd9f6 --- /dev/null +++ b/src/main/java/backend/wedstagram/service/FollowService/FollowServiceImpl.java @@ -0,0 +1,67 @@ +package backend.wedstagram.service.FollowService; + +import backend.wedstagram.domain.Follow; +import backend.wedstagram.domain.Member; +import backend.wedstagram.dto.FollowDto.FollowDto; +import backend.wedstagram.repository.FollowRepository; +import backend.wedstagram.repository.MemberRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +@Service +@RequiredArgsConstructor +public class FollowServiceImpl implements FollowService { + + private final FollowRepository followRepository; + private final MemberRepository memberRepository; + + @Override + public void saveFollow(Follow follow) { + followRepository.save(follow); + } + + @Override + public void deleteFollow(String followerUserName, String followingUserName) { + followRepository.deleteFollowByFollowerAndFollowing(followerUserName, followingUserName); + } + + @Override + public void findFollowByFollowerAndFollowing(String followerUserName, String followingUserName) { + followRepository.findByFollowerAndFollowing(followerUserName, followingUserName); + } + + @Override + public List getFollowers(String userName) { + Member member = memberRepository.findByUserName(userName).get(); + List followers = member.getFollowers(); + + List collectFollowDtos = followers.stream().map(Follow::getFollower).map(follower -> FollowDto.builder() + .id(follower.getId()) + .userName(follower.getUserName()) + .name(follower.getName()) + .profileImage(follower.getImageUrl()) + .build()).collect(Collectors.toList()); + + return collectFollowDtos; + } + + @Override + public List getFollowings(String userName) { + Member member = memberRepository.findByUserName(userName).get(); + List followings = member.getFollowings(); + + List collectFollowDtos = followings.stream().map(Follow::getFollowing).map(following -> FollowDto.builder() + .id(following.getId()) + .userName(following.getUserName()) + .name(following.getName()) + .profileImage(following.getImageUrl()) + .build()).collect(Collectors.toList()); + + return collectFollowDtos; + } +} diff --git a/src/main/java/backend/wedstagram/service/MemberService/MemberService.java b/src/main/java/backend/wedstagram/service/MemberService/MemberService.java index ad2dc73..7fef8a0 100644 --- a/src/main/java/backend/wedstagram/service/MemberService/MemberService.java +++ b/src/main/java/backend/wedstagram/service/MemberService/MemberService.java @@ -5,4 +5,6 @@ public interface MemberService { Member getMemberById(Long memberId); + + Member getMemberByUsername(String username); } diff --git a/src/main/java/backend/wedstagram/service/MemberService/MemberServiceImpl.java b/src/main/java/backend/wedstagram/service/MemberService/MemberServiceImpl.java index c32978a..37cdeb6 100644 --- a/src/main/java/backend/wedstagram/service/MemberService/MemberServiceImpl.java +++ b/src/main/java/backend/wedstagram/service/MemberService/MemberServiceImpl.java @@ -16,4 +16,9 @@ public Member getMemberById(Long memberId){ return memberRepository.findById(memberId).orElseThrow(); } + @Override + public Member getMemberByUsername(String username) { + return memberRepository.findByUserName(username).orElseThrow(); + } + } diff --git a/src/main/java/backend/wedstagram/service/MyPageService/MyPageService.java b/src/main/java/backend/wedstagram/service/MyPageService/MyPageService.java new file mode 100644 index 0000000..9a44b77 --- /dev/null +++ b/src/main/java/backend/wedstagram/service/MyPageService/MyPageService.java @@ -0,0 +1,4 @@ +package backend.wedstagram.service.MyPageService; + +public interface MyPageService { +} diff --git a/src/main/java/backend/wedstagram/service/MyPageService/MyPageServiceImpl.java b/src/main/java/backend/wedstagram/service/MyPageService/MyPageServiceImpl.java new file mode 100644 index 0000000..dbfaf8c --- /dev/null +++ b/src/main/java/backend/wedstagram/service/MyPageService/MyPageServiceImpl.java @@ -0,0 +1,4 @@ +package backend.wedstagram.service.MyPageService; + +public class MyPageServiceImpl { +}