diff --git a/src/main/java/clap/server/adapter/outbound/persistense/CategoryPersistenceAdapter.java b/src/main/java/clap/server/adapter/outbound/persistense/CategoryPersistenceAdapter.java index ef4b1d69..ed9f1a7f 100644 --- a/src/main/java/clap/server/adapter/outbound/persistense/CategoryPersistenceAdapter.java +++ b/src/main/java/clap/server/adapter/outbound/persistense/CategoryPersistenceAdapter.java @@ -49,6 +49,11 @@ public List findSubCategory() { .toList(); } + @Override + public boolean existsByNameOrCode(String name, String code) { + return categoryRepository.existsByNameOrCode(name, code); + } + @Override public void save(final Category category) { categoryRepository.save(categoryPersistenceMapper.toEntity(category)); diff --git a/src/main/java/clap/server/adapter/outbound/persistense/repository/task/CategoryRepository.java b/src/main/java/clap/server/adapter/outbound/persistense/repository/task/CategoryRepository.java index 017e760e..a5bec1e3 100644 --- a/src/main/java/clap/server/adapter/outbound/persistense/repository/task/CategoryRepository.java +++ b/src/main/java/clap/server/adapter/outbound/persistense/repository/task/CategoryRepository.java @@ -11,4 +11,6 @@ public interface CategoryRepository extends JpaRepository List findByIsDeletedFalse(); List findByIsDeletedFalseAndMainCategoryIsNull(); List findByIsDeletedFalseAndMainCategoryIsNotNull(); + + boolean existsByNameOrCode(String name, String code); } \ No newline at end of file diff --git a/src/main/java/clap/server/application/port/outbound/task/LoadCategoryPort.java b/src/main/java/clap/server/application/port/outbound/task/LoadCategoryPort.java index 49cc6eeb..2533e8ff 100644 --- a/src/main/java/clap/server/application/port/outbound/task/LoadCategoryPort.java +++ b/src/main/java/clap/server/application/port/outbound/task/LoadCategoryPort.java @@ -11,4 +11,6 @@ public interface LoadCategoryPort { List findAll(); List findMainCategory(); List findSubCategory(); + + boolean existsByNameOrCode(String name, String code); } diff --git a/src/main/java/clap/server/application/service/admin/AddCategoryService.java b/src/main/java/clap/server/application/service/admin/AddCategoryService.java index 6a09de2f..4b934dc4 100644 --- a/src/main/java/clap/server/application/service/admin/AddCategoryService.java +++ b/src/main/java/clap/server/application/service/admin/AddCategoryService.java @@ -15,6 +15,7 @@ import java.util.Optional; import static clap.server.exception.code.MemberErrorCode.ACTIVE_MEMBER_NOT_FOUND; +import static clap.server.exception.code.TaskErrorCode.CATEGORY_DUPLICATE; import static clap.server.exception.code.TaskErrorCode.CATEGORY_NOT_FOUND; @ApplicationService @@ -28,6 +29,7 @@ public class AddCategoryService implements AddMainCategoryUsecase, AddSubCategor @Transactional public void addMainCategory(Long adminId, String code, String name) { Optional activeMember = loadMemberPort.findActiveMemberById(adminId); + if (loadCategoryPort.existsByNameOrCode(name, code)) throw new ApplicationException(CATEGORY_DUPLICATE); Category mainCategory = Category.createMainCategory( activeMember.orElseThrow(() -> new ApplicationException(ACTIVE_MEMBER_NOT_FOUND)), code, name); @@ -39,7 +41,7 @@ public void addMainCategory(Long adminId, String code, String name) { public void addSubCategory(Long adminId, Long mainCategoryId, String code, String name, String descriptionExample) { Optional activeMember = loadMemberPort.findActiveMemberById(adminId); Optional mainCategory = loadCategoryPort.findById(mainCategoryId); - + if (loadCategoryPort.existsByNameOrCode(name, code)) throw new ApplicationException(CATEGORY_DUPLICATE); Category subCategory = Category.createSubCategory( activeMember.orElseThrow(() -> new ApplicationException(ACTIVE_MEMBER_NOT_FOUND)), mainCategory.orElseThrow(() -> new ApplicationException(CATEGORY_NOT_FOUND)), diff --git a/src/main/java/clap/server/exception/code/TaskErrorCode.java b/src/main/java/clap/server/exception/code/TaskErrorCode.java index 04b87837..3f01e759 100644 --- a/src/main/java/clap/server/exception/code/TaskErrorCode.java +++ b/src/main/java/clap/server/exception/code/TaskErrorCode.java @@ -18,7 +18,8 @@ public enum TaskErrorCode implements BaseErrorCode { NOT_A_REVIEWER(HttpStatus.FORBIDDEN, "TASK_009", "작업 승인 및 수정 권한이 없습니다."), NOT_A_REQUESTER(HttpStatus.FORBIDDEN, "TASK_010", "작업 수정 및 취소 권한이 없습니다."), TASK_STATUS_NOT_ALLOWED(HttpStatus.BAD_REQUEST, "TASK_011", "변경할 수 없는 작업 상태입니다. 다른 API를 사용해주세요"), - FILE_COUNT_EXCEEDED(HttpStatus.BAD_REQUEST, "TASK_012", "파일은 5개 이상 첨부 가능합니다.") + FILE_COUNT_EXCEEDED(HttpStatus.BAD_REQUEST, "TASK_012", "파일은 5개 이상 첨부 가능합니다."), + CATEGORY_DUPLICATE(HttpStatus.BAD_REQUEST, "TASK_013", "카테고리 이름 또는 코드는 중복될 수 없습니다.") ; private final HttpStatus httpStatus;