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

[Hotfix/BE] 로그아웃 시 팔로워 수가 초기화 되는 현상을 수정한다 #675

Merged
merged 2 commits into from
Sep 26, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@

import com.woowacourse.f12.domain.inventoryproduct.InventoryProduct;
import com.woowacourse.f12.domain.inventoryproduct.InventoryProductRepository;
import com.woowacourse.f12.domain.member.CareerLevel;
import com.woowacourse.f12.domain.member.Following;
import com.woowacourse.f12.domain.member.FollowingRepository;
import com.woowacourse.f12.domain.member.JobType;
import com.woowacourse.f12.domain.member.Member;
import com.woowacourse.f12.domain.member.MemberRepository;
import com.woowacourse.f12.domain.member.*;
import com.woowacourse.f12.dto.request.member.MemberRequest;
import com.woowacourse.f12.dto.request.member.MemberSearchRequest;
import com.woowacourse.f12.dto.response.member.LoggedInMemberResponse;
Expand All @@ -19,15 +14,16 @@
import com.woowacourse.f12.exception.notfound.MemberNotFoundException;
import com.woowacourse.f12.presentation.member.CareerLevelConstant;
import com.woowacourse.f12.presentation.member.JobTypeConstant;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true)
public class MemberService {
Expand Down Expand Up @@ -136,7 +132,7 @@ private List<Long> extractMemberIds(final List<Member> members) {

@Transactional
public void follow(final Long followerId, final Long followingId) {
final Member followingMember = memberRepository.findById(followingId)
final Member targetMember = memberRepository.findById(followingId)
.orElseThrow(MemberNotFoundException::new);
validateFollowingMembersExist(followerId);
validateNotFollowing(followerId, followingId);
Expand All @@ -145,7 +141,7 @@ public void follow(final Long followerId, final Long followingId) {
.followingId(followingId)
.build();
followingRepository.save(following);
increaseFollowerCount(followingMember);
targetMember.follow();
}

private void validateFollowingMembersExist(final Long followerId) {
Expand All @@ -160,35 +156,20 @@ private void validateNotFollowing(final Long followerId, final Long followingId)
}
}

private void increaseFollowerCount(final Member followingMember) {
final Member updatedMember = Member.builder()
.followerCount(followingMember.getFollowerCount() + 1)
.build();
followingMember.update(updatedMember);
}

@Transactional
public void unfollow(final Long followerId, final Long followingId) {
final Member followingMember = memberRepository.findById(followingId)
final Member targetMember = memberRepository.findById(followingId)
.orElseThrow(MemberNotFoundException::new);
validateFollowingMembersExist(followerId);
final Following following = findFollowingRelation(followerId, followingId);
followingRepository.delete(following);
decreaseFollowerCount(followingMember);
targetMember.unfollow();
}

private Following findFollowingRelation(final Long followerId, final Long followingId) {
return followingRepository.findByFollowerIdAndFollowingId(followerId, followingId)
.orElseThrow(NotFollowingException::new);
}

private void decreaseFollowerCount(final Member followingMember) {
final Member updatedMember = Member.builder()
.followerCount(followingMember.getFollowerCount() - 1)
.build();
followingMember.update(updatedMember);
}

public MemberPageResponse findFollowingsByConditions(final Long loggedInId, final MemberSearchRequest memberSearchRequest,
final Pageable pageable) {
final Slice<Member> slice = findFollowingsBySearchConditions(loggedInId, memberSearchRequest, pageable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public void update(final Member updateMember) {
updateImageUrl(updateMember.imageUrl);
updateCareerLevel(updateMember.careerLevel);
updateJobType(updateMember.jobType);
updateFollowerCount(updateMember.followerCount);
updateRegistered(updateMember.registered);
}

Expand Down Expand Up @@ -98,11 +97,15 @@ private void updateJobType(final JobType jobType) {
}
}

private void updateFollowerCount(final int followerCount) {
if (followerCount < 0){
public void follow() {
this.followerCount += 1;
}

public void unfollow() {
if (this.followerCount == 0) {
throw new InvalidFollowerCountException();
}
this.followerCount = followerCount;
this.followerCount -= 1;
}

private void updateRegistered(final boolean registered) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
package com.woowacourse.f12.acceptance;

import static com.woowacourse.f12.acceptance.support.RestAssuredRequestUtil.GET_요청을_보낸다;
import static com.woowacourse.f12.acceptance.support.RestAssuredRequestUtil.로그인된_상태로_DELETE_요청을_보낸다;
import static com.woowacourse.f12.acceptance.support.RestAssuredRequestUtil.로그인된_상태로_GET_요청을_보낸다;
import static com.woowacourse.f12.acceptance.support.RestAssuredRequestUtil.로그인된_상태로_PATCH_요청을_보낸다;
import static com.woowacourse.f12.acceptance.support.RestAssuredRequestUtil.로그인된_상태로_POST_요청을_보낸다;
import static com.woowacourse.f12.domain.member.CareerLevel.JUNIOR;
import static com.woowacourse.f12.domain.member.CareerLevel.SENIOR;
import static com.woowacourse.f12.domain.member.JobType.BACKEND;
import static com.woowacourse.f12.presentation.member.CareerLevelConstant.JUNIOR_CONSTANT;
import static com.woowacourse.f12.presentation.member.CareerLevelConstant.SENIOR_CONSTANT;
import static com.woowacourse.f12.presentation.member.JobTypeConstant.BACKEND_CONSTANT;
import static com.woowacourse.f12.support.fixture.AcceptanceFixture.민초;
import static com.woowacourse.f12.support.fixture.AcceptanceFixture.오찌;
import static com.woowacourse.f12.support.fixture.AcceptanceFixture.코린;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

import com.woowacourse.f12.domain.inventoryproduct.InventoryProduct;
import com.woowacourse.f12.domain.member.Member;
import com.woowacourse.f12.domain.product.Product;
Expand All @@ -26,6 +9,7 @@
import com.woowacourse.f12.dto.response.auth.LoginResponse;
import com.woowacourse.f12.dto.response.inventoryproduct.InventoryProductResponse;
import com.woowacourse.f12.dto.response.inventoryproduct.InventoryProductsResponse;
import com.woowacourse.f12.dto.response.member.LoggedInMemberResponse;
import com.woowacourse.f12.dto.response.member.MemberPageResponse;
import com.woowacourse.f12.dto.response.member.MemberResponse;
import com.woowacourse.f12.dto.response.member.MemberWithProfileProductResponse;
Expand All @@ -34,11 +18,23 @@
import com.woowacourse.f12.support.fixture.ReviewFixture;
import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;

import java.util.List;

import static com.woowacourse.f12.acceptance.support.RestAssuredRequestUtil.*;
import static com.woowacourse.f12.domain.member.CareerLevel.JUNIOR;
import static com.woowacourse.f12.domain.member.CareerLevel.SENIOR;
import static com.woowacourse.f12.domain.member.JobType.BACKEND;
import static com.woowacourse.f12.presentation.member.CareerLevelConstant.JUNIOR_CONSTANT;
import static com.woowacourse.f12.presentation.member.CareerLevelConstant.SENIOR_CONSTANT;
import static com.woowacourse.f12.presentation.member.JobTypeConstant.BACKEND_CONSTANT;
import static com.woowacourse.f12.support.fixture.AcceptanceFixture.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

class MemberAcceptanceTest extends AcceptanceTest {

@Autowired
Expand Down Expand Up @@ -640,6 +636,27 @@ class MemberAcceptanceTest extends AcceptanceTest {
);
}

@Test
void 재로그인_시_팔로워_수가_변하지_않는다() {
// given
MemberRequest memberRequest = new MemberRequest(SENIOR_CONSTANT, BACKEND_CONSTANT);
LoginResponse followingResponse = 민초.로그인을_한다();
민초.로그인한_상태로(followingResponse.getToken()).추가정보를_입력한다(memberRequest);

final LoginResponse followerResponse = 코린.로그인을_한다();
코린.로그인한_상태로(followerResponse.getToken()).추가정보를_입력한다(memberRequest);
코린.로그인한_상태로(followerResponse.getToken()).팔로우한다(followingResponse.getMember().getId());

final LoginResponse reLoggedInFollowingResponse = 민초.로그인을_한다();

// when
final LoggedInMemberResponse followingMemberResponse = 민초.로그인한_상태로(reLoggedInFollowingResponse.getToken()).자신의_프로필을_조회한다()
.as(LoggedInMemberResponse.class);

// then
assertThat(followingMemberResponse.getFollowerCount()).isOne();
}

private Product 제품을_저장한다(Product product) {
return productRepository.save(product);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ class MemberRepositoryTest {
.followerId(corinne.getId())
.followingId(mincho.getId())
.build());
Member updateMember = Member.builder()
.followerCount(mincho.getFollowerCount() + 1)
.build();
mincho.update(updateMember);
mincho.follow();
entityManager.flush();
entityManager.clear();

Expand Down Expand Up @@ -176,9 +173,7 @@ class MemberRepositoryTest {
.followerId(corinne.getId())
.followingId(mincho.getId())
.build());
mincho.update(Member.builder()
.followerCount(mincho.getFollowerCount() + 1)
.build());
mincho.follow();
entityManager.flush();
entityManager.clear();

Expand Down Expand Up @@ -216,9 +211,7 @@ class MemberRepositoryTest {
.followerId(corinne.getId())
.followingId(mincho.getId())
.build());
mincho.update(Member.builder()
.followerCount(mincho.getFollowerCount() + 1)
.build());
mincho.follow();
entityManager.flush();
entityManager.clear();

Expand Down Expand Up @@ -256,10 +249,7 @@ class MemberRepositoryTest {
.followerId(corinne.getId())
.followingId(mincho.getId())
.build());
Member updateMember = Member.builder()
.followerCount(mincho.getFollowerCount() + 1)
.build();
mincho.update(updateMember);
mincho.follow();
entityManager.flush();
entityManager.clear();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.woowacourse.f12.domain.member;

import com.woowacourse.f12.exception.badrequest.InvalidFollowerCountException;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class MemberTest {

Expand Down Expand Up @@ -55,4 +57,74 @@ class MemberTest {
assertThat(member).usingRecursiveComparison()
.isEqualTo(expected);
}

@Test
void 팔로워_수를_증가한다() {
// given
Member member = Member.builder()
.id(1L)
.name("유현지")
.imageUrl("imageUrl")
.careerLevel(CareerLevel.SENIOR)
.jobType(JobType.BACKEND)
.build();
Member expected = Member.builder()
.id(1L)
.name("유현지")
.imageUrl("imageUrl")
.careerLevel(CareerLevel.SENIOR)
.jobType(JobType.BACKEND)
.followerCount(1)
.build();

// when
member.follow();

// then
assertThat(member).usingRecursiveComparison()
.isEqualTo(expected);
}

@Test
void 팔로워_수를_감소한다() {
// given
Member member = Member.builder()
.id(1L)
.name("유현지")
.imageUrl("imageUrl")
.careerLevel(CareerLevel.SENIOR)
.jobType(JobType.BACKEND)
.followerCount(1)
.build();
Member expected = Member.builder()
.id(1L)
.name("유현지")
.imageUrl("imageUrl")
.careerLevel(CareerLevel.SENIOR)
.jobType(JobType.BACKEND)
.build();

// when
member.unfollow();

// then
assertThat(member).usingRecursiveComparison()
.isEqualTo(expected);
}

@Test
void 팔로워_수가_0인_상태에서_팔로워_수를_감소하면_예외가_발생한다() {
// given
Member member = Member.builder()
.id(1L)
.name("유현지")
.imageUrl("imageUrl")
.careerLevel(CareerLevel.SENIOR)
.jobType(JobType.BACKEND)
.build();

// when, then
assertThatThrownBy(member::unfollow)
.isInstanceOf(InvalidFollowerCountException.class);
}
}