Skip to content

Commit

Permalink
Merge pull request #492 from TaskFlow-CLAP/CLAP-382
Browse files Browse the repository at this point in the history
CLAP-382 카테고리 이름, 코드 중복 체크 로직 수정
  • Loading branch information
joowojr authored Feb 11, 2025
2 parents e8d5e4c + 7dab99b commit b9dca9b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,17 @@ public List<Category> findSubCategory() {
}

@Override
public boolean existsByNameOrCode(String name, String code) {
return categoryRepository.existsByNameOrCodeAndIsDeletedFalse(name, code);
public boolean existsMainCategoryByNameOrCode(String name, String code) {
return categoryRepository.existsByNameOrCodeAndMainCategoryIsNullAndIsDeletedFalse(name, code);
}

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


@Override
public void save(final Category category) {
categoryRepository.save(categoryPersistenceMapper.toEntity(category));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

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

boolean existsByNameOrCodeAndIsDeletedFalse(String name, 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.name = :name OR c.code = :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 = :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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public interface LoadCategoryPort {
List<Category> findMainCategory();
List<Category> findSubCategory();

boolean existsByNameOrCode(String name, String code);
boolean existsMainCategoryByNameOrCode(String name, String code);

boolean existsSubCategoryByNameOrCode(Category category, 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.existsByNameOrCode(name, code)) throw new ApplicationException(CATEGORY_DUPLICATE);
if (loadCategoryPort.existsMainCategoryByNameOrCode(name, code)) throw new ApplicationException(CATEGORY_DUPLICATE);
Category mainCategory = Category.createMainCategory(
activeMember.orElseThrow(() -> new ApplicationException(ACTIVE_MEMBER_NOT_FOUND)),
code, name);
Expand All @@ -39,13 +39,11 @@ public void addMainCategory(Long adminId, String code, String name) {
@Override
@Transactional
public void addSubCategory(Long adminId, Long mainCategoryId, String code, String name, String descriptionExample) {
Optional<Member> activeMember = loadMemberPort.findActiveMemberById(adminId);
Optional<Category> 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)),
code, name, descriptionExample);
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);
Category subCategory = Category.createSubCategory(activeMember, mainCategory,code, name, descriptionExample);
commandCategoryPort.save(subCategory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ public class UpdateCategoryService implements UpdateCategoryUsecase {
@Transactional
public void updateCategory(Long adminId, Long categoryId, String name, String code, String descriptionExample) {
Member admin = loadMemberPort.findActiveMemberById(adminId).orElseThrow(() -> new ApplicationException(ACTIVE_MEMBER_NOT_FOUND));
if (loadCategoryPort.existsByNameOrCode(name, code)) throw new ApplicationException(CATEGORY_DUPLICATE);
Category category = loadCategoryPort.findById(categoryId)
.orElseThrow(() -> new ApplicationException(CATEGORY_NOT_FOUND));
Category category = loadCategoryPort.findById(categoryId).orElseThrow(() -> new ApplicationException(CATEGORY_NOT_FOUND));
boolean isDuplicate;
if (category.getMainCategory() == null) {
isDuplicate = loadCategoryPort.existsMainCategoryByNameOrCode(name, code);
} else {
isDuplicate = loadCategoryPort.existsSubCategoryByNameOrCode(category.getMainCategory(), name, code);
}
if (isDuplicate) throw new ApplicationException(CATEGORY_DUPLICATE);

category.updateCategory(admin, name, code, descriptionExample);
commandCategoryPort.save(category);
}
Expand Down

0 comments on commit b9dca9b

Please sign in to comment.