-
Notifications
You must be signed in to change notification settings - Fork 2
Refactor/#143 modify changerequest api #144
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| package DGU_AI_LAB.admin_be.domain.requests.dto.request; | ||
|
|
||
| import DGU_AI_LAB.admin_be.domain.requests.entity.ChangeRequest; | ||
| import DGU_AI_LAB.admin_be.domain.requests.entity.ChangeType; | ||
| import DGU_AI_LAB.admin_be.domain.requests.entity.Request; | ||
| import DGU_AI_LAB.admin_be.domain.users.entity.User; | ||
| import DGU_AI_LAB.admin_be.error.ErrorCode; | ||
| import DGU_AI_LAB.admin_be.error.exception.BusinessException; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import DGU_AI_LAB.admin_be.domain.requests.dto.response.PortMappingDTO; | ||
| import DGU_AI_LAB.admin_be.domain.requests.dto.request.PortRequestDTO; | ||
| import DGU_AI_LAB.admin_be.domain.portRequests.service.PortRequestService; | ||
|
|
||
| @Slf4j | ||
| @Schema(description = "๋จ์ผ ๋ณ๊ฒฝ ์์ฒญ DTO") | ||
| public record SingleChangeRequestDTO( | ||
|
|
||
| @Schema(description = "๋ณ๊ฒฝ ํ์ ", example = "VOLUME_SIZE") | ||
| @NotNull(message = "๋ณ๊ฒฝ ํ์ ์ ํ์์ ๋๋ค.") | ||
| ChangeType changeType, | ||
|
|
||
| @Schema(description = "์๋ก์ด ๊ฐ", example = "100") | ||
| @NotBlank(message = "์๋ก์ด ๊ฐ์ ํ์์ ๋๋ค.") | ||
| String newValue, | ||
|
|
||
| @Schema(description = "๋ณ๊ฒฝ ์์ฒญ ์ฌ์ ", example = "ํ๋ก์ ํธ ์๊ตฌ์ฌํญ ๋ณ๊ฒฝ์ผ๋ก ์ธํ ์ฉ๋ ์ฆ์ค") | ||
| @NotBlank(message = "๋ณ๊ฒฝ ์ฌ์ ๋ ํ์์ ๋๋ค.") | ||
| String reason | ||
| ) { | ||
|
|
||
| /** | ||
| * ๊ธฐ์กด Request์์ oldValue๋ฅผ ์ถ์ถํ๊ณ ChangeRequest ์ํฐํฐ ์์ฑ | ||
| */ | ||
| public static ChangeRequest toEntity(SingleChangeRequestDTO dto, Request originalRequest, User requestedBy, ObjectMapper objectMapper, PortRequestService portRequestService) { | ||
| try { | ||
| String oldValue = extractOldValue(originalRequest, dto.changeType(), objectMapper, portRequestService); | ||
|
|
||
| return ChangeRequest.builder() | ||
| .request(originalRequest) | ||
| .changeType(dto.changeType()) | ||
| .oldValue(oldValue) | ||
| .newValue(dto.newValue()) | ||
| .reason(dto.reason()) | ||
| .requestedBy(requestedBy) | ||
| .build(); | ||
| } catch (Exception e) { | ||
| log.error("Failed to create ChangeRequest entity: {}", e.getMessage()); | ||
| throw new BusinessException(ErrorCode.INTERNAL_SERVER_ERROR); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * ๋ณ๊ฒฝ ํ์ ์ ๋ฐ๋ผ ๊ธฐ์กด ๊ฐ์ ์ถ์ถ | ||
| */ | ||
| private static String extractOldValue(Request originalRequest, ChangeType changeType, ObjectMapper objectMapper, PortRequestService portRequestService) { | ||
| try { | ||
| return switch (changeType) { | ||
| case VOLUME_SIZE -> objectMapper.writeValueAsString(originalRequest.getVolumeSizeGiB()); | ||
| case EXPIRES_AT -> objectMapper.writeValueAsString(originalRequest.getExpiresAt()); | ||
| case GROUP -> { | ||
| Set<Long> oldGroupIds = originalRequest.getRequestGroups().stream() | ||
| .map(requestGroup -> requestGroup.getGroup().getUbuntuGid()) | ||
| .collect(Collectors.toSet()); | ||
| yield objectMapper.writeValueAsString(oldGroupIds); | ||
| } | ||
| case RESOURCE_GROUP -> objectMapper.writeValueAsString(originalRequest.getResourceGroup().getRsgroupId()); | ||
| case CONTAINER_IMAGE -> objectMapper.writeValueAsString(originalRequest.getContainerImage().getImageId()); | ||
| case PORT -> { | ||
| // Get existing port requests for this request | ||
| List<PortMappingDTO> existingPorts = originalRequest.getRequestId() != null | ||
| ? portRequestService.getPortRequestsByRequestId(originalRequest.getRequestId()) | ||
| .stream() | ||
| .map(PortMappingDTO::fromEntity) | ||
| .collect(Collectors.toList()) | ||
| : List.of(); | ||
| yield objectMapper.writeValueAsString(existingPorts); | ||
| } | ||
| }; | ||
| } catch (Exception e) { | ||
| log.error("Failed to extract old value for change type {}: {}", changeType, e.getMessage()); | ||
| throw new BusinessException(ErrorCode.INTERNAL_SERVER_ERROR); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * DTO ๋ด๋ถ์์ ์์ฒด์ ์ผ๋ก ์ ํจ์ฑ์ ๊ฒ์ฆํ๊ณ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์กด์ฌ ์ฌ๋ถ๊น์ง ํ์ธํ๋ ํฉํ ๋ฆฌ ๋ฉ์๋ | ||
| */ | ||
| public static ChangeRequest createValidatedChangeRequest(SingleChangeRequestDTO dto, Request originalRequest, User requestedBy, ObjectMapper objectMapper, PortRequestService portRequestService) { | ||
| dto.validateAndCheckExistence(originalRequest); | ||
| return toEntity(dto, originalRequest, requestedBy, objectMapper, portRequestService); | ||
| } | ||
|
|
||
| /** | ||
| * ์ ํจ์ฑ ๊ฒ์ฆ ๋ฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์กด์ฌ ์ฌ๋ถ ํ์ธ | ||
| */ | ||
| private void validateAndCheckExistence(Request originalRequest) { | ||
| validateBasicFormat(); | ||
| validateValueFormat(); | ||
| // ํ์ํ ๊ฒฝ์ฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์กด์ฌ ์ฌ๋ถ ๊ฒ์ฆ ๋ก์ง ์ถ๊ฐ ๊ฐ๋ฅ | ||
| } | ||
|
|
||
| /** | ||
| * ๊ธฐ๋ณธ ํ์ ์ ํจ์ฑ ๊ฒ์ฆ | ||
| */ | ||
| private void validateBasicFormat() { | ||
| if (changeType == null) { | ||
| throw new BusinessException("๋ณ๊ฒฝ ํ์ ์ ํ์์ ๋๋ค.", ErrorCode.INVALID_INPUT_VALUE); | ||
| } | ||
|
|
||
| if (newValue == null || newValue.trim().isEmpty()) { | ||
| throw new BusinessException("์๋ก์ด ๊ฐ์ ํ์์ ๋๋ค.", ErrorCode.INVALID_INPUT_VALUE); | ||
| } | ||
|
|
||
| if (reason == null || reason.trim().isEmpty()) { | ||
| throw new BusinessException("๋ณ๊ฒฝ ์ฌ์ ๋ ํ์์ ๋๋ค.", ErrorCode.INVALID_INPUT_VALUE); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * ๋ณ๊ฒฝ ํ์ ์ ๋ฐ๋ฅธ ๊ฐ ํ์ ๊ฒ์ฆ | ||
| */ | ||
| private void validateValueFormat() { | ||
| try { | ||
| switch (changeType) { | ||
| case VOLUME_SIZE -> { | ||
| Long volumeSize = Long.parseLong(newValue.trim()); | ||
| if (volumeSize <= 0) { | ||
| throw new BusinessException("๋ณผ๋ฅจ ํฌ๊ธฐ๋ ์์์ฌ์ผ ํฉ๋๋ค.", ErrorCode.INVALID_INPUT_VALUE); | ||
| } | ||
| } | ||
| case EXPIRES_AT -> { | ||
| LocalDateTime.parse(newValue.trim()); | ||
|
||
| } | ||
| case GROUP -> { | ||
| ObjectMapper mapper = new ObjectMapper(); | ||
saokiritoni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Set<Long> groupIds = mapper.readValue(newValue, mapper.getTypeFactory().constructCollectionType(Set.class, Long.class)); | ||
| if (groupIds.isEmpty()) { | ||
| throw new BusinessException("๊ทธ๋ฃน ID ๋ชฉ๋ก์ ๋น์ด์์ ์ ์์ต๋๋ค.", ErrorCode.INVALID_INPUT_VALUE); | ||
| } | ||
| } | ||
| case RESOURCE_GROUP -> { | ||
| Integer resourceGroupId = Integer.parseInt(newValue.trim()); | ||
| if (resourceGroupId <= 0) { | ||
| throw new BusinessException("๋ฆฌ์์ค ๊ทธ๋ฃน ID๋ ์์์ฌ์ผ ํฉ๋๋ค.", ErrorCode.INVALID_INPUT_VALUE); | ||
| } | ||
| } | ||
| case CONTAINER_IMAGE -> { | ||
| Long imageId = Long.parseLong(newValue.trim()); | ||
| if (imageId <= 0) { | ||
| throw new BusinessException("์ปจํ ์ด๋ ์ด๋ฏธ์ง ID๋ ์์์ฌ์ผ ํฉ๋๋ค.", ErrorCode.INVALID_INPUT_VALUE); | ||
| } | ||
| } | ||
| case PORT -> { | ||
| ObjectMapper mapper = new ObjectMapper(); | ||
|
||
| List<PortRequestDTO> portRequests = mapper.readValue(newValue, | ||
| mapper.getTypeFactory().constructCollectionType(List.class, PortRequestDTO.class)); | ||
|
|
||
| // Validate each port request | ||
| for (PortRequestDTO portRequest : portRequests) { | ||
| if (portRequest.internalPort() == null || portRequest.internalPort() < 1 || portRequest.internalPort() > 65535) { | ||
| throw new BusinessException("๋ด๋ถ ํฌํธ๋ 1-65535 ๋ฒ์์ฌ์ผ ํฉ๋๋ค.", ErrorCode.INVALID_INPUT_VALUE); | ||
| } | ||
| if (portRequest.usagePurpose() == null || portRequest.usagePurpose().trim().isEmpty()) { | ||
| throw new BusinessException("ํฌํธ ์ฌ์ฉ ๋ชฉ์ ์ ํ์์ ๋๋ค.", ErrorCode.INVALID_INPUT_VALUE); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } catch (BusinessException e) { | ||
| throw e; | ||
| } catch (Exception e) { | ||
| throw new BusinessException("์๋ก์ด ๊ฐ์ ํ์์ด ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค: " + e.getMessage(), ErrorCode.INVALID_INPUT_VALUE); | ||
| } | ||
| } | ||
| } | ||
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.
The removed import for ModifyRequestDTO suggests this class may no longer be used. Verify that ModifyRequestDTO can be safely removed from the codebase to avoid dead code.