-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/107 선호물품 등록 엔드포인트 구현 #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/main/java/com/somemore/center/controller/PreferItemCommandApiController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package com.somemore.center.controller; | ||
|
|
||
| import com.somemore.center.dto.request.PreferItemCreateRequestDto; | ||
| import com.somemore.center.dto.response.PreferItemCreateResponseDto; | ||
| import com.somemore.center.usecase.command.CreatePreferItemUseCase; | ||
| import com.somemore.global.common.response.ApiResponse; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
| import jakarta.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.security.access.annotation.Secured; | ||
| import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @RestController | ||
| @RequestMapping("/api/preferItem") | ||
| @Tag(name = "PreferItem Command API", description = "선호 물품 등록, 삭제 기능을 제공합니다") | ||
| public class PreferItemCommandApiController { | ||
|
|
||
| private final CreatePreferItemUseCase createPreferItemUseCase; | ||
|
|
||
| @Secured("ROLE_CENTER") | ||
| @Operation(summary = "기관 선호물품 등록 API") | ||
| @PostMapping() | ||
| public ApiResponse<PreferItemCreateResponseDto> registerPreferItem(@Valid @RequestBody PreferItemCreateRequestDto requestDto, | ||
| @AuthenticationPrincipal String userId) { | ||
|
|
||
| PreferItemCreateResponseDto responseDto = createPreferItemUseCase.createPreferItem(UUID.fromString(userId), requestDto); | ||
|
|
||
| return ApiResponse.ok(200, responseDto, "관심 기관 등록 성공"); | ||
| } | ||
| } |
12 changes: 8 additions & 4 deletions
12
src/main/java/com/somemore/center/dto/request/PreferItemCreateRequestDto.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/com/somemore/center/dto/response/PreferItemCreateResponseDto.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package com.somemore.center.dto.response; | ||
|
|
||
| import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
| import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
| import com.somemore.center.domain.PreferItem; | ||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import lombok.Builder; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) | ||
| @Builder | ||
| public record PreferItemCreateResponseDto( | ||
| @Schema(description = "선호물품의 ID", example = "111") | ||
| Long id, | ||
| @Schema(description = "기관의 ID", example = "123e4567-e89b-12d3-a456-426614174000") | ||
| UUID centerId, | ||
| @Schema(description = "선호물품 이름", example = "어린이 도서") | ||
| String itemName | ||
| ) { | ||
| public static PreferItemCreateResponseDto from(PreferItem preferItem) { | ||
| return PreferItemCreateResponseDto.builder() | ||
| .id(preferItem.getId()) | ||
| .centerId(preferItem.getCenterId()) | ||
| .itemName(preferItem.getItemName()) | ||
| .build(); | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 4 additions & 1 deletion
5
src/main/java/com/somemore/center/usecase/command/CreatePreferItemUseCase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,11 @@ | ||
| package com.somemore.center.usecase.command; | ||
|
|
||
| import com.somemore.center.dto.request.PreferItemCreateRequestDto; | ||
| import com.somemore.center.dto.response.PreferItemCreateResponseDto; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| public interface CreatePreferItemUseCase { | ||
|
|
||
| void createPreferItem(PreferItemCreateRequestDto requestDto); | ||
| PreferItemCreateResponseDto createPreferItem(UUID userId, PreferItemCreateRequestDto requestDto); | ||
| } |
83 changes: 83 additions & 0 deletions
83
src/test/java/com/somemore/center/controller/PreferItemCommandApiControllerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package com.somemore.center.controller; | ||
|
|
||
| import com.somemore.ControllerTestSupport; | ||
| import com.somemore.WithMockCustomUser; | ||
| import com.somemore.center.dto.request.PreferItemCreateRequestDto; | ||
| import com.somemore.center.dto.response.PreferItemCreateResponseDto; | ||
| import com.somemore.center.usecase.command.CreatePreferItemUseCase; | ||
| import com.somemore.global.exception.BadRequestException; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.boot.test.mock.mockito.MockBean; | ||
| import org.springframework.http.MediaType; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| import static org.mockito.BDDMockito.given; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
|
||
| class PreferItemCommandApiControllerTest extends ControllerTestSupport { | ||
|
|
||
| @MockBean | ||
| private CreatePreferItemUseCase createPreferItemUseCase; | ||
|
|
||
|
|
||
| @DisplayName("기관은 선호물품을 등록할 수 있다. (controller)") | ||
| @Test | ||
| @WithMockCustomUser(role = "CENTER") | ||
| void registerPreferItem() throws Exception { | ||
| // given | ||
| UUID userId = UUID.fromString("123e4567-e89b-12d3-a456-426614174000"); // 고정된 UUID 사용 | ||
| PreferItemCreateRequestDto requestDto = new PreferItemCreateRequestDto("어린이 도서"); | ||
| PreferItemCreateResponseDto responseDto = new PreferItemCreateResponseDto( | ||
| 1L, | ||
| userId, | ||
| "어린이 도서" | ||
| ); | ||
|
|
||
| given(createPreferItemUseCase.createPreferItem(userId, requestDto)) | ||
| .willReturn(responseDto); | ||
|
|
||
| // when & then | ||
| mockMvc.perform(post("/api/preferItem") | ||
| .contentType(MediaType.APPLICATION_JSON) | ||
| .content(objectMapper.writeValueAsString(requestDto)) | ||
| .principal(() -> userId.toString())) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(jsonPath("$.code").value(200)) | ||
| .andExpect(jsonPath("$.message").value("관심 기관 등록 성공")) | ||
| .andExpect(jsonPath("$.data.id").value(responseDto.id())) | ||
| .andExpect(jsonPath("$.data.center_id").value(responseDto.centerId().toString())) | ||
| .andExpect(jsonPath("$.data.item_name").value(responseDto.itemName())); | ||
|
|
||
| // verify | ||
| verify(createPreferItemUseCase).createPreferItem(userId, requestDto); | ||
| } | ||
|
|
||
| @DisplayName("존재하지 않는 기관 ID로 선호물품을 등록할 수 없다. (controller)") | ||
| @Test | ||
| @WithMockCustomUser(role = "CENTER") | ||
| void registerPreferItem_Fail_WhenCenterNotExists() throws Exception { | ||
| // given | ||
| UUID userId = UUID.fromString("123e4567-e89b-12d3-a456-426614174000"); | ||
| PreferItemCreateRequestDto requestDto = new PreferItemCreateRequestDto("어린이 도서"); | ||
|
|
||
| given(createPreferItemUseCase.createPreferItem(userId, requestDto)) | ||
| .willThrow(new BadRequestException("존재하지 않는 기관입니다.")); | ||
|
|
||
| // when & then | ||
| mockMvc.perform(post("/api/preferItem") | ||
| .contentType(MediaType.APPLICATION_JSON) | ||
| .content(objectMapper.writeValueAsString(requestDto)) | ||
| .principal(() -> userId.toString())) | ||
| .andExpect(status().isBadRequest()) | ||
| .andExpect(jsonPath("$.status").value(400)) | ||
| .andExpect(jsonPath("$.detail").value("존재하지 않는 기관입니다.")); | ||
|
|
||
| // verify | ||
| verify(createPreferItemUseCase).createPreferItem(userId, requestDto); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
스웨거 적용 하면 좋을 것 같아요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 여태 등록의 경우에는 id만 반환해줬는데 선호물품아이디, 기관아이디, 아이템이름을 보내주는 이유가 있을까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
피그마를 보고 넣어줬습니다 물품 추가, 삭제시 바로바로 웹페이지에 적용이 되는데
정합성을 유지하려면 필드를 넣어줘야할 것 같았습니다!
프론트가 구현 방식에 따라 다시 바뀔수도 있습니다