diff --git a/src/main/java/com/kamco/cd/kamcoback/postgres/core/CommonCodeCoreService.java b/src/main/java/com/kamco/cd/kamcoback/postgres/core/CommonCodeCoreService.java index b3541ed9..259b885c 100644 --- a/src/main/java/com/kamco/cd/kamcoback/postgres/core/CommonCodeCoreService.java +++ b/src/main/java/com/kamco/cd/kamcoback/postgres/core/CommonCodeCoreService.java @@ -43,7 +43,7 @@ public class CommonCodeCoreService @CacheEvict(value = "commonCodes", allEntries = true) public ResponseObj save(CommonCodeDto.AddReq req) { - String regex = "^([A-Z]+|[0-9]+|[A-Z0-9]+(_[A-Z0-9]+)+)$"; + String regex = "^[A-Z0-9]+(_[A-Z0-9]+)*$"; boolean isValid = req.getCode().matches(regex); if (!isValid) { return new ResponseObj(ApiResponseCode.CONFLICT, "공통코드에 영문 대문자, 숫자, 언더바(_)만 입력 가능합니다."); @@ -76,9 +76,11 @@ public class CommonCodeCoreService "parent id 를 찾을 수 없습니다. id : " + req.getParentId())); entity.addParent(parentCommonCodeEntity); + } else { + entity.addParent(null); } - commonCodeRepository.save(entity).toDto(); + return new ResponseObj(ApiResponseCode.OK, ""); } @@ -202,7 +204,7 @@ public class CommonCodeCoreService public ResponseObj getCodeCheckDuplicate(Long parentId, String code) { Long existsCount = commonCodeRepository.findByParentIdCodeExists(parentId, code); - String regex = "^([A-Z]+|[0-9]+|[A-Z0-9]+(_[A-Z0-9]+)+)$"; + String regex = "^[A-Z0-9]+(_[A-Z0-9]+)*$"; boolean isValid = code.matches(regex); if (!isValid) { return new ResponseObj(ApiResponseCode.CONFLICT, "공통코드에 영문 대문자, 숫자, 언더바(_)만 입력 가능합니다."); diff --git a/src/main/java/com/kamco/cd/kamcoback/postgres/repository/code/CommonCodeRepositoryImpl.java b/src/main/java/com/kamco/cd/kamcoback/postgres/repository/code/CommonCodeRepositoryImpl.java index 2557240d..4298eedd 100644 --- a/src/main/java/com/kamco/cd/kamcoback/postgres/repository/code/CommonCodeRepositoryImpl.java +++ b/src/main/java/com/kamco/cd/kamcoback/postgres/repository/code/CommonCodeRepositoryImpl.java @@ -4,6 +4,7 @@ import static com.kamco.cd.kamcoback.postgres.entity.QCommonCodeEntity.commonCod import com.kamco.cd.kamcoback.postgres.entity.CommonCodeEntity; import com.kamco.cd.kamcoback.postgres.entity.QCommonCodeEntity; +import com.querydsl.core.types.dsl.BooleanExpression; import com.querydsl.jpa.impl.JPAQueryFactory; import java.util.List; import java.util.Optional; @@ -106,10 +107,16 @@ public class CommonCodeRepositoryImpl implements CommonCodeRepositoryCustom { return queryFactory .select(commonCodeEntity.code.count()) .from(commonCodeEntity) - .where(commonCodeEntity.parent.id.eq(parentId), commonCodeEntity.code.eq(code)) + .where(conditionParentId(parentId), commonCodeEntity.code.eq(code)) .fetchOne(); } + private BooleanExpression conditionParentId(Long parentId) { + return parentId == null + ? commonCodeEntity.parent.id.isNull() + : commonCodeEntity.parent.id.eq(parentId); + } + private List findAllByIds(Set ids) { return queryFactory .selectFrom(commonCodeEntity)