Skip to content

Commit

Permalink
#120 feat: 회원 동네 삭제시 남은 회원 동네에 대하여 선택 처리 (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
yonghwankim-dev authored Sep 30, 2023
1 parent ce8045c commit 8428311
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public enum MemberTownErrorCode implements ErrorCode {
ALREADY_ADDRESS_NAME(HttpStatus.CONFLICT, "이미 존재하는 동네입니다."),
UNREGISTERED_ADDRESS_TO_REMOVE(HttpStatus.BAD_REQUEST, "등록되지 않은 동네를 삭제할 수 없습니다."),
NOT_SELECT_UNREGISTERED_MEMBER_TOWN(HttpStatus.BAD_REQUEST, "회원이 등록한 동네만 선택할 수 있습니다."),
FAIL_REMOVE_ADDRESS(HttpStatus.INTERNAL_SERVER_ERROR, "동네 삭제를 실패하였습니다.");
FAIL_REMOVE_ADDRESS(HttpStatus.INTERNAL_SERVER_ERROR, "동네 삭제를 실패하였습니다."),
NOT_FOUND_MEMBER_TOWN(HttpStatus.NOT_FOUND, "회원 동네를 찾을 수 없습니다.");

private final HttpStatus httpStatus;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Transactional
@Transactional(readOnly = true)
@RequiredArgsConstructor
@Service
public class MemberTownService {
Expand All @@ -35,6 +35,7 @@ public class MemberTownService {
private final RegionRepository regionRepository;
private final MemberTownValidator memberTownValidator;

@Transactional
public MemberAddRegionResponse addMemberTown(Principal principal, MemberTownAddRequest request) {
log.info("회원 동네 추가 서비스 요청 : 회원아이디={}, 추가할 동네 등록번호={}", principal.getLoginId(), request.getAddressId());

Expand All @@ -49,6 +50,7 @@ public MemberAddRegionResponse addMemberTown(Principal principal, MemberTownAddR
return MemberAddRegionResponse.from(town);
}

@Transactional
public MemberTownRemoveResponse removeMemberTown(Principal principal, MemberTownRemoveRequest request) {
log.info("회원 동네 삭제 서비스 요청 : 회원아이디={}, 삭제할 동네 등록번호={}", principal.getLoginId(), request.getAddressId());

Expand All @@ -59,11 +61,18 @@ public MemberTownRemoveResponse removeMemberTown(Principal principal, MemberTown
Member member = findMemberBy(principal);
memberTownRepository.deleteMemberTownByMemberIdAndRegionId(member.getId(), region.getId());

// TODO: 남은 회원 동네 선택 처리
changeIsSelectedWithRemainMemberTown(principal);

return MemberTownRemoveResponse.create(region.getName());
}

private void changeIsSelectedWithRemainMemberTown(Principal principal) {
MemberTown remainMemberTown = memberTownRepository.findAllByMemberId(principal.getMemberId()).stream()
.findAny()
.orElseThrow(() -> new RestApiException(MemberTownErrorCode.NOT_FOUND_MEMBER_TOWN));
remainMemberTown.changeIsSelected(true);
}

private Region getAddressIdBy(Long addressId) {
return regionRepository.findById(addressId)
.orElseThrow(() -> new RestApiException(RegionErrorCode.NOT_FOUND_REGION));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public static MemberTown notSelectedMemberTown(Region region, Member member) {
return new MemberTown(region.getShortAddress(), member, region, false);
}

public void changeIsSelected(boolean selected) {
this.isSelected = selected;
}

@Override
public String toString() {
return String.format("%s, %s(id=%d, name=%s, isSelected=%s)", "회원동네", this.getClass().getSimpleName(), id, name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,16 @@ public void removeMemberTown() throws JsonProcessingException {
MemberTownRemoveResponse response = memberTownService.removeMemberTown(Principal.from(saveMember), request);

// then
assertAll(() -> {
assertThat(response.getAddress()).isEqualTo("서울 송파구 가락동");
assertThat(memberTownRepository.findMemberTownByMemberIdAndName(saveMember.getId(), "가락동")
.isEmpty()).isTrue();
});
assertAll(
() -> assertThat(response.getAddress()).isEqualTo("서울 송파구 가락동"),
() -> assertThat(memberTownRepository.findMemberTownByMemberIdAndName(saveMember.getId(), "가락동")
.isEmpty()).isTrue(),
() -> {
MemberTown remainMemberTown = memberTownRepository.findMemberTownByMemberIdAndName(saveMember.getId(),
"궁정동").orElseThrow();
assertThat(remainMemberTown.isSelected()).isTrue();
}
);
}

@DisplayName("등록되지 않은 주소 이름을 가지고 회원의 동네를 제거할 수 없다")
Expand Down

0 comments on commit 8428311

Please sign in to comment.