Skip to content

Commit

Permalink
fix: 프로필에 동일한 활동을 중복해서 등록할 수 없도록 구현
Browse files Browse the repository at this point in the history
- 온보딩을 완료한 사용자가 온보딩을 할 경우 예외 처리
- 활동 id 중 이미 존재하는 활동의 id가 포함되어 있을 경우 예외 반환
- 활동 id 목록에 중복 id가 포함되어 있을 경우 예외 반환

#232
  • Loading branch information
amaran-th committed Aug 7, 2023
1 parent 74f13a5 commit d02814e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public void registerActivities(
final Member member,
final MemberActivityInitialRequest memberActivityInitialRequest
) {
if (member.isOnboarded()) {
throw new MemberException(MemberExceptionType.ALREADY_ONBOARDING);
}
final List<Long> activityIds = memberActivityInitialRequest.getActivityIds();
saveMemberActivities(member, activityIds);

Expand Down Expand Up @@ -62,11 +65,30 @@ public List<MemberActivityResponses> addActivity(
final MemberActivityAddRequest memberActivityAddRequest
) {
final List<Long> activityIds = memberActivityAddRequest.getActivityIds();
final List<MemberActivity> memberActivities = memberActivityRepository.findAllByMember(member);
if (isAlreadyExistActivity(memberActivities, activityIds) || hasDuplicateId(memberActivities,
activityIds)) {
throw new MemberException(MemberExceptionType.ALREADY_EXIST_ACTIVITY);
}
saveMemberActivities(member, activityIds);

return MemberActivityResponses.from(memberActivityRepository.findAllByMember(member));
}

private boolean isAlreadyExistActivity(final List<MemberActivity> memberActivities,
final List<Long> activityIds) {
return memberActivities
.stream()
.anyMatch(memberActivity ->
activityIds.contains(memberActivity.getActivity().getId())
);
}

private boolean hasDuplicateId(final List<MemberActivity> memberActivities,
final List<Long> activityIds) {
return activityIds.stream().distinct().count() != memberActivities.size();
}

public List<MemberActivityResponses> deleteActivity(
final Member member,
final MemberActivityDeleteRequest memberActivityDeleteRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public enum MemberExceptionType implements BaseExceptionType {
"해당 멤버는 존재하지 않습니다."
),

ALREADY_ONBOARDING(
HttpStatus.BAD_REQUEST,
"이미 온보딩을 완료한 사용자입니다."
),

INVALID_ACTIVITY_IDS(
HttpStatus.BAD_REQUEST,
"요청한 activity id들 중에 유효하지 않은 값이 존재합니다"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ void addActivity() throws Exception {

//when
final List<MemberActivityResponses> actual = memberActivityService.addActivity(member, request);

//then
assertThat(expected)
.usingRecursiveComparison()
Expand All @@ -121,7 +120,7 @@ void addActivity() throws Exception {
void test_addActivity_invalid_activity_ids_Exception() throws Exception {
//given
final Member savedMember = memberRepository.findById(1L).get();
final List<Long> activityIds = List.of(1L, 2L, 7L);
final List<Long> activityIds = List.of(4L, 5L, 7L);
final MemberActivityAddRequest request = new MemberActivityAddRequest(activityIds);

//when & then
Expand All @@ -138,10 +137,7 @@ void test_addActivity_ALREADY_EXIST_ACTIVITY_Exception_duplicate_try() throws Ex
final List<Long> activityIds = List.of(1L, 2L, 7L);
final MemberActivityAddRequest request = new MemberActivityAddRequest(activityIds);

// when
memberActivityService.addActivity(savedMember, request);

// then
// when, then
assertThatThrownBy(() -> memberActivityService.addActivity(savedMember, request))
.isInstanceOf(MemberException.class)
.hasMessage(MemberExceptionType.ALREADY_EXIST_ACTIVITY.errorMessage());
Expand All @@ -152,7 +148,7 @@ void test_addActivity_ALREADY_EXIST_ACTIVITY_Exception_duplicate_try() throws Ex
void test_addActivity_ALREADY_EXIST_ACTIVITY_Exception_duplicate_input() throws Exception {
//given
final Member savedMember = memberRepository.findById(1L).get();
final List<Long> activityIds = List.of(1L, 2L, 1L);
final List<Long> activityIds = List.of(4L, 4L, 5L);
final MemberActivityAddRequest request = new MemberActivityAddRequest(activityIds);

// when, then
Expand Down
2 changes: 1 addition & 1 deletion backend/emm-sale/src/test/resources/data-test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ insert into activity(id, type, name)
values (6, 'JOB', 'Backend');

insert into member(id, name, image_url, open_profile_url, github_id, created_at, updated_at)
values (1, 'member1', 'https://imageurl.com', 'https://openprofileurl.com', 1, CURRENT_TIMESTAMP(),
values (1, null, 'https://imageurl.com', 'https://openprofileurl.com', 1, CURRENT_TIMESTAMP(),
CURRENT_TIMESTAMP());

insert into member(id, name, image_url, open_profile_url, github_id, created_at, updated_at)
Expand Down

0 comments on commit d02814e

Please sign in to comment.