공통코드 컬럼 추가, 코드도 저장,수정할 수 있게 추가

This commit is contained in:
2025-12-03 16:36:31 +09:00
parent c3c484442e
commit 46de8c223a
6 changed files with 122 additions and 7 deletions

View File

@@ -27,6 +27,10 @@ public class CommonCodeDto {
private int order; private int order;
private boolean used; private boolean used;
private Long parentId; private Long parentId;
private String props1;
private String props2;
private String props3;
} }
@Schema(name = "CodeModifyReq", description = "공통코드 수정 정보") @Schema(name = "CodeModifyReq", description = "공통코드 수정 정보")
@@ -35,10 +39,15 @@ public class CommonCodeDto {
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public static class ModifyReq { public static class ModifyReq {
@NotEmpty private String code;
@NotEmpty private String name; @NotEmpty private String name;
private String description; private String description;
private int order; private int order;
private boolean used; private boolean used;
private String props1;
private String props2;
private String props3;
} }
@Schema(name = "CodeOrderReq", description = "공통코드 순서 변경 정보") @Schema(name = "CodeOrderReq", description = "공통코드 순서 변경 정보")
@@ -77,6 +86,10 @@ public class CommonCodeDto {
@JsonFormatDttm private ZonedDateTime updatedDttm; @JsonFormatDttm private ZonedDateTime updatedDttm;
private String props1;
private String props2;
private String props3;
public Basic( public Basic(
Long id, Long id,
String code, String code,
@@ -87,7 +100,11 @@ public class CommonCodeDto {
Boolean deleted, Boolean deleted,
List<CommonCodeDto.Basic> children, List<CommonCodeDto.Basic> children,
ZonedDateTime createdDttm, ZonedDateTime createdDttm,
ZonedDateTime updatedDttm) { ZonedDateTime updatedDttm,
String props1,
String props2,
String props3
) {
this.id = id; this.id = id;
this.code = code; this.code = code;
this.description = description; this.description = description;
@@ -98,6 +115,9 @@ public class CommonCodeDto {
this.children = children; this.children = children;
this.createdDttm = createdDttm; this.createdDttm = createdDttm;
this.updatedDttm = updatedDttm; this.updatedDttm = updatedDttm;
this.props1 = props1;
this.props2 = props2;
this.props3 = props3;
} }
} }
} }

View File

@@ -18,6 +18,7 @@ import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MethodArgumentNotValidException;
@@ -285,6 +286,29 @@ public class GlobalExceptionHandler {
errorLog.getId()); errorLog.getId());
} }
@ResponseStatus(HttpStatus.CONFLICT)
@ExceptionHandler(DuplicateKeyException.class)
public ApiResponseDto<String> handlerDuplicateKeyException(
DuplicateKeyException e, HttpServletRequest request) {
log.warn("[DuplicateKeyException] resource :{} ", e.getMessage());
String codeName = "DUPLICATE_DATA";
ErrorLogEntity errorLog =
saveErrerLogData(
request,
ApiResponseCode.getCode(codeName),
HttpStatus.valueOf("CONFLICT"),
ErrorLogDto.LogErrorLevel.WARNING,
e.getStackTrace());
return ApiResponseDto.createException(
ApiResponseCode.getCode(codeName),
ApiResponseCode.getMessage(codeName),
HttpStatus.valueOf("CONFLICT"),
errorLog.getId());
}
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(RuntimeException.class) @ExceptionHandler(RuntimeException.class)
public ApiResponseDto<String> handlerRuntimeException( public ApiResponseDto<String> handlerRuntimeException(

View File

@@ -10,6 +10,7 @@ import jakarta.persistence.EntityNotFoundException;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -34,16 +35,21 @@ public class CommonCodeCoreService
new EntityNotFoundException( new EntityNotFoundException(
"parent id 를 찾을 수 없습니다. id : " + req.getParentId())); "parent id 를 찾을 수 없습니다. id : " + req.getParentId()));
Long existsCount = commonCodeRepository.findByParentIdCodeExists(req.getParentId(), req.getCode());
if (existsCount > 0) {
throw new DuplicateKeyException("이미 등록되어 있습니다.");
}
CommonCodeEntity entity = CommonCodeEntity entity =
new CommonCodeEntity( new CommonCodeEntity(
req.getCode(), req.getName(), req.getDescription(), req.getOrder(), req.isUsed()); req.getCode(), req.getName(), req.getDescription(), req.getOrder(), req.isUsed(), req.getProps1(), req.getProps2(), req.getProps3());
entity.addParent(parentCommonCodeEntity); entity.addParent(parentCommonCodeEntity);
return commonCodeRepository.save(entity).toDto(); return commonCodeRepository.save(entity).toDto();
} }
CommonCodeEntity entity = CommonCodeEntity entity =
new CommonCodeEntity( new CommonCodeEntity(
req.getCode(), req.getName(), req.getDescription(), req.getOrder(), req.isUsed()); req.getCode(), req.getName(), req.getDescription(), req.getOrder(), req.isUsed(), req.getProps1(), req.getProps2(), req.getProps3());
return commonCodeRepository.save(entity).toDto(); return commonCodeRepository.save(entity).toDto();
} }
@@ -53,14 +59,25 @@ public class CommonCodeCoreService
.findByCodeId(id) .findByCodeId(id)
.orElseThrow(() -> new EntityNotFoundException("common code 를 찾을 수 없습니다. id : " + id)); .orElseThrow(() -> new EntityNotFoundException("common code 를 찾을 수 없습니다. id : " + id));
Long parentId = found.getParent() == null ? null : found.getParent().getId();
Long existsCount = commonCodeRepository.findByParentIdCodeExiststoUpdate(id, parentId, req.getCode());
if (existsCount > 0) {
throw new DuplicateKeyException("이미 등록되어 있습니다.");
}
CommonCodeEntity entity = CommonCodeEntity entity =
new CommonCodeEntity( new CommonCodeEntity(
id, id,
req.getCode(),
req.getName(), req.getName(),
req.getDescription(), req.getDescription(),
req.getOrder(), req.getOrder(),
req.isUsed(), req.isUsed(),
found.getDeleted()); found.getDeleted(),
req.getProps1(),
req.getProps2(),
req.getProps3()
);
return commonCodeRepository.save(entity).toDto(); return commonCodeRepository.save(entity).toDto();
} }

View File

@@ -61,23 +61,42 @@ public class CommonCodeEntity extends CommonDateEntity {
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.ALL) @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<CommonCodeEntity> children = new ArrayList<>(); private List<CommonCodeEntity> children = new ArrayList<>();
@Size(max = 255)
@Column(name = "props1")
private String props1;
@Size(max = 255)
@Column(name = "props2")
private String props2;
@Size(max = 255)
@Column(name = "props3")
private String props3;
public CommonCodeEntity( public CommonCodeEntity(
String code, String name, String description, Integer order, Boolean used) { String code, String name, String description, Integer order, Boolean used, String props1, String props2, String props3) {
this.code = code; this.code = code;
this.name = name; this.name = name;
this.description = description; this.description = description;
this.order = order; this.order = order;
this.used = used; this.used = used;
this.props1 = props1;
this.props2 = props2;
this.props3 = props3;
} }
public CommonCodeEntity( public CommonCodeEntity(
Long id, String name, String description, Integer order, Boolean used, Boolean deleted) { Long id, String code, String name, String description, Integer order, Boolean used, Boolean deleted, String props1, String props2, String props3) {
this.id = id; this.id = id;
this.code = code;
this.name = name; this.name = name;
this.description = description; this.description = description;
this.order = order; this.order = order;
this.used = used; this.used = used;
this.deleted = deleted; this.deleted = deleted;
this.props1 = props1;
this.props2 = props2;
this.props3 = props3;
} }
public CommonCodeDto.Basic toDto() { public CommonCodeDto.Basic toDto() {
@@ -91,7 +110,11 @@ public class CommonCodeEntity extends CommonDateEntity {
this.deleted, this.deleted,
this.children.stream().map(CommonCodeEntity::toDto).toList(), this.children.stream().map(CommonCodeEntity::toDto).toList(),
super.getCreatedDate(), super.getCreatedDate(),
super.getModifiedDate()); super.getModifiedDate(),
this.props1,
this.props2,
this.props3
);
} }
public void addParent(CommonCodeEntity parent) { public void addParent(CommonCodeEntity parent) {

View File

@@ -2,6 +2,8 @@ package com.kamco.cd.kamcoback.postgres.repository.code;
import com.kamco.cd.kamcoback.code.dto.CommonCodeDto; import com.kamco.cd.kamcoback.code.dto.CommonCodeDto;
import com.kamco.cd.kamcoback.postgres.entity.CommonCodeEntity; import com.kamco.cd.kamcoback.postgres.entity.CommonCodeEntity;
import jakarta.validation.constraints.NotEmpty;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@@ -15,4 +17,8 @@ public interface CommonCodeRepositoryCustom {
void updateOrder(CommonCodeDto.OrderReq req); void updateOrder(CommonCodeDto.OrderReq req);
Optional<String> getCode(String parentCodeCd, String childCodeCd); Optional<String> getCode(String parentCodeCd, String childCodeCd);
Long findByParentIdCodeExists(Long parentId, @NotEmpty String code);
Long findByParentIdCodeExiststoUpdate(Long id, Long parentId, @NotEmpty String code);
} }

View File

@@ -96,6 +96,31 @@ public class CommonCodeRepositoryImpl implements CommonCodeRepositoryCustom {
return Optional.ofNullable(result); return Optional.ofNullable(result);
} }
@Override
public Long findByParentIdCodeExists(Long parentId, String code) {
return queryFactory
.select(commonCodeEntity.code.count())
.from(commonCodeEntity)
.where(
commonCodeEntity.parent.id.eq(parentId),
commonCodeEntity.code.eq(code)
)
.fetchOne();
}
@Override
public Long findByParentIdCodeExiststoUpdate(Long id, Long parentId, String code) {
return queryFactory
.select(commonCodeEntity.code.count())
.from(commonCodeEntity)
.where(
commonCodeEntity.parent.id.eq(parentId),
commonCodeEntity.code.eq(code),
commonCodeEntity.id.ne(id)
)
.fetchOne();
}
private List<CommonCodeEntity> findAllByIds(Set<Long> ids) { private List<CommonCodeEntity> findAllByIds(Set<Long> ids) {
return queryFactory return queryFactory
.selectFrom(commonCodeEntity) .selectFrom(commonCodeEntity)