-
Notifications
You must be signed in to change notification settings - Fork 0
feat: 재고 수동 변경 기능 구현 #121
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
feat: 재고 수동 변경 기능 구현 #121
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9fdb3d1
feat: 재고 엔티티 내에 재고 수정 업데이트 메서드 추가
JoonKyoLee f737a66
feat: 재고 응답 DTO 추가
JoonKyoLee e2562dc
feat: 재고 수동 수정 요청 DTO 추가
JoonKyoLee 393becb
feat: 재고 수동 수정 서비스 로직 추가
JoonKyoLee 0a333a4
feat: 재고 수동 수정 API 추가
JoonKyoLee 7fbc00b
test: 재고 수동 수정 서비스 로직 테스트 추가
JoonKyoLee 36bd070
test: 재고 수동 수정 API 테스트 추가
JoonKyoLee b38d8c3
feat: 재고 수동 수정 요청 DTO 내의 유효성 검증 부분 추가
JoonKyoLee fbebc50
test: 재고 수동 수정 API 호출 시 요청값 검증에 실패하는 테스트 추가
JoonKyoLee 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
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
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
46 changes: 46 additions & 0 deletions
46
src/main/java/com/almang/inventory/inventory/controller/InventoryController.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,46 @@ | ||
| package com.almang.inventory.inventory.controller; | ||
|
|
||
| import com.almang.inventory.global.api.ApiResponse; | ||
| import com.almang.inventory.global.api.SuccessMessage; | ||
| import com.almang.inventory.global.security.principal.CustomUserPrincipal; | ||
| import com.almang.inventory.inventory.dto.request.UpdateInventoryRequest; | ||
| import com.almang.inventory.inventory.dto.response.InventoryResponse; | ||
| import com.almang.inventory.inventory.service.InventoryService; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
| import jakarta.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
| import org.springframework.web.bind.annotation.PatchMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @Slf4j | ||
| @RestController | ||
| @RequestMapping("/api/v1/inventory") | ||
| @RequiredArgsConstructor | ||
| @Tag(name = "Inventory", description = "재고 관련 API") | ||
| public class InventoryController { | ||
|
|
||
| private final InventoryService inventoryService; | ||
|
|
||
| @PatchMapping("/{inventoryId}") | ||
| @Operation(summary = "재고 수동 수정", description = "재고를 수정하고 수정된 재고 정보를 반환합니다.") | ||
| public ResponseEntity<ApiResponse<InventoryResponse>> updateInventory( | ||
| @PathVariable Long inventoryId, | ||
| @Valid @RequestBody UpdateInventoryRequest request, | ||
| @AuthenticationPrincipal CustomUserPrincipal userPrincipal | ||
| ) { | ||
| Long userId = userPrincipal.getId(); | ||
| log.info("[InventoryController] 재고 수동 수정 요청 - userId: {}", userId); | ||
| InventoryResponse response = inventoryService.updateInventory(inventoryId, request, userId); | ||
|
|
||
| return ResponseEntity.ok( | ||
| ApiResponse.success(SuccessMessage.UPDATE_INVENTORY_SUCCESS.getMessage(), response) | ||
| ); | ||
| } | ||
| } |
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
16 changes: 16 additions & 0 deletions
16
src/main/java/com/almang/inventory/inventory/dto/request/UpdateInventoryRequest.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,16 @@ | ||
| package com.almang.inventory.inventory.dto.request; | ||
|
|
||
| import jakarta.validation.constraints.DecimalMax; | ||
| import jakarta.validation.constraints.DecimalMin; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import jakarta.validation.constraints.PositiveOrZero; | ||
| import java.math.BigDecimal; | ||
|
|
||
| public record UpdateInventoryRequest( | ||
| @NotNull Long productId, | ||
| @PositiveOrZero BigDecimal displayStock, | ||
| @PositiveOrZero BigDecimal warehouseStock, | ||
| @PositiveOrZero BigDecimal outgoingReserved, | ||
| @PositiveOrZero BigDecimal incomingReserved, | ||
| @DecimalMin("0.0") @DecimalMax("1.0") BigDecimal reorderTriggerPoint | ||
| ) {} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/almang/inventory/inventory/dto/response/InventoryResponse.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,26 @@ | ||
| package com.almang.inventory.inventory.dto.response; | ||
|
|
||
| import com.almang.inventory.inventory.domain.Inventory; | ||
| import java.math.BigDecimal; | ||
|
|
||
| public record InventoryResponse( | ||
| Long inventoryId, | ||
| Long productId, | ||
| BigDecimal displayStock, | ||
| BigDecimal warehouseStock, | ||
| BigDecimal outgoingReserved, | ||
| BigDecimal incomingReserved, | ||
| BigDecimal reorderTriggerPoint | ||
| ) { | ||
| public static InventoryResponse from(Inventory inventory) { | ||
| return new InventoryResponse( | ||
| inventory.getId(), | ||
| inventory.getProduct().getId(), | ||
| inventory.getDisplayStock(), | ||
| inventory.getWarehouseStock(), | ||
| inventory.getOutgoingReserved(), | ||
| inventory.getIncomingReserved(), | ||
| inventory.getReorderTriggerPoint() | ||
| ); | ||
| } | ||
| } |
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
178 changes: 178 additions & 0 deletions
178
src/test/java/com/almang/inventory/inventory/controller/InventoryControllerTest.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,178 @@ | ||
| package com.almang.inventory.inventory.controller; | ||
|
|
||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.anyLong; | ||
| import static org.mockito.Mockito.when; | ||
| import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
|
||
| import com.almang.inventory.global.api.SuccessMessage; | ||
| import com.almang.inventory.global.config.TestSecurityConfig; | ||
| import com.almang.inventory.global.exception.BaseException; | ||
| import com.almang.inventory.global.exception.ErrorCode; | ||
| import com.almang.inventory.global.security.principal.CustomUserPrincipal; | ||
| import com.almang.inventory.inventory.dto.request.UpdateInventoryRequest; | ||
| import com.almang.inventory.inventory.dto.response.InventoryResponse; | ||
| import com.almang.inventory.inventory.service.InventoryService; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import java.math.BigDecimal; | ||
| import java.util.List; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
| import org.springframework.context.annotation.Import; | ||
| import org.springframework.data.jpa.mapping.JpaMetamodelMappingContext; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
| import org.springframework.test.context.ActiveProfiles; | ||
| import org.springframework.test.context.bean.override.mockito.MockitoBean; | ||
| import org.springframework.test.web.servlet.MockMvc; | ||
|
|
||
| @WebMvcTest(InventoryController.class) | ||
| @Import(TestSecurityConfig.class) | ||
| @ActiveProfiles("test") | ||
| public class InventoryControllerTest { | ||
|
|
||
| @Autowired private MockMvc mockMvc; | ||
| @Autowired private ObjectMapper objectMapper; | ||
|
|
||
| @MockitoBean private InventoryService inventoryService; | ||
| @MockitoBean private JpaMetamodelMappingContext jpaMetamodelMappingContext; | ||
|
|
||
| private UsernamePasswordAuthenticationToken auth() { | ||
| CustomUserPrincipal principal = | ||
| new CustomUserPrincipal(1L, "inventory_admin", List.of()); | ||
| return new UsernamePasswordAuthenticationToken( | ||
| principal, null, principal.getAuthorities() | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void 재고_수동_수정에_성공한다() throws Exception { | ||
| // given | ||
| Long inventoryId = 1L; | ||
| Long productId = 10L; | ||
|
|
||
| UpdateInventoryRequest request = new UpdateInventoryRequest( | ||
| productId, | ||
| BigDecimal.valueOf(1.234), | ||
| BigDecimal.valueOf(10.000), | ||
| BigDecimal.valueOf(0.500), | ||
| BigDecimal.valueOf(3.000), | ||
| BigDecimal.valueOf(0.25) | ||
| ); | ||
|
|
||
| InventoryResponse response = new InventoryResponse( | ||
| inventoryId, | ||
| productId, | ||
| BigDecimal.valueOf(1.234), | ||
| BigDecimal.valueOf(10.000), | ||
| BigDecimal.valueOf(0.500), | ||
| BigDecimal.valueOf(3.000), | ||
| BigDecimal.valueOf(0.25) | ||
| ); | ||
|
|
||
| when(inventoryService.updateInventory(anyLong(), any(UpdateInventoryRequest.class), anyLong())) | ||
| .thenReturn(response); | ||
|
|
||
| // when & then | ||
| mockMvc.perform(patch("/api/v1/inventory/{inventoryId}", inventoryId) | ||
| .with(authentication(auth())) | ||
| .contentType(MediaType.APPLICATION_JSON) | ||
| .content(objectMapper.writeValueAsString(request))) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(jsonPath("$.status").value(200)) | ||
| .andExpect(jsonPath("$.message") | ||
| .value(SuccessMessage.UPDATE_INVENTORY_SUCCESS.getMessage())) | ||
| .andExpect(jsonPath("$.data.inventoryId").value(inventoryId)) | ||
| .andExpect(jsonPath("$.data.productId").value(productId)) | ||
| .andExpect(jsonPath("$.data.displayStock").value(1.234)) | ||
| .andExpect(jsonPath("$.data.warehouseStock").value(10.000)) | ||
| .andExpect(jsonPath("$.data.incomingReserved").value(3.000)) | ||
| .andExpect(jsonPath("$.data.reorderTriggerPoint").value(0.25)); | ||
| } | ||
|
|
||
| @Test | ||
| void 재고_수동_수정시_사용자가_존재하지_않으면_예외가_발생한다() throws Exception { | ||
| // given | ||
| Long inventoryId = 1L; | ||
|
|
||
| UpdateInventoryRequest request = new UpdateInventoryRequest( | ||
| 10L, | ||
| BigDecimal.ZERO, | ||
| BigDecimal.ZERO, | ||
| BigDecimal.ZERO, | ||
| BigDecimal.ZERO, | ||
| BigDecimal.valueOf(0.2) | ||
| ); | ||
|
|
||
| when(inventoryService.updateInventory(anyLong(), any(UpdateInventoryRequest.class), anyLong())) | ||
| .thenThrow(new BaseException(ErrorCode.USER_NOT_FOUND)); | ||
|
|
||
| // when & then | ||
| mockMvc.perform(patch("/api/v1/inventory/{inventoryId}", inventoryId) | ||
| .with(authentication(auth())) | ||
| .contentType(MediaType.APPLICATION_JSON) | ||
| .content(objectMapper.writeValueAsString(request))) | ||
| .andExpect(status().isNotFound()) | ||
| .andExpect(jsonPath("$.status").value(ErrorCode.USER_NOT_FOUND.getHttpStatus().value())) | ||
| .andExpect(jsonPath("$.message").value(ErrorCode.USER_NOT_FOUND.getMessage())) | ||
| .andExpect(jsonPath("$.data").doesNotExist()); | ||
| } | ||
|
|
||
| @Test | ||
| void 재고_수동_수정시_재고가_존재하지_않으면_예외가_발생한다() throws Exception { | ||
| // given | ||
| Long inventoryId = 9999L; | ||
|
|
||
| UpdateInventoryRequest request = new UpdateInventoryRequest( | ||
| 10L, | ||
| BigDecimal.ZERO, | ||
| BigDecimal.ZERO, | ||
| BigDecimal.ZERO, | ||
| BigDecimal.ZERO, | ||
| BigDecimal.valueOf(0.2) | ||
| ); | ||
|
|
||
| when(inventoryService.updateInventory(anyLong(), any(UpdateInventoryRequest.class), anyLong())) | ||
| .thenThrow(new BaseException(ErrorCode.INVENTORY_NOT_FOUND)); | ||
|
|
||
| // when & then | ||
| mockMvc.perform(patch("/api/v1/inventory/{inventoryId}", inventoryId) | ||
| .with(authentication(auth())) | ||
| .contentType(MediaType.APPLICATION_JSON) | ||
| .content(objectMapper.writeValueAsString(request))) | ||
| .andExpect(status().isNotFound()) | ||
| .andExpect(jsonPath("$.status").value(ErrorCode.INVENTORY_NOT_FOUND.getHttpStatus().value())) | ||
| .andExpect(jsonPath("$.message").value(ErrorCode.INVENTORY_NOT_FOUND.getMessage())) | ||
| .andExpect(jsonPath("$.data").doesNotExist()); | ||
| } | ||
|
|
||
| @Test | ||
| void 재고_수동_수정_요청값_검증에_실패하면_예외가_발생한다() throws Exception { | ||
| // given | ||
| Long inventoryId = 1L; | ||
|
|
||
| UpdateInventoryRequest invalidRequest = new UpdateInventoryRequest( | ||
| null, | ||
| BigDecimal.valueOf(-1.0), | ||
| BigDecimal.ZERO, | ||
| BigDecimal.ZERO, | ||
| BigDecimal.ZERO, | ||
| BigDecimal.valueOf(1.5) | ||
| ); | ||
|
|
||
| // when & then | ||
| mockMvc.perform(patch("/api/v1/inventory/{inventoryId}", inventoryId) | ||
| .with(authentication(auth())) | ||
| .contentType(MediaType.APPLICATION_JSON) | ||
| .content(objectMapper.writeValueAsString(invalidRequest))) | ||
| .andExpect(status().isBadRequest()) | ||
| .andExpect(jsonPath("$.status") | ||
| .value(ErrorCode.INVALID_INPUT_VALUE.getHttpStatus().value())) | ||
| .andExpect(jsonPath("$.message") | ||
| .value(ErrorCode.INVALID_INPUT_VALUE.getMessage())) | ||
| .andExpect(jsonPath("$.data").doesNotExist()); | ||
| } | ||
| } |
Oops, something went wrong.
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.
재고 값에 대한 검증이 필요합니다.
현재
updateManually메서드는 음수 재고 값에 대한 검증을 수행하지 않습니다. 재고 수량(displayStock,warehouseStock,outgoingReserved,incomingReserved)이 음수가 되면 데이터 무결성 문제가 발생할 수 있습니다.또한
reorderTriggerPoint는Store엔티티의defaultCountCheckThreshold와 마찬가지로 0과 1 사이의 값이어야 하는데, 이에 대한 범위 검증이 없습니다.서비스 계층에서 요청 DTO에 다음과 같은 검증 어노테이션을 추가하는 것을 권장합니다:
참고: Jakarta Bean Validation 공식 문서
🤖 Prompt for AI Agents