Skip to content

Commit 0a4f316

Browse files
committed
refactor(VolunteerApplyStatusChangeService): 중복 코드 해결
- approve와 reject usecase를 한 구현체에서 해결
1 parent 0854249 commit 0a4f316

File tree

5 files changed

+31
-98
lines changed

5 files changed

+31
-98
lines changed

src/main/java/com/somemore/volunteerapply/service/RejectVolunteerApplyService.java

Lines changed: 0 additions & 75 deletions
This file was deleted.

src/main/java/com/somemore/volunteerapply/service/ApproveVolunteerApplyService.java renamed to src/main/java/com/somemore/volunteerapply/service/VolunteerApplyStatusChangeService.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.somemore.volunteerapply.event.VolunteerApplyStatusChangeEvent;
1010
import com.somemore.volunteerapply.repository.VolunteerApplyRepository;
1111
import com.somemore.volunteerapply.usecase.ApproveVolunteerApplyUseCase;
12+
import com.somemore.volunteerapply.usecase.RejectVolunteerApplyUseCase;
1213
import lombok.RequiredArgsConstructor;
1314
import org.springframework.stereotype.Service;
1415
import org.springframework.transaction.annotation.Transactional;
@@ -19,26 +20,36 @@
1920
import static com.somemore.global.exception.ExceptionMessage.RECRUIT_BOARD_ALREADY_COMPLETED;
2021
import static com.somemore.global.exception.ExceptionMessage.UNAUTHORIZED_RECRUIT_BOARD;
2122
import static com.somemore.volunteerapply.domain.ApplyStatus.APPROVED;
23+
import static com.somemore.volunteerapply.domain.ApplyStatus.REJECTED;
2224

2325
@RequiredArgsConstructor
2426
@Transactional
2527
@Service
26-
public class ApproveVolunteerApplyService implements ApproveVolunteerApplyUseCase {
28+
public class VolunteerApplyStatusChangeService implements ApproveVolunteerApplyUseCase, RejectVolunteerApplyUseCase {
2729

2830
private final VolunteerApplyRepository volunteerApplyRepository;
2931
private final RecruitBoardQueryUseCase recruitBoardQueryUseCase;
3032
private final ServerEventPublisher serverEventPublisher;
3133

3234
@Override
3335
public void approve(Long id, UUID centerId) {
36+
changeApplyStatus(id, centerId, APPROVED);
37+
}
38+
39+
@Override
40+
public void reject(Long id, UUID centerId) {
41+
changeApplyStatus(id, centerId, REJECTED);
42+
}
43+
44+
private void changeApplyStatus(Long id, UUID centerId, ApplyStatus newStatus) {
3445
VolunteerApply apply = getVolunteerApply(id);
3546
RecruitBoard recruitBoard = recruitBoardQueryUseCase.getById(apply.getRecruitBoardId());
3647

3748
validateWriter(recruitBoard, centerId);
3849
validateBoardStatus(recruitBoard);
3950

4051
ApplyStatus oldStatus = apply.getStatus();
41-
apply.changeStatus(APPROVED);
52+
apply.changeStatus(newStatus);
4253
volunteerApplyRepository.save(apply);
4354

4455
publishVolunteerApplyStatusChangeEvent(apply, recruitBoard, oldStatus);
@@ -66,11 +77,9 @@ private void validateBoardStatus(RecruitBoard recruitBoard) {
6677
private void publishVolunteerApplyStatusChangeEvent(VolunteerApply apply,
6778
RecruitBoard recruitBoard,
6879
ApplyStatus oldStatus) {
69-
7080
if (apply.getStatus() == oldStatus) {
7181
return;
7282
}
73-
7483
serverEventPublisher.publish(VolunteerApplyStatusChangeEvent.from(apply, recruitBoard, oldStatus));
7584
}
76-
}
85+
}

src/test/java/com/somemore/volunteerapply/service/ApplyVolunteerApplyServiceTest.java renamed to src/test/java/com/somemore/volunteerapply/service/ApplyVolunteerApplyStatusChangeServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.springframework.transaction.annotation.Transactional;
2323

2424
@Transactional
25-
class ApplyVolunteerApplyServiceTest extends IntegrationTestSupport {
25+
class ApplyVolunteerApplyStatusChangeServiceTest extends IntegrationTestSupport {
2626

2727
@Autowired
2828
private ApplyVolunteerApplyService volunteerApplyCommandService;

src/test/java/com/somemore/volunteerapply/service/ApproveVolunteerApplyServiceTest.java renamed to src/test/java/com/somemore/volunteerapply/service/ApproveVolunteerApplyStatusChangeServiceTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
import static org.mockito.Mockito.verify;
3535

3636
@Transactional
37-
class ApproveVolunteerApplyServiceTest extends IntegrationTestSupport {
37+
class ApproveVolunteerApplyStatusChangeServiceTest extends IntegrationTestSupport {
3838

39-
private ApproveVolunteerApplyService approveVolunteerApplyService;
39+
private VolunteerApplyStatusChangeService volunteerApplyStatusChangeService;
4040

4141
@Autowired
4242
private VolunteerApplyRepository volunteerApplyRepository;
@@ -53,7 +53,7 @@ class ApproveVolunteerApplyServiceTest extends IntegrationTestSupport {
5353
@BeforeEach
5454
void setUp() {
5555
serverEventPublisher = mock(ServerEventPublisher.class);
56-
approveVolunteerApplyService = new ApproveVolunteerApplyService(
56+
volunteerApplyStatusChangeService = new VolunteerApplyStatusChangeService(
5757
volunteerApplyRepository,
5858
recruitBoardQueryUseCase,
5959
serverEventPublisher
@@ -73,7 +73,7 @@ void approve() {
7373
volunteerApplyRepository.save(apply);
7474

7575
// when
76-
approveVolunteerApplyService.approve(apply.getId(), centerId);
76+
volunteerApplyStatusChangeService.approve(apply.getId(), centerId);
7777

7878
// then
7979
VolunteerApply approve = volunteerApplyRepository.findById(apply.getId()).orElseThrow();
@@ -97,7 +97,7 @@ void approveWithWrongCenter() {
9797
// when
9898
// then
9999
assertThatThrownBy(
100-
() -> approveVolunteerApplyService.approve(id, wrongCenterId)
100+
() -> volunteerApplyStatusChangeService.approve(id, wrongCenterId)
101101
).isInstanceOf(BadRequestException.class)
102102
.hasMessage(UNAUTHORIZED_RECRUIT_BOARD.getMessage());
103103
}
@@ -117,7 +117,7 @@ void approveWithAlreadyCompletedRecruit() {
117117
// when
118118
// then
119119
assertThatThrownBy(
120-
() -> approveVolunteerApplyService.approve(id, centerId)
120+
() -> volunteerApplyStatusChangeService.approve(id, centerId)
121121
).isInstanceOf(BadRequestException.class)
122122
.hasMessage(ExceptionMessage.RECRUIT_BOARD_ALREADY_COMPLETED.getMessage());
123123
}
@@ -136,7 +136,7 @@ void approveWithSameStatusDoesNotPublishEvent() {
136136
volunteerApplyRepository.save(apply);
137137

138138
// when
139-
approveVolunteerApplyService.approve(apply.getId(), centerId);
139+
volunteerApplyStatusChangeService.approve(apply.getId(), centerId);
140140

141141
// then
142142
verify(serverEventPublisher, never()).publish(any());
@@ -157,7 +157,7 @@ void approveWithDifferentStatusPublishesEvent() {
157157
volunteerApplyRepository.save(apply);
158158

159159
// when
160-
approveVolunteerApplyService.approve(apply.getId(), centerId);
160+
volunteerApplyStatusChangeService.approve(apply.getId(), centerId);
161161

162162
// then
163163
ArgumentCaptor<VolunteerApplyStatusChangeEvent> eventCaptor = ArgumentCaptor.forClass(VolunteerApplyStatusChangeEvent.class);

src/test/java/com/somemore/volunteerapply/service/RejectVolunteerApplyServiceTest.java renamed to src/test/java/com/somemore/volunteerapply/service/RejectVolunteerApplyStatusChangeServiceTest.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import static com.somemore.common.fixture.RecruitBoardFixture.createRecruitBoard;
2424
import static com.somemore.global.exception.ExceptionMessage.UNAUTHORIZED_RECRUIT_BOARD;
2525
import static com.somemore.recruitboard.domain.VolunteerCategory.OTHER;
26-
import static com.somemore.volunteerapply.domain.ApplyStatus.APPROVED;
2726
import static com.somemore.volunteerapply.domain.ApplyStatus.REJECTED;
2827
import static com.somemore.volunteerapply.domain.ApplyStatus.WAITING;
2928
import static org.assertj.core.api.Assertions.assertThat;
@@ -35,9 +34,9 @@
3534
import static org.mockito.Mockito.verify;
3635

3736
@Transactional
38-
class RejectVolunteerApplyServiceTest extends IntegrationTestSupport {
37+
class RejectVolunteerApplyStatusChangeServiceTest extends IntegrationTestSupport {
3938

40-
private RejectVolunteerApplyService rejectVolunteerApplyService;
39+
private VolunteerApplyStatusChangeService volunteerApplyStatusChangeService;
4140

4241
@Autowired
4342
private VolunteerApplyRepository volunteerApplyRepository;
@@ -54,7 +53,7 @@ class RejectVolunteerApplyServiceTest extends IntegrationTestSupport {
5453
@BeforeEach
5554
void setUp() {
5655
serverEventPublisher = mock(ServerEventPublisher.class);
57-
rejectVolunteerApplyService = new RejectVolunteerApplyService(
56+
volunteerApplyStatusChangeService = new VolunteerApplyStatusChangeService(
5857
volunteerApplyRepository,
5958
recruitBoardQueryUseCase,
6059
serverEventPublisher
@@ -74,7 +73,7 @@ void reject() {
7473
volunteerApplyRepository.save(apply);
7574

7675
// when
77-
rejectVolunteerApplyService.reject(apply.getId(), centerId);
76+
volunteerApplyStatusChangeService.reject(apply.getId(), centerId);
7877

7978
// then
8079
VolunteerApply approve = volunteerApplyRepository.findById(apply.getId()).orElseThrow();
@@ -98,7 +97,7 @@ void rejectWithWrongCenter() {
9897
// when
9998
// then
10099
assertThatThrownBy(
101-
() -> rejectVolunteerApplyService.reject(id, wrongCenterId)
100+
() -> volunteerApplyStatusChangeService.reject(id, wrongCenterId)
102101
).isInstanceOf(BadRequestException.class)
103102
.hasMessage(UNAUTHORIZED_RECRUIT_BOARD.getMessage());
104103
}
@@ -118,7 +117,7 @@ void rejectWithAlreadyCompletedRecruit() {
118117
// when
119118
// then
120119
assertThatThrownBy(
121-
() -> rejectVolunteerApplyService.reject(id, centerId)
120+
() -> volunteerApplyStatusChangeService.reject(id, centerId)
122121
).isInstanceOf(BadRequestException.class)
123122
.hasMessage(ExceptionMessage.RECRUIT_BOARD_ALREADY_COMPLETED.getMessage());
124123
}
@@ -137,7 +136,7 @@ void approveWithSameStatusDoesNotPublishEvent() {
137136
volunteerApplyRepository.save(apply);
138137

139138
// when
140-
rejectVolunteerApplyService.reject(apply.getId(), centerId);
139+
volunteerApplyStatusChangeService.reject(apply.getId(), centerId);
141140

142141
// then
143142
verify(serverEventPublisher, never()).publish(any());
@@ -158,7 +157,7 @@ void approveWithDifferentStatusPublishesEvent() {
158157
volunteerApplyRepository.save(apply);
159158

160159
// when
161-
rejectVolunteerApplyService.reject(apply.getId(), centerId);
160+
volunteerApplyStatusChangeService.reject(apply.getId(), centerId);
162161

163162
// then
164163
ArgumentCaptor<VolunteerApplyStatusChangeEvent> eventCaptor = ArgumentCaptor.forClass(VolunteerApplyStatusChangeEvent.class);

0 commit comments

Comments
 (0)