Skip to content

Commit

Permalink
Refactor/190 pray (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
soonga00 authored Nov 22, 2024
2 parents d789f61 + e32c3a7 commit a3318f2
Show file tree
Hide file tree
Showing 22 changed files with 1,313 additions and 1,353 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 @@ -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 a3318f2

Please sign in to comment.