Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/category #47

Merged
merged 14 commits into from
Nov 7, 2023
20 changes: 20 additions & 0 deletions src/main/java/com/uspray/uspray/DTO/pray/PrayListResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.uspray.uspray.DTO.pray;

import com.uspray.uspray.DTO.pray.request.PrayResponseDto;
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@AllArgsConstructor
@Builder
@NoArgsConstructor
public class PrayListResponseDto {

@Schema(description = "카테고리 ID", example = "1")
private Long categoryId;
private List<PrayResponseDto> prays;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import com.uspray.uspray.DTO.ApiResponseDto;
import com.uspray.uspray.DTO.pray.PrayListResponseDto;
import com.uspray.uspray.DTO.pray.request.PrayRequestDto;
import com.uspray.uspray.DTO.pray.request.PrayResponseDto;
import com.uspray.uspray.exception.SuccessStatus;
Expand Down Expand Up @@ -46,7 +47,7 @@ public class PrayController {
content = @Content(schema = @Schema(implementation = PrayResponseDto.class)))

@GetMapping()
public ApiResponseDto<List<PrayResponseDto>> getPrayList(
public ApiResponseDto<List<PrayListResponseDto>> getPrayList(
@Parameter(hidden = true) @AuthenticationPrincipal User user,
@Parameter(description = "정렬 기준 (date, count)", required = true, example = "date") String orderType
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.uspray.uspray.infrastructure.querydsl;

import static com.uspray.uspray.domain.QCategory.category;
import static com.uspray.uspray.domain.QPray.pray;

import com.querydsl.jpa.impl.JPAQueryFactory;
Expand All @@ -17,9 +18,11 @@ public class PrayRepositoryImpl implements PrayRepositoryCustom {

@Override
public List<Pray> findAllWithOrder(String orderType, String username) {
return queryFactory.
selectFrom(pray)
.where(pray.member.userId.eq(username), pray.deleted.eq(false))
return queryFactory
.select(pray)
.from(pray)
.join(pray.category, category)
.where(category.member.userId.eq(username))
.orderBy(orderType.equals("date") ? pray.createdAt.desc() : pray.count.asc())
.fetch();
}
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/com/uspray/uspray/service/PrayService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.uspray.uspray.service;

import com.uspray.uspray.DTO.pray.PrayListResponseDto;
import com.uspray.uspray.DTO.pray.request.PrayRequestDto;
import com.uspray.uspray.DTO.pray.request.PrayResponseDto;
import com.uspray.uspray.domain.Category;
Expand All @@ -11,6 +12,7 @@
import com.uspray.uspray.infrastructure.MemberRepository;
import com.uspray.uspray.infrastructure.PrayRepository;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.transaction.Transactional;
Expand Down Expand Up @@ -80,9 +82,19 @@ public PrayResponseDto updatePray(Long prayId, String username, PrayRequestDto p
}

@Transactional
public List<PrayResponseDto> getPrayList(String username, String orderType) {
return prayRepository.findAllWithOrder(orderType, username).stream()
.map(PrayResponseDto::of)
public List<PrayListResponseDto> getPrayList(String username, String orderType) {
List<Pray> prays = prayRepository.findAllWithOrder(orderType, username);

// Pray 엔티티를 categoryId를 기준으로 그룹화한 맵 생성
Map<Long, List<Pray>> prayMap = prays.stream()
.collect(Collectors.groupingBy(pray -> pray.getCategory().getId()));

// 그룹화된 맵을 PrayListResponseDto 변환하여 반환
return prayMap.entrySet().stream()
.map(entry -> new PrayListResponseDto(entry.getKey(),
entry.getValue().stream()
.map(PrayResponseDto::of)
.collect(Collectors.toList())))
.collect(Collectors.toList());
baebae02 marked this conversation as resolved.
Show resolved Hide resolved
}
}