Skip to content

Commit

Permalink
Feat: Category 저장 시 순서 저장 로직 추가 (#41)
Browse files Browse the repository at this point in the history
* Feat: Category 저장 시 순서 저장 로직 추가

* Feat: CategoryCustomRepository 추가

* Feat: parentCategory 저장 로직 추가

* Refactor: 불필요한 주석 제거

* Feat: position 저장 로직 추가

* Refactor: Optional 사용

* Fix: 쿼리 수정
  • Loading branch information
ChoiSangok authored Sep 2, 2023
1 parent 94a3ce3 commit 2b3c7b6
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package kdkd.youre.backend.domain.category.domain.repository;

import kdkd.youre.backend.domain.category.domain.Category;
import kdkd.youre.backend.domain.member.domain.Member;

public interface CategoryCustomRepository {
Long findMaxPositionByMember(Member member);
Long findMaxPositionForMemberAndParent(Member member, Category parent);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import kdkd.youre.backend.domain.member.domain.Member;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CategoryRepository extends JpaRepository<Category, Long> {
Boolean existsByNameAndMember(String name, Member member);
public interface CategoryRepository extends JpaRepository<Category, Long>, CategoryCustomRepository{
Boolean existsByNameAndMember(String name, Member member);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package kdkd.youre.backend.domain.category.domain.repository;

import com.querydsl.jpa.impl.JPAQueryFactory;
import kdkd.youre.backend.domain.category.domain.Category;
import kdkd.youre.backend.domain.member.domain.Member;
import lombok.RequiredArgsConstructor;

import static kdkd.youre.backend.domain.category.domain.QCategory.category;

@RequiredArgsConstructor
public class CategoryRepositoryImpl implements CategoryCustomRepository{
private final JPAQueryFactory queryFactory;

@Override
public Long findMaxPositionByMember(Member member) {
return queryFactory
.select(category.position.max())
.from(category)
.where(
category.member.eq(member).and(category.parent.isNull())
)
.fetchOne();
}

@Override
public Long findMaxPositionForMemberAndParent(Member member, Category parent) {
return queryFactory
.select(category.position.max())
.from(category)
.where(
category.member.eq(member).and(category.parent.eq(parent))
)
.fetchOne();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class CategoryService {

public IdResponse saveCategory(CategorySaveRequest request, Member member) {

//카테고리 중복체크
checkDuplicateByName(request.getName(), member);

Category parentCategory = Optional.ofNullable(request.getParentId())
Expand All @@ -36,11 +35,28 @@ public IdResponse saveCategory(CategorySaveRequest request, Member member) {
.map(parent -> parent.getChildFullName(request.getName()))
.orElse(request.getName());

//TODO: 추후 도메인 로직으로 분리 예정
Long position = Optional.ofNullable(parentCategory)
.map(p -> categoryRepository.findMaxPositionForMemberAndParent(member, p))
.orElseGet(() -> categoryRepository.findMaxPositionByMember(member));

Long newPosition = Optional.ofNullable(position)
.map(p -> (p / 10000L + 1) * 10000L)
.orElse(10000L);

Long depth = Optional.ofNullable(parentCategory)
.map(Category::getDepth)
.map(d -> d + 1)
.orElse(1L);

Category category = Category.builder()
.name(request.getName())
.fullName(fullName)
.parent(parentCategory)
.member(member)
.depth(depth)
.isBookmarked(false)
.position(newPosition)
.build();

categoryRepository.save(category);
Expand Down

0 comments on commit 2b3c7b6

Please sign in to comment.