Skip to content

Commit b9dca9b

Browse files
authored
Merge pull request #492 from TaskFlow-CLAP/CLAP-382
CLAP-382 카테고리 이름, 코드 중복 체크 로직 수정
2 parents e8d5e4c + 7dab99b commit b9dca9b

File tree

5 files changed

+34
-15
lines changed

5 files changed

+34
-15
lines changed

src/main/java/clap/server/adapter/outbound/persistense/CategoryPersistenceAdapter.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,17 @@ public List<Category> findSubCategory() {
5050
}
5151

5252
@Override
53-
public boolean existsByNameOrCode(String name, String code) {
54-
return categoryRepository.existsByNameOrCodeAndIsDeletedFalse(name, code);
53+
public boolean existsMainCategoryByNameOrCode(String name, String code) {
54+
return categoryRepository.existsByNameOrCodeAndMainCategoryIsNullAndIsDeletedFalse(name, code);
5555
}
5656

57+
@Override
58+
public boolean existsSubCategoryByNameOrCode(Category category, String name, String code) {
59+
CategoryEntity categoryEntity = categoryPersistenceMapper.toEntity(category);
60+
return categoryRepository.existsByMainCategoryAndIsDeletedFalseAndNameOrCode(categoryEntity, name, code);
61+
}
62+
63+
5764
@Override
5865
public void save(final Category category) {
5966
categoryRepository.save(categoryPersistenceMapper.toEntity(category));

src/main/java/clap/server/adapter/outbound/persistense/repository/task/CategoryRepository.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package clap.server.adapter.outbound.persistense.repository.task;
22
import clap.server.adapter.outbound.persistense.entity.task.CategoryEntity;
33
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.data.jpa.repository.Query;
5+
import org.springframework.data.repository.query.Param;
46
import org.springframework.stereotype.Repository;
57

68
import java.util.List;
@@ -12,5 +14,9 @@ public interface CategoryRepository extends JpaRepository<CategoryEntity, Long>
1214
List<CategoryEntity> findByIsDeletedFalseAndMainCategoryIsNull();
1315
List<CategoryEntity> findByIsDeletedFalseAndMainCategoryIsNotNull();
1416

15-
boolean existsByNameOrCodeAndIsDeletedFalse(String name, String code);
17+
@Query("SELECT CASE WHEN COUNT(c) > 0 THEN true ELSE false END FROM CategoryEntity c WHERE c.mainCategory IS NULL AND c.isDeleted = false AND (c.name = :name OR c.code = :code)")
18+
boolean existsByNameOrCodeAndMainCategoryIsNullAndIsDeletedFalse(@Param("name") String name, @Param("code") String code);
19+
20+
@Query("SELECT CASE WHEN COUNT(c) > 0 THEN true ELSE false END FROM CategoryEntity c WHERE c.mainCategory = :mainCategory AND c.isDeleted = false AND (c.name = :name OR c.code = :code)")
21+
boolean existsByMainCategoryAndIsDeletedFalseAndNameOrCode(@Param("mainCategory")CategoryEntity mainCategory, @Param("name") String name, @Param("code") String code);
1622
}

src/main/java/clap/server/application/port/outbound/task/LoadCategoryPort.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ public interface LoadCategoryPort {
1212
List<Category> findMainCategory();
1313
List<Category> findSubCategory();
1414

15-
boolean existsByNameOrCode(String name, String code);
15+
boolean existsMainCategoryByNameOrCode(String name, String code);
16+
17+
boolean existsSubCategoryByNameOrCode(Category category, String name, String code);
1618
}

src/main/java/clap/server/application/service/admin/AddCategoryService.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class AddCategoryService implements AddMainCategoryUsecase, AddSubCategor
2929
@Transactional
3030
public void addMainCategory(Long adminId, String code, String name) {
3131
Optional<Member> activeMember = loadMemberPort.findActiveMemberById(adminId);
32-
if (loadCategoryPort.existsByNameOrCode(name, code)) throw new ApplicationException(CATEGORY_DUPLICATE);
32+
if (loadCategoryPort.existsMainCategoryByNameOrCode(name, code)) throw new ApplicationException(CATEGORY_DUPLICATE);
3333
Category mainCategory = Category.createMainCategory(
3434
activeMember.orElseThrow(() -> new ApplicationException(ACTIVE_MEMBER_NOT_FOUND)),
3535
code, name);
@@ -39,13 +39,11 @@ public void addMainCategory(Long adminId, String code, String name) {
3939
@Override
4040
@Transactional
4141
public void addSubCategory(Long adminId, Long mainCategoryId, String code, String name, String descriptionExample) {
42-
Optional<Member> activeMember = loadMemberPort.findActiveMemberById(adminId);
43-
Optional<Category> mainCategory = loadCategoryPort.findById(mainCategoryId);
44-
if (loadCategoryPort.existsByNameOrCode(name, code)) throw new ApplicationException(CATEGORY_DUPLICATE);
45-
Category subCategory = Category.createSubCategory(
46-
activeMember.orElseThrow(() -> new ApplicationException(ACTIVE_MEMBER_NOT_FOUND)),
47-
mainCategory.orElseThrow(() -> new ApplicationException(CATEGORY_NOT_FOUND)),
48-
code, name, descriptionExample);
42+
Member activeMember = loadMemberPort.findActiveMemberById(adminId).orElseThrow(() -> new ApplicationException(ACTIVE_MEMBER_NOT_FOUND));
43+
Category mainCategory = loadCategoryPort.findById(mainCategoryId).orElseThrow(() -> new ApplicationException(CATEGORY_NOT_FOUND));
44+
45+
if (loadCategoryPort.existsSubCategoryByNameOrCode(mainCategory, name, code)) throw new ApplicationException(CATEGORY_DUPLICATE);
46+
Category subCategory = Category.createSubCategory(activeMember, mainCategory,code, name, descriptionExample);
4947
commandCategoryPort.save(subCategory);
5048
}
5149
}

src/main/java/clap/server/application/service/admin/UpdateCategoryService.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,15 @@ public class UpdateCategoryService implements UpdateCategoryUsecase {
2626
@Transactional
2727
public void updateCategory(Long adminId, Long categoryId, String name, String code, String descriptionExample) {
2828
Member admin = loadMemberPort.findActiveMemberById(adminId).orElseThrow(() -> new ApplicationException(ACTIVE_MEMBER_NOT_FOUND));
29-
if (loadCategoryPort.existsByNameOrCode(name, code)) throw new ApplicationException(CATEGORY_DUPLICATE);
30-
Category category = loadCategoryPort.findById(categoryId)
31-
.orElseThrow(() -> new ApplicationException(CATEGORY_NOT_FOUND));
29+
Category category = loadCategoryPort.findById(categoryId).orElseThrow(() -> new ApplicationException(CATEGORY_NOT_FOUND));
30+
boolean isDuplicate;
31+
if (category.getMainCategory() == null) {
32+
isDuplicate = loadCategoryPort.existsMainCategoryByNameOrCode(name, code);
33+
} else {
34+
isDuplicate = loadCategoryPort.existsSubCategoryByNameOrCode(category.getMainCategory(), name, code);
35+
}
36+
if (isDuplicate) throw new ApplicationException(CATEGORY_DUPLICATE);
37+
3238
category.updateCategory(admin, name, code, descriptionExample);
3339
commandCategoryPort.save(category);
3440
}

0 commit comments

Comments
 (0)