Skip to content

Commit

Permalink
Merge branch 'main' into refactor/history-controller
Browse files Browse the repository at this point in the history
  • Loading branch information
200516bb authored Nov 29, 2024
2 parents c648f89 + a3318f2 commit e3627a3
Show file tree
Hide file tree
Showing 43 changed files with 1,590 additions and 1,558 deletions.
399 changes: 199 additions & 200 deletions src/main/java/com/uspray/uspray/InitDb.java

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/main/java/com/uspray/uspray/UsprayApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableJpaAuditing
@EnableFeignClients
@EnableConfigurationProperties
@EnableScheduling
public class UsprayApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public ApiResponseDto<CategoryResponseDto> deleteCategory(
@PathVariable("categoryId") Long categoryId
) {
return ApiResponseDto.success(SuccessStatus.DELETE_CATEGORY_SUCCESS,
prayFacade.deleteCategory(user.getUsername(), categoryId));
prayFacade.deleteCategory(categoryId));
}

@PutMapping("/{categoryId}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.uspray.uspray.domain.category.model;

import com.uspray.uspray.domain.category.dto.CategoryRequestDto;
import com.uspray.uspray.global.enums.CategoryType;
import com.uspray.uspray.global.common.model.AuditingTimeEntity;
import com.uspray.uspray.domain.member.model.Member;
import com.uspray.uspray.domain.pray.model.Pray;
import java.util.List;
import com.uspray.uspray.global.common.model.AuditingTimeEntity;
import com.uspray.uspray.global.enums.CategoryType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
Expand All @@ -15,7 +13,6 @@
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.Builder;
Expand Down Expand Up @@ -48,9 +45,6 @@ public class Category extends AuditingTimeEntity {
@Enumerated(EnumType.STRING)
private CategoryType categoryType;

@OneToMany(mappedBy = "category")
private List<Pray> prays;

@Builder
public Category(
Long id,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.uspray.uspray.domain.category.repository;

import com.uspray.uspray.global.enums.CategoryType;
import com.uspray.uspray.domain.category.model.Category;
import com.uspray.uspray.domain.category.repository.querydsl.CategoryRepositoryCustom;
import com.uspray.uspray.domain.member.model.Member;
import com.uspray.uspray.global.enums.CategoryType;
import com.uspray.uspray.global.exception.ErrorStatus;
import com.uspray.uspray.global.exception.model.CustomException;
import com.uspray.uspray.global.exception.model.NotFoundException;
import com.uspray.uspray.domain.category.repository.querydsl.CategoryRepositoryCustom;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -17,26 +17,10 @@ public interface CategoryRepository extends JpaRepository<Category, Long>,

Category getCategoryById(Long categoryId);

List<Category> getCategoriesByMemberOrderByOrder(Member member);

List<Category> getCategoriesByMemberAndCategoryTypeOrderByOrder(Member member,
List<Category> findAllByMemberAndCategoryTypeOrderByOrderAsc(Member member,
CategoryType categoryType);

List<Category> findAllByMemberAndCategoryTypeOrderByOrderAsc(Member member, CategoryType categoryType);

default Category getCategoryByIdAndMember(Long categoryId, Member member) {
Category category = findById(categoryId)
.filter(c -> c.getMember().equals(member))
.orElseThrow(() -> new NotFoundException(
ErrorStatus.CATEGORY_NOT_FOUND_EXCEPTION
));

if (!category.getCategoryType().equals(CategoryType.PERSONAL)) {
throw new CustomException(ErrorStatus.PRAY_CATEGORY_TYPE_MISMATCH);
}

return category;
}
Optional<Category> findByIdAndMemberAndCategoryType(Long categoryId, Member member, CategoryType categoryType);

default void checkDuplicate(String name, Member member, CategoryType type) {
if (existsByNameAndMemberAndCategoryType(name, member, type)) {
Expand All @@ -47,7 +31,7 @@ default void checkDuplicate(String name, Member member, CategoryType type) {

default int checkDuplicateAndReturnMaxOrder(String name, Member member, CategoryType type) {
checkDuplicate(name, member, type);
if (getCategoriesByMemberAndCategoryTypeOrderByOrder(member, type).isEmpty()) {
if (findAllByMemberAndCategoryTypeOrderByOrderAsc(member, type).isEmpty()) {
return 0;
}
return getMaxCategoryOrder(member, type);
Expand All @@ -56,7 +40,7 @@ default int checkDuplicateAndReturnMaxOrder(String name, Member member, Category
boolean existsByNameAndMemberAndCategoryType(String name, Member member, CategoryType type);

default int getMaxCategoryOrder(Member member, CategoryType type) {
Category category = getCategoriesByMemberAndCategoryTypeOrderByOrder(member, type).stream()
Category category = findAllByMemberAndCategoryTypeOrderByOrderAsc(member, type).stream()
.reduce((first, second) -> second)
.orElseThrow(() -> new NotFoundException(ErrorStatus.CATEGORY_NOT_FOUND_EXCEPTION));
return category.getOrder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

public interface CategoryRepositoryCustom {

List<PrayListResponseDto> findAllWithOrderAndType(String username, String prayType);

List<PrayListResponseDto> findAllWithOrderAndType(String username, String prayType, List<Long> prayIds);
List<PrayListResponseDto> findAllWithOrderAndType(String username, String prayType,
List<Long> prayIds);
}
Original file line number Diff line number Diff line change
@@ -1,111 +1,61 @@
package com.uspray.uspray.domain.category.repository.querydsl;

import static com.uspray.uspray.domain.category.model.QCategory.category;
import static com.uspray.uspray.domain.member.model.QMember.member;
import static com.uspray.uspray.domain.pray.model.QPray.pray;

import com.querydsl.jpa.impl.JPAQueryFactory;
import com.uspray.uspray.domain.category.model.Category;
import com.uspray.uspray.domain.member.model.Member;
import com.uspray.uspray.domain.pray.dto.pray.PrayListResponseDto;
import com.uspray.uspray.domain.pray.dto.pray.response.PrayResponseDto;
import com.uspray.uspray.domain.pray.dto.pray.response.QPrayResponseDto;
import com.uspray.uspray.domain.pray.model.Pray;
import com.uspray.uspray.global.enums.PrayType;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

@Repository
@RequiredArgsConstructor
public class CategoryRepositoryImpl implements CategoryRepositoryCustom {

private final JPAQueryFactory queryFactory;

@Override
public List<PrayListResponseDto> findAllWithOrderAndType(String username, String prayType) {
// 카테고리 목록 가져오기
List<Category> categories = queryFactory
.selectFrom(category)
.where(category.member.userId.eq(username))
.where(category.categoryType.stringValue().likeIgnoreCase(prayType))
.orderBy(category.order.asc())
.fetch();

if (categories.isEmpty()) {
return new ArrayList<>();
}

// 각 카테고리 별로 PrayResponseDto 목록 가져오기
List<PrayListResponseDto> prayListResponseDtos = new ArrayList<>();
for (Category cat : categories) {
List<Pray> prays = queryFactory
.selectFrom(pray)
.where(pray.category.id.eq(cat.getId())
.and(pray.member.userId.eq(username))
.and(pray.prayType.stringValue().likeIgnoreCase(prayType)))
.orderBy(pray.createdAt.asc())
.fetch();

List<PrayResponseDto> prayResponseDtos = prays.stream()
.map(pray_iter -> {
if (pray_iter.getPrayType().equals(PrayType.SHARED)) {
Member originMember = queryFactory
.selectFrom(member)
.where(member.id.eq(pray_iter.getOriginMemberId()))
.fetchOne();
return PrayResponseDto.shared(pray_iter, originMember);
} else {
return PrayResponseDto.of(pray_iter);
}
})
.collect(Collectors.toList());

prayListResponseDtos.add(
new PrayListResponseDto(cat.getId(), cat.getName(), cat.getColor(),
prayResponseDtos));
}
return prayListResponseDtos;
}

@Override
public List<PrayListResponseDto> findAllWithOrderAndType(String username, String prayType,
List<Long> prayIds) {
List<Category> categories = queryFactory
.selectFrom(category)
.where(category.member.userId.eq(username))
.where(category.categoryType.stringValue().likeIgnoreCase(prayType))
.orderBy(category.order.asc())
.fetch();

// 각 카테고리 별로 PrayResponseDto 목록 가져오기
List<PrayListResponseDto> prayListResponseDtos = new ArrayList<>();
for (Category cat : categories) {
List<PrayResponseDto> prayResponseDtos = queryFactory
.select(new QPrayResponseDto(
pray.id,
pray.content,
pray.member.name,
pray.deadline,
pray.category.id,
pray.category.name,
pray.lastPrayedAt,
pray.isShared
))
.from(pray)
.where(pray.category.id.eq(cat.getId())
.and(pray.member.userId.eq(username))
.and(pray.prayType.stringValue().likeIgnoreCase(prayType)))
.fetch();
prayResponseDtos.removeIf(p -> !prayIds.contains(p.getPrayId()));
prayResponseDtos.forEach(p -> p.setInGroup(true));

prayListResponseDtos.add(
new PrayListResponseDto(cat.getId(), cat.getName(), cat.getColor(),
prayResponseDtos));
}
return prayListResponseDtos;
}
private final JPAQueryFactory queryFactory;


@Override
public List<PrayListResponseDto> findAllWithOrderAndType(String username, String prayType,
List<Long> prayIds) {
List<Category> categories = queryFactory
.selectFrom(category)
.where(category.member.userId.eq(username))
.where(category.categoryType.stringValue().likeIgnoreCase(prayType))
.orderBy(category.order.asc())
.fetch();

// 각 카테고리 별로 PrayResponseDto 목록 가져오기
List<PrayListResponseDto> prayListResponseDtos = new ArrayList<>();
for (Category cat : categories) {
List<PrayResponseDto> prayResponseDtos = queryFactory
.select(new QPrayResponseDto(
pray.id,
pray.content,
pray.member.name,
pray.deadline,
pray.category.id,
pray.category.name,
pray.lastPrayedAt,
pray.isShared
))
.from(pray)
.where(pray.category.id.eq(cat.getId())
.and(pray.member.userId.eq(username))
.and(pray.categoryType.stringValue().likeIgnoreCase(prayType)))
.fetch();
prayResponseDtos.removeIf(p -> !prayIds.contains(p.getPrayId()));
prayResponseDtos.forEach(p -> p.setInGroup(true));

prayListResponseDtos.add(
new PrayListResponseDto(cat.getId(), cat.getName(), cat.getColor(),
prayResponseDtos));
}
return prayListResponseDtos;
}
}
Loading

0 comments on commit e3627a3

Please sign in to comment.