Skip to content

Commit

Permalink
Merge pull request #541 from TaskFlow-CLAP/CLAP-414
Browse files Browse the repository at this point in the history
CLAP-414 카테고리 수정시 중복 체크 오류 수정
  • Loading branch information
hyoseong-Choi authored Feb 13, 2025
2 parents 35cca2b + 3d620e9 commit 98952ba
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,22 @@ public List<Category> findSubCategory() {
}

@Override
public boolean existsMainCategoryByNameOrCode(String name, String code) {
return categoryRepository.existsByNameOrCodeAndMainCategoryIsNullAndIsDeletedFalse(name, code);
public boolean existsMainCategoryByNameOrCode(Category category, String name, String code) {
if (category == null) {
return categoryRepository.existsByNameOrCodeAndMainCategoryIsNullAndIsDeletedFalse(name, code);
}
CategoryEntity categoryEntity = categoryPersistenceMapper.toEntity(category);
return categoryRepository.existsByNameOrCodeAndMainCategoryIsNullAndIsDeletedFalse(categoryEntity, name, code);
}

@Override
public boolean existsSubCategoryByNameOrCode(Category category, String name, String code) {
public boolean existsSubCategoryByNameOrCode(Category category, Category mainCategory, String name, String code) {
CategoryEntity mainCategoryEntity = categoryPersistenceMapper.toEntity(mainCategory);
if (category == null) {
return categoryRepository.existsByMainCategoryAndIsDeletedFalseAndNameOrCode(mainCategoryEntity, name, code);
}
CategoryEntity categoryEntity = categoryPersistenceMapper.toEntity(category);
return categoryRepository.existsByMainCategoryAndIsDeletedFalseAndNameOrCode(categoryEntity, name, code);
return categoryRepository.existsByMainCategoryAndIsDeletedFalseAndNameOrCode(categoryEntity, mainCategoryEntity, name, code);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package clap.server.adapter.outbound.persistense.repository.task;

import clap.server.adapter.outbound.persistense.entity.task.CategoryEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -11,12 +12,28 @@
public interface CategoryRepository extends JpaRepository<CategoryEntity, Long> {

List<CategoryEntity> findByIsDeletedFalse();

List<CategoryEntity> findByIsDeletedFalseAndMainCategoryIsNull();

List<CategoryEntity> findByIsDeletedFalseAndMainCategoryIsNotNull();

@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)")
boolean existsByNameOrCodeAndMainCategoryIsNullAndIsDeletedFalse(@Param("name") String name, @Param("code") String code);
boolean existsByNameOrCodeAndMainCategoryIsNullAndIsDeletedFalse(@Param("name") String name,
@Param("code") String code);

@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)")
boolean existsByNameOrCodeAndMainCategoryIsNullAndIsDeletedFalse(@Param("category") CategoryEntity category,
@Param("name") String name,
@Param("code") String code);

@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)")
boolean existsByMainCategoryAndIsDeletedFalseAndNameOrCode(@Param("mainCategory")CategoryEntity mainCategory, @Param("name") String name, @Param("code") String code);
boolean existsByMainCategoryAndIsDeletedFalseAndNameOrCode(@Param("mainCategory") CategoryEntity mainCategory,
@Param("name") String name,
@Param("code") String code);

@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)")
boolean existsByMainCategoryAndIsDeletedFalseAndNameOrCode(@Param("category") CategoryEntity category,
@Param("mainCategory") CategoryEntity mainCategory,
@Param("name") String name,
@Param("code") String code);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface LoadCategoryPort {
List<Category> findMainCategory();
List<Category> findSubCategory();

boolean existsMainCategoryByNameOrCode(String name, String code);
boolean existsMainCategoryByNameOrCode(Category category, String name, String code);

boolean existsSubCategoryByNameOrCode(Category category, String name, String code);
boolean existsSubCategoryByNameOrCode(Category category, Category mainCategory, String name, String code);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class AddCategoryService implements AddMainCategoryUsecase, AddSubCategor
@Transactional
public void addMainCategory(Long adminId, String code, String name) {
Optional<Member> activeMember = loadMemberPort.findActiveMemberById(adminId);
if (loadCategoryPort.existsMainCategoryByNameOrCode(name, code)) throw new ApplicationException(CATEGORY_DUPLICATE);
if (loadCategoryPort.existsMainCategoryByNameOrCode(null, name, code)) throw new ApplicationException(CATEGORY_DUPLICATE);
Category mainCategory = Category.createMainCategory(
activeMember.orElseThrow(() -> new ApplicationException(ACTIVE_MEMBER_NOT_FOUND)),
code, name);
Expand All @@ -42,7 +42,7 @@ public void addSubCategory(Long adminId, Long mainCategoryId, String code, Strin
Member activeMember = loadMemberPort.findActiveMemberById(adminId).orElseThrow(() -> new ApplicationException(ACTIVE_MEMBER_NOT_FOUND));
Category mainCategory = loadCategoryPort.findById(mainCategoryId).orElseThrow(() -> new ApplicationException(CATEGORY_NOT_FOUND));

if (loadCategoryPort.existsSubCategoryByNameOrCode(mainCategory, name, code)) throw new ApplicationException(CATEGORY_DUPLICATE);
if (loadCategoryPort.existsSubCategoryByNameOrCode(null, mainCategory, name, code)) throw new ApplicationException(CATEGORY_DUPLICATE);
Category subCategory = Category.createSubCategory(activeMember, mainCategory,code, name, descriptionExample);
commandCategoryPort.save(subCategory);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public void updateCategory(Long adminId, Long categoryId, String name, String co
Category category = loadCategoryPort.findById(categoryId).orElseThrow(() -> new ApplicationException(CATEGORY_NOT_FOUND));
boolean isDuplicate;
if (category.getMainCategory() == null) {
isDuplicate = loadCategoryPort.existsMainCategoryByNameOrCode(name, code);
isDuplicate = loadCategoryPort.existsMainCategoryByNameOrCode(category, name, code);
} else {
isDuplicate = loadCategoryPort.existsSubCategoryByNameOrCode(category.getMainCategory(), name, code);
isDuplicate = loadCategoryPort.existsSubCategoryByNameOrCode(category, category.getMainCategory(), name, code);
}
if (isDuplicate) throw new ApplicationException(CATEGORY_DUPLICATE);

Expand Down

0 comments on commit 98952ba

Please sign in to comment.