Skip to content

Commit

Permalink
내집다방 API 구현ing
Browse files Browse the repository at this point in the history
  • Loading branch information
HyoBN committed Oct 4, 2023
2 parents 16ca46c + 541f9da commit 712ba1d
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 55 deletions.
21 changes: 11 additions & 10 deletions src/main/java/zipdabang/server/converter/MemberConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -341,15 +341,16 @@ public static Follow toFollow(){
return Follow.builder().build();
}

public static MemberResponseDto.FollowingResultDto toFollowingResultDto(Follow follow){
public static MemberResponseDto.FollowingResultDto toFollowingResultDto(Follow follow, Member member, Long targetId){

return MemberResponseDto.FollowingResultDto.builder()
.targetId(follow.getTargetMember().getMemberId())
.followAt(LocalDateTime.now())
.targetId(targetId)
.isFollowing(follow != null)
.build();
}

public static MemberResponseDto.FollowInfoDto toFollowInfoDto(Member member){
return MemberResponseDto.FollowInfoDto.builder()
public static MemberResponseDto.FollowingInfoDto toFollowInfoDto(Member member){
return MemberResponseDto.FollowingInfoDto.builder()
.id(member.getMemberId())
.caption(member.getCaption())
.nickname(member.getNickname())
Expand All @@ -359,8 +360,8 @@ public static MemberResponseDto.FollowInfoDto toFollowInfoDto(Member member){

public static MemberResponseDto.FollowerInfoDto toFollowerInfoDto(Member member, Member owner){

List<Member> followingMembers = owner.getFollowingList().stream()
.map(Follow::getTargetMember).collect(Collectors.toList());
List<Member> followingMembers = owner.getMyFollowingList().stream()
.map(Follow::getFollowee).collect(Collectors.toList());

return MemberResponseDto.FollowerInfoDto.builder()
.id(member.getMemberId())
Expand All @@ -372,8 +373,8 @@ public static MemberResponseDto.FollowerInfoDto toFollowerInfoDto(Member member,
}

public static MemberResponseDto.FollowingListDto toFollowingListDto(Page<Follow> followList){
List<MemberResponseDto.FollowInfoDto> followInfoDtoList = followList.stream()
.map(follow -> toFollowInfoDto(follow.getTargetMember())).collect(Collectors.toList());
List<MemberResponseDto.FollowingInfoDto> followInfoDtoList = followList.stream()
.map(follow -> toFollowInfoDto(follow.getFollowee())).collect(Collectors.toList());

return MemberResponseDto.FollowingListDto.builder()
.followingList(followInfoDtoList)
Expand All @@ -387,7 +388,7 @@ public static MemberResponseDto.FollowingListDto toFollowingListDto(Page<Follow>

public static MemberResponseDto.FollowerListDto toFollowerListDto(Page<Follow> followList, Member owner){
List<MemberResponseDto.FollowerInfoDto> followerInfoDtoList = followList.stream()
.map(follow -> toFollowerInfoDto(follow.getFollowingMember(), owner)).collect(Collectors.toList());
.map(follow -> toFollowerInfoDto(follow.getFollower(), owner)).collect(Collectors.toList());

return MemberResponseDto.FollowerListDto.builder()
.followerList(followerInfoDtoList)
Expand Down
41 changes: 24 additions & 17 deletions src/main/java/zipdabang/server/domain/member/Follow.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,35 @@ public class Follow extends BaseEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

// follow 객체
// 팔로우 당하는 대상
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "target_id")
private Member targetMember;
@JoinColumn(name = "followee_id")
private Member followee;

// follow 주체
// 팔로우 하는 놈
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "following_id")
private Member followingMember;

public void setTargetMember(Member targetMember){
if (this.targetMember != null)
targetMember.getFollowerList().remove(this);
this.targetMember = targetMember;
targetMember.getFollowerList().add(this);
@JoinColumn(name = "follower_id")
private Member follower;

// 팔로우 하는놈 세팅
public void setFollower(Member follower){
if (this.follower != null)
follower.getMyFollowingList().remove(this);
this.follower = follower;
follower.getMyFollowingList().add(this);
}

public void setFollowingMember(Member followingMember){
if (this.followingMember != null)
followingMember.getFollowerList().remove(this);
this.followingMember = followingMember;
followingMember.getFollowerList().add(this);
public void setFollowee(Member followee){
if(this.followee != null)
followee.getMyFollowerList().remove(this);
this.followee = followee;
followee.getMyFollowerList().add(this);
}

public void cancleFollow(Member followee, Member follower){
this.follower = null;
this.followee = null;
followee.getMyFollowerList().remove(this);
follower.getMyFollowingList().remove(this);
}
}
16 changes: 7 additions & 9 deletions src/main/java/zipdabang/server/domain/member/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ public class Member extends BaseEntity {

private String detailAddress;

private Long followers;
private Long followings;

@Enumerated(EnumType.STRING)
private SocialType socialType;

Expand Down Expand Up @@ -109,13 +106,14 @@ public class Member extends BaseEntity {
@OneToMany(mappedBy = "member", cascade = CascadeType.ALL)
private List<Inquery> inqueryList;

// 나를 따르는 = follow 테이블에서 targetMember가 나인,
@OneToMany(mappedBy = "targetMember", cascade = CascadeType.ALL)
private List<Follow> followerList;

// 내가 따르는 = follow 테이블에서 followingMember가 나인,
@OneToMany(mappedBy = "followingMember", cascade = CascadeType.ALL)
private List<Follow> followingList;
// 나를 따르는 놈들
@OneToMany(mappedBy = "followee", cascade = CascadeType.ALL)
private List<Follow> myFollowerList;

// 내가 따르는 놈들
@OneToMany(mappedBy = "follower", cascade = CascadeType.ALL)
private List<Follow> myFollowingList;

public void setProfileUrl(String profileUrl) {
this.profileUrl = profileUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
import zipdabang.server.domain.member.Follow;
import zipdabang.server.domain.member.Member;

import java.util.Optional;

public interface FollowRepository extends JpaRepository<Follow, Long> {

Page<Follow> findAllByTargetMember(Member member, PageRequest pageRequest);
Page<Follow> findAllByFollowingMember(Member member, PageRequest pageRequest);
Page<Follow> findAllByFollowee(Member member, PageRequest pageRequest);
Page<Follow> findAllByFollower(Member member, PageRequest pageRequest);

Optional<Follow> findByFollowerAndFollowee(Member follower, Member followee);
}
2 changes: 1 addition & 1 deletion src/main/java/zipdabang/server/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public interface MemberService {
public void unblockMember(Member owner, Long blockedId);
public Page<Member> findBlockedMember(Integer page, Member member);

public Follow createFollow(Long targetId, Member member);
public Follow toggleFollow(Long targetId, Member member);

Page<Follow> findFollowing(Member member, Integer page);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,20 +378,33 @@ public Page<Member> findBlockedMember(Integer page, Member member) {

@Override
@Transactional
public Follow createFollow(Long targetId, Member member) {

public Follow toggleFollow(Long targetId, Member member) {
if(targetId.equals(member.getMemberId()))
throw new MemberException(Code.SELF_FOLLOW_FORBIDDEN);
Follow follow = MemberConverter.toFollow();
follow.setFollowingMember(member);
follow.setTargetMember(memberRepository.findById(targetId).get());
return followRepository.save(follow);
Member target = memberRepository.findById(targetId).get();

Optional<Follow> checkFollow = followRepository.findByFollowerAndFollowee(member, target);

// 팔로우 하기
if (checkFollow.isEmpty()) {
Follow follow = MemberConverter.toFollow();
// 내 팔로우 대상에게 팔로워 목록 추가
follow.setFollower(member);
// 내 팔로잉 목록에 해당 멤버를 넣기
follow.setFollowee(target);
return follow;
}else{
// 팔로우 끊기
checkFollow.get().cancleFollow(target,member);
followRepository.delete(checkFollow.get());
return null;
}
}

@Override
public Page<Follow> findFollowing(Member member, Integer page) {
page -= 1;
Page<Follow> followingMember = followRepository.findAllByFollowingMember(member, PageRequest.of(page, pageSize, Sort.by(Sort.Direction.DESC, "createdAt")));
Page<Follow> followingMember = followRepository.findAllByFollower(member, PageRequest.of(page, pageSize, Sort.by(Sort.Direction.DESC, "createdAt")));

if(followingMember.getTotalPages() <= page)
throw new MemberException(Code.OVER_PAGE_INDEX_ERROR);
Expand All @@ -402,7 +415,7 @@ public Page<Follow> findFollowing(Member member, Integer page) {
@Override
public Page<Follow> findFollower(Member member, Integer page) {
page -= 1;
Page<Follow> followerMember = followRepository.findAllByTargetMember(member, PageRequest.of(page, pageSize, Sort.by(Sort.Direction.DESC, "createdAt")));
Page<Follow> followerMember = followRepository.findAllByFollowee(member, PageRequest.of(page, pageSize, Sort.by(Sort.Direction.DESC, "createdAt")));

if(followerMember.getTotalPages() <= page)
throw new MemberException(Code.OVER_PAGE_INDEX_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import zipdabang.server.base.Code;
import zipdabang.server.base.ResponseDto;
import zipdabang.server.base.exception.handler.MemberException;
import zipdabang.server.base.exception.handler.RecipeException;
import zipdabang.server.converter.MemberConverter;
import zipdabang.server.domain.Category;
import zipdabang.server.domain.member.Follow;
Expand Down Expand Up @@ -416,7 +415,7 @@ else if (page < 1)



@Operation(summary = "🎪팔로우하기 API", description = "팔로우하기 API 입니다.")
@Operation(summary = "🎪팔로우하기/취소하기 API", description = "팔로우하기 API 입니다.")
@PostMapping("/members/followings/{targetId}")
@Parameters({
@Parameter(name = "member", hidden = true)
Expand All @@ -427,8 +426,8 @@ else if (page < 1)
@ApiResponse(responseCode = "4065", description = "FORBIDDEN , 스스로는 팔로우가 안됩니다", content = @Content(schema = @Schema(implementation = ResponseDto.class))),
})
public ResponseDto<MemberResponseDto.FollowingResultDto> followMember(@CheckTempMember @AuthMember Member member, @ExistMember @PathVariable(name = "targetId") Long targetId){
Follow follow = memberService.createFollow(targetId, member);
return ResponseDto.of(MemberConverter.toFollowingResultDto(follow));
Follow follow = memberService.toggleFollow(targetId, member);
return ResponseDto.of(MemberConverter.toFollowingResultDto(follow,member, targetId));
}

@Operation(summary = "🎪팔로우중인 사용자 조회 API", description = "팔로우중인 사용자 조회 API 입니다. 페이지 주세요")
Expand Down Expand Up @@ -490,4 +489,5 @@ public ResponseDto<MemberResponseDto.MemberStatusDto> updateProfileDefault(@Auth
return ResponseDto.of(MemberConverter.toMemberStatusDto(member.getMemberId(), "UpdateProfileDefault"));
}

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,14 @@ public static class PagingMemberListDto {
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public static class FollowingResultDto{
Long targetId;
LocalDateTime followAt;
Boolean isFollowing;
}

@Builder
@Getter
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public static class FollowInfoDto{
public static class FollowingInfoDto{
Long id;
String nickname;
String imageUrl;
Expand All @@ -248,7 +248,7 @@ public static class FollowerInfoDto{
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public static class FollowingListDto{
List<FollowInfoDto> followingList;
List<FollowingInfoDto> followingList;
Long totalElements;
Integer currentPageElements;
Integer totalPage;
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ spring:
## # local redis
# redis:
# host: localhost
# redis:
# briefing-redis.bjyb5r.ng.0001.apne1.cache.amazonaws.com

redis:
host: zipdabang-redis.osattk.ng.0001.apn2.cache.amazonaws.com
Expand Down

0 comments on commit 712ba1d

Please sign in to comment.