Skip to content

Commit ec93f29

Browse files
committed
feat: 선호 물품 삭제 관련 프레젠테이션 레이어 구현
- 컨트롤러 구현 - 테스트 코드 작성및 검증 완료
1 parent b0d3b3f commit ec93f29

File tree

2 files changed

+84
-4
lines changed

2 files changed

+84
-4
lines changed

src/main/java/com/somemore/center/controller/PreferItemCommandApiController.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44
import com.somemore.center.dto.request.PreferItemCreateRequestDto;
55
import com.somemore.center.dto.response.PreferItemCreateResponseDto;
66
import com.somemore.center.usecase.command.CreatePreferItemUseCase;
7+
import com.somemore.center.usecase.command.DeletePreferItemUseCase;
78
import com.somemore.global.common.response.ApiResponse;
89
import io.swagger.v3.oas.annotations.Operation;
910
import io.swagger.v3.oas.annotations.tags.Tag;
1011
import jakarta.validation.Valid;
1112
import java.util.UUID;
1213
import lombok.RequiredArgsConstructor;
1314
import org.springframework.security.access.annotation.Secured;
14-
import org.springframework.web.bind.annotation.PostMapping;
15-
import org.springframework.web.bind.annotation.RequestBody;
16-
import org.springframework.web.bind.annotation.RequestMapping;
17-
import org.springframework.web.bind.annotation.RestController;
15+
import org.springframework.web.bind.annotation.*;
1816

1917
@RequiredArgsConstructor
2018
@RestController
@@ -23,6 +21,7 @@
2321
public class PreferItemCommandApiController {
2422

2523
private final CreatePreferItemUseCase createPreferItemUseCase;
24+
private final DeletePreferItemUseCase deletePreferItemUseCase;
2625

2726
@Secured("ROLE_CENTER")
2827
@Operation(summary = "기관 선호물품 등록 API")
@@ -36,4 +35,14 @@ public ApiResponse<PreferItemCreateResponseDto> registerPreferItem(
3635

3736
return ApiResponse.ok(200, responseDto, "관심 기관 등록 성공");
3837
}
38+
39+
@Secured("ROLE_CENTER")
40+
@Operation(summary = "기관 선호물품 삭제 API")
41+
@DeleteMapping("/{preferItemId}")
42+
public ApiResponse<String> deletePreferItem(@CurrentUser UUID centerId, @PathVariable Long preferItemId) {
43+
44+
deletePreferItemUseCase.deletePreferItem(centerId, preferItemId);
45+
46+
return ApiResponse.ok("선호 물품 삭제 성공");
47+
}
3948
}

src/test/java/com/somemore/center/controller/PreferItemCommandApiControllerTest.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.somemore.center.dto.request.PreferItemCreateRequestDto;
66
import com.somemore.center.dto.response.PreferItemCreateResponseDto;
77
import com.somemore.center.usecase.command.CreatePreferItemUseCase;
8+
import com.somemore.center.usecase.command.DeletePreferItemUseCase;
89
import com.somemore.global.exception.BadRequestException;
910
import org.junit.jupiter.api.DisplayName;
1011
import org.junit.jupiter.api.Test;
@@ -14,7 +15,9 @@
1415
import java.util.UUID;
1516

1617
import static org.mockito.BDDMockito.given;
18+
import static org.mockito.Mockito.doThrow;
1719
import static org.mockito.Mockito.verify;
20+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
1821
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
1922
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
2023
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -24,6 +27,9 @@ class PreferItemCommandApiControllerTest extends ControllerTestSupport {
2427
@MockBean
2528
private CreatePreferItemUseCase createPreferItemUseCase;
2629

30+
@MockBean
31+
private DeletePreferItemUseCase deletePreferItemUseCase;
32+
2733

2834
@DisplayName("기관은 선호물품을 등록할 수 있다. (controller)")
2935
@Test
@@ -80,4 +86,69 @@ void registerPreferItem_Fail_WhenCenterNotExists() throws Exception {
8086
// verify
8187
verify(createPreferItemUseCase).createPreferItem(userId, requestDto);
8288
}
89+
90+
@DisplayName("기관은 등록한 선호물품을 삭제할 수 있다. (controller)")
91+
@Test
92+
@WithMockCustomUser(role = "CENTER")
93+
void deletePreferItem() throws Exception {
94+
// given
95+
UUID userId = UUID.fromString("123e4567-e89b-12d3-a456-426614174000");
96+
Long preferItemId = 1L;
97+
98+
// when & then
99+
mockMvc.perform(delete("/api/preferItem/{preferItemId}", preferItemId)
100+
.principal(userId::toString))
101+
.andExpect(status().isOk())
102+
.andExpect(jsonPath("$.code").value(200))
103+
.andExpect(jsonPath("$.message").value("선호 물품 삭제 성공"));
104+
105+
// verify
106+
verify(deletePreferItemUseCase).deletePreferItem(userId, preferItemId);
107+
}
108+
109+
@DisplayName("선호물품을 등록한 기관이 아니라면 선호물품을 삭제할 수 없다. (controller)")
110+
@Test
111+
@WithMockCustomUser(role = "CENTER")
112+
void deletePreferItem_Fail_WhenUnauthorized() throws Exception {
113+
// given
114+
UUID userId = UUID.fromString("123e4567-e89b-12d3-a456-426614174000");
115+
Long preferItemId = 1L;
116+
117+
doThrow(new BadRequestException("다른 기관의 선호 물품은 삭제할 수 없습니다."))
118+
.when(deletePreferItemUseCase)
119+
.deletePreferItem(userId, preferItemId);
120+
121+
// when & then
122+
mockMvc.perform(delete("/api/preferItem/{preferItemId}", preferItemId)
123+
.principal(userId::toString))
124+
.andExpect(status().isBadRequest())
125+
.andExpect(jsonPath("$.status").value(400))
126+
.andExpect(jsonPath("$.detail").value("다른 기관의 선호 물품은 삭제할 수 없습니다."));
127+
128+
// verify
129+
verify(deletePreferItemUseCase).deletePreferItem(userId, preferItemId);
130+
}
131+
132+
@DisplayName("존재하지 않는 선호물품을 삭제할 수 없다. (controller)")
133+
@Test
134+
@WithMockCustomUser(role = "CENTER")
135+
void deletePreferItem_Fail_WhenPreferItemNotExists() throws Exception {
136+
// given
137+
UUID userId = UUID.fromString("123e4567-e89b-12d3-a456-426614174000");
138+
Long preferItemId = 1L;
139+
140+
doThrow(new BadRequestException("존재하지 않는 선호 물품입니다."))
141+
.when(deletePreferItemUseCase)
142+
.deletePreferItem(userId, preferItemId);
143+
144+
// when & then
145+
mockMvc.perform(delete("/api/preferItem/{preferItemId}", preferItemId)
146+
.principal(userId::toString))
147+
.andExpect(status().isBadRequest())
148+
.andExpect(jsonPath("$.status").value(400))
149+
.andExpect(jsonPath("$.detail").value("존재하지 않는 선호 물품입니다."));
150+
151+
// verify
152+
verify(deletePreferItemUseCase).deletePreferItem(userId, preferItemId);
153+
}
83154
}

0 commit comments

Comments
 (0)