Skip to content

Commit 3d620e9

Browse files
committed
CLAP-414 Fix: 카테고리 수정시 중복 체크 오류 수정
<footer> - #538
1 parent 9b9cbc2 commit 3d620e9

File tree

5 files changed

+37
-12
lines changed

5 files changed

+37
-12
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,22 @@ public List<Category> findSubCategory() {
5050
}
5151

5252
@Override
53-
public boolean existsMainCategoryByNameOrCode(String name, String code) {
54-
return categoryRepository.existsByNameOrCodeAndMainCategoryIsNullAndIsDeletedFalse(name, code);
53+
public boolean existsMainCategoryByNameOrCode(Category category, String name, String code) {
54+
if (category == null) {
55+
return categoryRepository.existsByNameOrCodeAndMainCategoryIsNullAndIsDeletedFalse(name, code);
56+
}
57+
CategoryEntity categoryEntity = categoryPersistenceMapper.toEntity(category);
58+
return categoryRepository.existsByNameOrCodeAndMainCategoryIsNullAndIsDeletedFalse(categoryEntity, name, code);
5559
}
5660

5761
@Override
58-
public boolean existsSubCategoryByNameOrCode(Category category, String name, String code) {
62+
public boolean existsSubCategoryByNameOrCode(Category category, Category mainCategory, String name, String code) {
63+
CategoryEntity mainCategoryEntity = categoryPersistenceMapper.toEntity(mainCategory);
64+
if (category == null) {
65+
return categoryRepository.existsByMainCategoryAndIsDeletedFalseAndNameOrCode(mainCategoryEntity, name, code);
66+
}
5967
CategoryEntity categoryEntity = categoryPersistenceMapper.toEntity(category);
60-
return categoryRepository.existsByMainCategoryAndIsDeletedFalseAndNameOrCode(categoryEntity, name, code);
68+
return categoryRepository.existsByMainCategoryAndIsDeletedFalseAndNameOrCode(categoryEntity, mainCategoryEntity, name, code);
6169
}
6270

6371

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package clap.server.adapter.outbound.persistense.repository.task;
2+
23
import clap.server.adapter.outbound.persistense.entity.task.CategoryEntity;
34
import org.springframework.data.jpa.repository.JpaRepository;
45
import org.springframework.data.jpa.repository.Query;
@@ -11,12 +12,28 @@
1112
public interface CategoryRepository extends JpaRepository<CategoryEntity, Long> {
1213

1314
List<CategoryEntity> findByIsDeletedFalse();
15+
1416
List<CategoryEntity> findByIsDeletedFalseAndMainCategoryIsNull();
17+
1518
List<CategoryEntity> findByIsDeletedFalseAndMainCategoryIsNotNull();
1619

1720
@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);
21+
boolean existsByNameOrCodeAndMainCategoryIsNullAndIsDeletedFalse(@Param("name") String name,
22+
@Param("code") String code);
23+
24+
@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 != :category AND (c.name = :name OR c.code = :code)")
25+
boolean existsByNameOrCodeAndMainCategoryIsNullAndIsDeletedFalse(@Param("category") CategoryEntity category,
26+
@Param("name") String name,
27+
@Param("code") String code);
1928

2029
@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);
30+
boolean existsByMainCategoryAndIsDeletedFalseAndNameOrCode(@Param("mainCategory") CategoryEntity mainCategory,
31+
@Param("name") String name,
32+
@Param("code") String code);
33+
34+
@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 != :category AND (c.name = :name OR c.code = :code)")
35+
boolean existsByMainCategoryAndIsDeletedFalseAndNameOrCode(@Param("category") CategoryEntity category,
36+
@Param("mainCategory") CategoryEntity mainCategory,
37+
@Param("name") String name,
38+
@Param("code") String code);
2239
}

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

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

15-
boolean existsMainCategoryByNameOrCode(String name, String code);
15+
boolean existsMainCategoryByNameOrCode(Category category, String name, String code);
1616

17-
boolean existsSubCategoryByNameOrCode(Category category, String name, String code);
17+
boolean existsSubCategoryByNameOrCode(Category category, Category mainCategory, String name, String code);
1818
}

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

Lines changed: 2 additions & 2 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.existsMainCategoryByNameOrCode(name, code)) throw new ApplicationException(CATEGORY_DUPLICATE);
32+
if (loadCategoryPort.existsMainCategoryByNameOrCode(null, name, code)) throw new ApplicationException(CATEGORY_DUPLICATE);
3333
Category mainCategory = Category.createMainCategory(
3434
activeMember.orElseThrow(() -> new ApplicationException(ACTIVE_MEMBER_NOT_FOUND)),
3535
code, name);
@@ -42,7 +42,7 @@ public void addSubCategory(Long adminId, Long mainCategoryId, String code, Strin
4242
Member activeMember = loadMemberPort.findActiveMemberById(adminId).orElseThrow(() -> new ApplicationException(ACTIVE_MEMBER_NOT_FOUND));
4343
Category mainCategory = loadCategoryPort.findById(mainCategoryId).orElseThrow(() -> new ApplicationException(CATEGORY_NOT_FOUND));
4444

45-
if (loadCategoryPort.existsSubCategoryByNameOrCode(mainCategory, name, code)) throw new ApplicationException(CATEGORY_DUPLICATE);
45+
if (loadCategoryPort.existsSubCategoryByNameOrCode(null, mainCategory, name, code)) throw new ApplicationException(CATEGORY_DUPLICATE);
4646
Category subCategory = Category.createSubCategory(activeMember, mainCategory,code, name, descriptionExample);
4747
commandCategoryPort.save(subCategory);
4848
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public void updateCategory(Long adminId, Long categoryId, String name, String co
2929
Category category = loadCategoryPort.findById(categoryId).orElseThrow(() -> new ApplicationException(CATEGORY_NOT_FOUND));
3030
boolean isDuplicate;
3131
if (category.getMainCategory() == null) {
32-
isDuplicate = loadCategoryPort.existsMainCategoryByNameOrCode(name, code);
32+
isDuplicate = loadCategoryPort.existsMainCategoryByNameOrCode(category, name, code);
3333
} else {
34-
isDuplicate = loadCategoryPort.existsSubCategoryByNameOrCode(category.getMainCategory(), name, code);
34+
isDuplicate = loadCategoryPort.existsSubCategoryByNameOrCode(category, category.getMainCategory(), name, code);
3535
}
3636
if (isDuplicate) throw new ApplicationException(CATEGORY_DUPLICATE);
3737

0 commit comments

Comments
 (0)