공통코드 컬럼 추가, 코드도 저장,수정할 수 있게 추가
This commit is contained in:
@@ -27,6 +27,10 @@ public class CommonCodeDto {
|
||||
private int order;
|
||||
private boolean used;
|
||||
private Long parentId;
|
||||
|
||||
private String props1;
|
||||
private String props2;
|
||||
private String props3;
|
||||
}
|
||||
|
||||
@Schema(name = "CodeModifyReq", description = "공통코드 수정 정보")
|
||||
@@ -35,10 +39,15 @@ public class CommonCodeDto {
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class ModifyReq {
|
||||
@NotEmpty private String code;
|
||||
@NotEmpty private String name;
|
||||
private String description;
|
||||
private int order;
|
||||
private boolean used;
|
||||
|
||||
private String props1;
|
||||
private String props2;
|
||||
private String props3;
|
||||
}
|
||||
|
||||
@Schema(name = "CodeOrderReq", description = "공통코드 순서 변경 정보")
|
||||
@@ -77,6 +86,10 @@ public class CommonCodeDto {
|
||||
|
||||
@JsonFormatDttm private ZonedDateTime updatedDttm;
|
||||
|
||||
private String props1;
|
||||
private String props2;
|
||||
private String props3;
|
||||
|
||||
public Basic(
|
||||
Long id,
|
||||
String code,
|
||||
@@ -87,7 +100,11 @@ public class CommonCodeDto {
|
||||
Boolean deleted,
|
||||
List<CommonCodeDto.Basic> children,
|
||||
ZonedDateTime createdDttm,
|
||||
ZonedDateTime updatedDttm) {
|
||||
ZonedDateTime updatedDttm,
|
||||
String props1,
|
||||
String props2,
|
||||
String props3
|
||||
) {
|
||||
this.id = id;
|
||||
this.code = code;
|
||||
this.description = description;
|
||||
@@ -98,6 +115,9 @@ public class CommonCodeDto {
|
||||
this.children = children;
|
||||
this.createdDttm = createdDttm;
|
||||
this.updatedDttm = updatedDttm;
|
||||
this.props1 = props1;
|
||||
this.props2 = props2;
|
||||
this.props3 = props3;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ import java.util.stream.Collectors;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
@@ -285,6 +286,29 @@ public class GlobalExceptionHandler {
|
||||
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)
|
||||
@ExceptionHandler(RuntimeException.class)
|
||||
public ApiResponseDto<String> handlerRuntimeException(
|
||||
|
||||
@@ -10,6 +10,7 @@ import jakarta.persistence.EntityNotFoundException;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -34,16 +35,21 @@ public class CommonCodeCoreService
|
||||
new EntityNotFoundException(
|
||||
"parent id 를 찾을 수 없습니다. id : " + req.getParentId()));
|
||||
|
||||
Long existsCount = commonCodeRepository.findByParentIdCodeExists(req.getParentId(), req.getCode());
|
||||
if (existsCount > 0) {
|
||||
throw new DuplicateKeyException("이미 등록되어 있습니다.");
|
||||
}
|
||||
|
||||
CommonCodeEntity entity =
|
||||
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);
|
||||
return commonCodeRepository.save(entity).toDto();
|
||||
}
|
||||
|
||||
CommonCodeEntity entity =
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -53,14 +59,25 @@ public class CommonCodeCoreService
|
||||
.findByCodeId(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 =
|
||||
new CommonCodeEntity(
|
||||
id,
|
||||
req.getCode(),
|
||||
req.getName(),
|
||||
req.getDescription(),
|
||||
req.getOrder(),
|
||||
req.isUsed(),
|
||||
found.getDeleted());
|
||||
found.getDeleted(),
|
||||
req.getProps1(),
|
||||
req.getProps2(),
|
||||
req.getProps3()
|
||||
);
|
||||
|
||||
return commonCodeRepository.save(entity).toDto();
|
||||
}
|
||||
|
||||
@@ -61,23 +61,42 @@ public class CommonCodeEntity extends CommonDateEntity {
|
||||
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
||||
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(
|
||||
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.name = name;
|
||||
this.description = description;
|
||||
this.order = order;
|
||||
this.used = used;
|
||||
this.props1 = props1;
|
||||
this.props2 = props2;
|
||||
this.props3 = props3;
|
||||
}
|
||||
|
||||
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.code = code;
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.order = order;
|
||||
this.used = used;
|
||||
this.deleted = deleted;
|
||||
this.props1 = props1;
|
||||
this.props2 = props2;
|
||||
this.props3 = props3;
|
||||
}
|
||||
|
||||
public CommonCodeDto.Basic toDto() {
|
||||
@@ -91,7 +110,11 @@ public class CommonCodeEntity extends CommonDateEntity {
|
||||
this.deleted,
|
||||
this.children.stream().map(CommonCodeEntity::toDto).toList(),
|
||||
super.getCreatedDate(),
|
||||
super.getModifiedDate());
|
||||
super.getModifiedDate(),
|
||||
this.props1,
|
||||
this.props2,
|
||||
this.props3
|
||||
);
|
||||
}
|
||||
|
||||
public void addParent(CommonCodeEntity parent) {
|
||||
|
||||
@@ -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.postgres.entity.CommonCodeEntity;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -15,4 +17,8 @@ public interface CommonCodeRepositoryCustom {
|
||||
void updateOrder(CommonCodeDto.OrderReq req);
|
||||
|
||||
Optional<String> getCode(String parentCodeCd, String childCodeCd);
|
||||
|
||||
Long findByParentIdCodeExists(Long parentId, @NotEmpty String code);
|
||||
|
||||
Long findByParentIdCodeExiststoUpdate(Long id, Long parentId, @NotEmpty String code);
|
||||
}
|
||||
|
||||
@@ -96,6 +96,31 @@ public class CommonCodeRepositoryImpl implements CommonCodeRepositoryCustom {
|
||||
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) {
|
||||
return queryFactory
|
||||
.selectFrom(commonCodeEntity)
|
||||
|
||||
Reference in New Issue
Block a user