Skip to content

Commit e245f58

Browse files
authored
refactor: 관심 기관 삭제 기능 변경 (#231)
* refactor(interest-center): 관심 기관 추가 삭제 로직 변경 * refactor(interest-center): 관심 기관 추가 삭제 로직 변경 API * test(interest-center): 관심 기관 추가 삭제 로직 변경 테스트 * test(interest-center): 관심 기관 추가 삭제 로직 변경 API 테스트 * fix: sonar qube 이슈 수정
1 parent a75f8ad commit e245f58

12 files changed

+119
-42
lines changed

src/main/java/com/somemore/interestcenter/controller/InterestCenterCommandApiController.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.somemore.interestcenter.controller;
22

3+
import com.somemore.auth.annotation.CurrentUser;
34
import com.somemore.global.common.response.ApiResponse;
45
import com.somemore.interestcenter.dto.request.RegisterInterestCenterRequestDto;
56
import com.somemore.interestcenter.dto.response.RegisterInterestCenterResponseDto;
@@ -8,8 +9,14 @@
89
import io.swagger.v3.oas.annotations.Operation;
910
import io.swagger.v3.oas.annotations.tags.Tag;
1011
import jakarta.validation.Valid;
12+
import java.util.UUID;
1113
import lombok.RequiredArgsConstructor;
12-
import org.springframework.web.bind.annotation.*;
14+
import org.springframework.security.access.annotation.Secured;
15+
import org.springframework.web.bind.annotation.DeleteMapping;
16+
import org.springframework.web.bind.annotation.PathVariable;
17+
import org.springframework.web.bind.annotation.PostMapping;
18+
import org.springframework.web.bind.annotation.RequestBody;
19+
import org.springframework.web.bind.annotation.RestController;
1320

1421
@RequiredArgsConstructor
1522
@RestController
@@ -19,20 +26,26 @@ public class InterestCenterCommandApiController {
1926
private final RegisterInterestCenterUseCase registerInterestCenterUseCase;
2027
private final CancelInterestCenterUseCase cancelInterestCenterUseCase;
2128

29+
@Secured("ROLE_VOLUNTEER")
2230
@Operation(summary = "관심기관 등록 API")
2331
@PostMapping("/api/interest-center")
24-
public ApiResponse<RegisterInterestCenterResponseDto> registerInterestCenter(@Valid @RequestBody RegisterInterestCenterRequestDto requestDto) {
32+
public ApiResponse<RegisterInterestCenterResponseDto> registerInterestCenter(
33+
@CurrentUser UUID volunteerId,
34+
@Valid @RequestBody RegisterInterestCenterRequestDto requestDto) {
2535

26-
RegisterInterestCenterResponseDto responseDto = registerInterestCenterUseCase.registerInterestCenter(requestDto);
36+
RegisterInterestCenterResponseDto responseDto = registerInterestCenterUseCase.registerInterestCenter(volunteerId, requestDto);
2737

2838
return ApiResponse.ok(200, responseDto, "관심 기관 등록 성공");
2939
}
3040

41+
@Secured("ROLE_VOLUNTEER")
3142
@Operation(summary = "관심기관 취소 API")
32-
@DeleteMapping("/api/interest-center/{interest-center-id}")
33-
public ApiResponse<String> deleteInterestCenter(@PathVariable("interest-center-id") Long interestCenterId) {
43+
@DeleteMapping("/api/interest-center/{centerId}")
44+
public ApiResponse<String> deleteInterestCenter(
45+
@CurrentUser UUID volunteerId,
46+
@PathVariable UUID centerId) {
3447

35-
cancelInterestCenterUseCase.cancelInterestCenter(interestCenterId);
48+
cancelInterestCenterUseCase.cancelInterestCenter(volunteerId, centerId);
3649

3750
return ApiResponse.ok("관심 기관 취소 성공");
3851
}

src/main/java/com/somemore/interestcenter/dto/request/RegisterInterestCenterRequestDto.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,11 @@
1111
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
1212
public record RegisterInterestCenterRequestDto(
1313

14-
@Schema(description = "봉사자 ID", example = "123e4567-e89b-12d3-a456-426614174000")
15-
@NotNull(message = "봉사자 ID는 필수값입니다.")
16-
UUID volunteerId,
17-
1814
@Schema(description = "기관 ID", example = "123e4567-e89b-12d3-a456-426614174000")
1915
@NotNull(message = "기관 ID는 필수값입니다.")
2016
UUID centerId
2117
) {
22-
public InterestCenter toEntity(){
18+
public InterestCenter toEntity(UUID volunteerId){
2319
return InterestCenter.create(volunteerId, centerId);
2420
}
2521
}

src/main/java/com/somemore/interestcenter/repository/InterestCenterRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ public interface InterestCenterRepository {
1313
Optional<RegisterInterestCenterResponseDto> findInterestCenterResponseById(Long id);
1414
List<UUID> findInterestCenterIdsByVolunteerId(UUID volunteerId);
1515
boolean existsByVolunteerIdAndCenterId(UUID volunteerId, UUID centerId);
16+
Optional<InterestCenter> findByVolunteerIdAndCenterId(UUID volunteerId, UUID centerId);
1617
}

src/main/java/com/somemore/interestcenter/repository/InterestCenterRepositoryImpl.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
import com.somemore.interestcenter.domain.InterestCenter;
66
import com.somemore.interestcenter.domain.QInterestCenter;
77
import com.somemore.interestcenter.dto.response.RegisterInterestCenterResponseDto;
8-
import lombok.RequiredArgsConstructor;
9-
import org.springframework.stereotype.Repository;
10-
118
import java.util.List;
129
import java.util.Optional;
1310
import java.util.UUID;
11+
import lombok.RequiredArgsConstructor;
12+
import org.springframework.stereotype.Repository;
1413

1514
@RequiredArgsConstructor
1615
@Repository
@@ -93,4 +92,18 @@ public boolean existsByVolunteerIdAndCenterId(UUID volunteerId, UUID centerId) {
9392
return result != null;
9493
}
9594

95+
@Override
96+
public Optional<InterestCenter> findByVolunteerIdAndCenterId(UUID volunteerId, UUID centerId) {
97+
QInterestCenter interestCenter = QInterestCenter.interestCenter;
98+
99+
InterestCenter result = queryFactory.selectFrom(interestCenter)
100+
.where(
101+
interestCenter.volunteerId.eq(volunteerId)
102+
.and(interestCenter.centerId.eq(centerId))
103+
.and(interestCenter.deleted.isFalse()))
104+
.fetchOne();
105+
106+
return Optional.ofNullable(result);
107+
}
108+
96109
}

src/main/java/com/somemore/interestcenter/service/CancelInterestCenterService.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.somemore.interestcenter.service;
22

3+
import com.somemore.center.usecase.query.CenterQueryUseCase;
34
import com.somemore.global.exception.BadRequestException;
45
import com.somemore.interestcenter.domain.InterestCenter;
56
import com.somemore.interestcenter.repository.InterestCenterRepository;
67
import com.somemore.interestcenter.usecase.CancelInterestCenterUseCase;
8+
import java.util.UUID;
79
import lombok.RequiredArgsConstructor;
810
import org.springframework.stereotype.Service;
911

@@ -14,10 +16,13 @@
1416
public class CancelInterestCenterService implements CancelInterestCenterUseCase {
1517

1618
private final InterestCenterRepository interestCenterRepository;
19+
private final CenterQueryUseCase centerQueryUseCase;
1720

1821
@Override
19-
public void cancelInterestCenter(Long interestCenterId) {
20-
InterestCenter interestCenter = interestCenterRepository.findById(interestCenterId)
22+
public void cancelInterestCenter(UUID volunteerId, UUID centerId) {
23+
centerQueryUseCase.validateCenterExists(centerId);
24+
25+
InterestCenter interestCenter = interestCenterRepository.findByVolunteerIdAndCenterId(volunteerId, centerId)
2126
.orElseThrow(() -> new BadRequestException(CANNOT_CANCEL_DELETED_INTEREST_CENTER.getMessage()));
2227

2328
interestCenter.markAsDeleted();

src/main/java/com/somemore/interestcenter/service/RegisterInterestCenterService.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.somemore.interestcenter.dto.response.RegisterInterestCenterResponseDto;
88
import com.somemore.interestcenter.repository.InterestCenterRepository;
99
import com.somemore.interestcenter.usecase.RegisterInterestCenterUseCase;
10+
import java.util.UUID;
1011
import lombok.RequiredArgsConstructor;
1112
import org.springframework.stereotype.Service;
1213

@@ -20,16 +21,16 @@ public class RegisterInterestCenterService implements RegisterInterestCenterUseC
2021
private final CenterQueryUseCase centerQueryUseCase;
2122

2223
@Override
23-
public RegisterInterestCenterResponseDto registerInterestCenter(RegisterInterestCenterRequestDto requestDto) {
24+
public RegisterInterestCenterResponseDto registerInterestCenter(UUID volunteerId, RegisterInterestCenterRequestDto requestDto) {
2425

2526
centerQueryUseCase.validateCenterExists(requestDto.centerId());
2627

27-
boolean isDuplicate = repository.existsByVolunteerIdAndCenterId(requestDto.volunteerId(), requestDto.centerId());
28+
boolean isDuplicate = repository.existsByVolunteerIdAndCenterId(volunteerId, requestDto.centerId());
2829
if(isDuplicate){
2930
throw new DuplicateException(DUPLICATE_INTEREST_CENTER.getMessage());
3031
}
3132

32-
InterestCenter interestCenter = repository.save(requestDto.toEntity());
33+
InterestCenter interestCenter = repository.save(requestDto.toEntity(volunteerId));
3334

3435
return RegisterInterestCenterResponseDto.from(interestCenter);
3536
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.somemore.interestcenter.usecase;
22

3+
import java.util.UUID;
4+
35
public interface CancelInterestCenterUseCase {
4-
void cancelInterestCenter(Long interestCenterId);
6+
void cancelInterestCenter(UUID volunteerId, UUID centerId);
57
}

src/main/java/com/somemore/interestcenter/usecase/RegisterInterestCenterUseCase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import com.somemore.interestcenter.dto.request.RegisterInterestCenterRequestDto;
44
import com.somemore.interestcenter.dto.response.RegisterInterestCenterResponseDto;
5+
import java.util.UUID;
56

67
public interface RegisterInterestCenterUseCase {
7-
RegisterInterestCenterResponseDto registerInterestCenter(RegisterInterestCenterRequestDto requestDto);
8+
RegisterInterestCenterResponseDto registerInterestCenter(UUID volunteerId, RegisterInterestCenterRequestDto requestDto);
89
}

src/test/java/com/somemore/interestcenter/controller/InterestCenterCommandApiControllerTest.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.somemore.interestcenter.controller;
22

33
import com.somemore.ControllerTestSupport;
4+
import com.somemore.WithMockCustomUser;
45
import com.somemore.interestcenter.dto.request.RegisterInterestCenterRequestDto;
56
import com.somemore.interestcenter.dto.response.RegisterInterestCenterResponseDto;
67
import com.somemore.interestcenter.usecase.CancelInterestCenterUseCase;
@@ -11,6 +12,7 @@
1112
import org.springframework.boot.test.mock.mockito.MockBean;
1213

1314
import java.util.UUID;
15+
import org.springframework.security.test.context.support.WithMockUser;
1416

1517
import static org.mockito.BDDMockito.given;
1618
import static org.mockito.ArgumentMatchers.any;
@@ -31,16 +33,18 @@ class InterestCenterCommandApiControllerTest extends ControllerTestSupport {
3133
@BeforeEach
3234
void setUp() {
3335
requestDto = new RegisterInterestCenterRequestDto(
34-
UUID.fromString("123e4567-e89b-12d3-a456-426614174000"),
3536
UUID.fromString("123e4567-e89b-12d3-a456-426614174000")
3637
);
3738
}
3839

40+
@WithMockCustomUser
3941
@Test
4042
void registerInterestCenter_ShouldReturnSuccess() throws Exception {
4143
// given
42-
RegisterInterestCenterResponseDto responseDto = new RegisterInterestCenterResponseDto(1L, requestDto.volunteerId(), requestDto.centerId());
43-
given(registerInterestCenterUseCase.registerInterestCenter(any(RegisterInterestCenterRequestDto.class)))
44+
UUID volunteerId = UUID.randomUUID();
45+
46+
RegisterInterestCenterResponseDto responseDto = new RegisterInterestCenterResponseDto(1L, volunteerId, requestDto.centerId());
47+
given(registerInterestCenterUseCase.registerInterestCenter(any(UUID.class), any(RegisterInterestCenterRequestDto.class)))
4448
.willReturn(responseDto);
4549

4650
// when & then
@@ -54,14 +58,15 @@ void registerInterestCenter_ShouldReturnSuccess() throws Exception {
5458
.andExpect(jsonPath("$.data.center_id").value(responseDto.centerId().toString()));
5559
}
5660

61+
@WithMockCustomUser
5762
@Test
5863
void deleteInterestCenter_ShouldReturnSuccess() throws Exception {
5964
// given
60-
Long interestCenterId = 1L;
61-
doNothing().when(cancelInterestCenterUseCase).cancelInterestCenter(interestCenterId);
65+
UUID centerId = UUID.randomUUID();
66+
doNothing().when(cancelInterestCenterUseCase).cancelInterestCenter(any(UUID.class), any(UUID.class));
6267

6368
// when & then
64-
mockMvc.perform(delete("/api/interest-center/{interest-center-id}", interestCenterId))
69+
mockMvc.perform(delete("/api/interest-center/{centerId}", centerId))
6570
.andExpect(status().isOk())
6671
.andExpect(jsonPath("$.message").value("관심 기관 취소 성공"));
6772
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.somemore.interestcenter.repository;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import com.somemore.IntegrationTestSupport;
6+
import com.somemore.interestcenter.domain.InterestCenter;
7+
import java.util.Optional;
8+
import java.util.UUID;
9+
import org.junit.jupiter.api.DisplayName;
10+
import org.junit.jupiter.api.Test;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.transaction.annotation.Transactional;
13+
14+
@Transactional
15+
class InterestCenterRepositoryImplTest extends IntegrationTestSupport {
16+
17+
@Autowired
18+
private InterestCenterRepository interestCenterRepository;
19+
20+
@DisplayName("봉사자 아이디와 기관 아이디로 관심 기관을 조회할 수 있다.")
21+
@Test
22+
void findByVolunteerIdAndCenterId() {
23+
// given
24+
UUID centerId = UUID.randomUUID();
25+
UUID volunteerId = UUID.randomUUID();
26+
27+
InterestCenter interestCenter = InterestCenter.create(volunteerId, centerId);
28+
29+
interestCenterRepository.save(interestCenter);
30+
31+
// when
32+
Optional<InterestCenter> result = interestCenterRepository.findByVolunteerIdAndCenterId(
33+
volunteerId, centerId);
34+
35+
// then
36+
assertThat(result).isPresent();
37+
assertThat(result.get().getCenterId()).isEqualTo(centerId);
38+
assertThat(result.get().getVolunteerId()).isEqualTo(volunteerId);
39+
}
40+
41+
}

0 commit comments

Comments
 (0)