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

follow service, controller 구현 #8

Merged
merged 1 commit into from
Apr 29, 2024
Merged
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: 14 additions & 0 deletions src/main/java/backend/wedstagram/Converter/FollowConverter.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
80 changes: 80 additions & 0 deletions src/main/java/backend/wedstagram/controller/FollowController.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> unfollowUser(@PathVariable(value = "userName") String userName, @RequestBody String recieveUserName) {

followService.deleteFollow(userName, recieveUserName);
return ResponseEntity.status(HttpStatus.OK).body("언팔로우 완료");
}

// 내 팔로워 보기
@GetMapping("{userName}/followers")
public ResponseEntity<FollowResponseDto> getFollowers(@PathVariable(value = "userName") String userName) {

Member member = memberService.getMemberByUsername(userName);
List<FollowDto> followers = followService.getFollowers(member.getUserName());

FollowResponseDto followResponseDto = FollowResponseDto.builder()
.followList(followers)
.build();

return ResponseEntity.status(HttpStatus.OK).body(followResponseDto);
}

// 내 팔로잉 보기
@GetMapping("{userName}/followings")
public ResponseEntity<FollowResponseDto> getFollowings(@PathVariable(value = "userName") String userName) {

Member member = memberService.getMemberByUsername(userName);
List<FollowDto> followings = followService.getFollowings(member.getUserName());

FollowResponseDto followResponseDto = FollowResponseDto.builder()
.followList(followings)
.build();

return ResponseEntity.status(HttpStatus.OK).body(followResponseDto);
}

// 팔로워 삭제
@DeleteMapping("{userName}/followers")
public ResponseEntity<String> deleteFollowers(@PathVariable(value = "userName") String userName, @RequestBody String recieveUserName) {

followService.deleteFollow(recieveUserName, userName);
return ResponseEntity.status(HttpStatus.OK).body("팔로워 삭제 완료");
}
}
13 changes: 9 additions & 4 deletions src/main/java/backend/wedstagram/domain/Member.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -28,4 +27,10 @@ public class Member {
//S3 사용 예정
private String imageUrl;

@OneToMany(mappedBy = "follower")
private List<Follow> followers;

@OneToMany(mappedBy = "following")
private List<Follow> followings;

}
19 changes: 19 additions & 0 deletions src/main/java/backend/wedstagram/dto/FollowDto/FollowDto.java
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -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<FollowDto> followList;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,Long> {
Follow findByFollowerAndFollowing(String followerUserName, String followingUserName);

List<Follow> findAllByFollowingId(Long followingId);

Follow deleteFollowByFollowerAndFollowing(String followerUserName, String followingUserName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@

public interface MemberRepository extends JpaRepository<Member,Long> {
Optional<Member> findById(Long memberId);

Optional<Member> findByUserName(String UserName);
}
Original file line number Diff line number Diff line change
@@ -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<FollowDto> getFollowers(String userName);
public List<FollowDto> getFollowings(String userName);

}
Original file line number Diff line number Diff line change
@@ -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<FollowDto> getFollowers(String userName) {
Member member = memberRepository.findByUserName(userName).get();
List<Follow> followers = member.getFollowers();

List<FollowDto> 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<FollowDto> getFollowings(String userName) {
Member member = memberRepository.findByUserName(userName).get();
List<Follow> followings = member.getFollowings();

List<FollowDto> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
public interface MemberService {

Member getMemberById(Long memberId);

Member getMemberByUsername(String username);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package backend.wedstagram.service.MyPageService;

public interface MyPageService {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package backend.wedstagram.service.MyPageService;

public class MyPageServiceImpl {
}