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

[refactor] 특정 모임 약속 목록 반환 querydsl 변환 #158

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
public record PromisesDto(
List<PromiseDto> promises
) {
public static PromisesDto of(List<Promise> promises, Boolean done) {
public static PromisesDto of(List<Promise> promises) {
return new PromisesDto(
promises.stream()
.filter(promise -> done == null || done.equals(promise.isCompleted()))
.map(PromisesDto.PromiseDto::from)
.toList()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,9 @@ public PromisesDto getPromises(
final Boolean done,
final Boolean isParticipant
) {
List<Promise> allPromises = promiseRetriever.findAllByMeetingId(meetingId);
List<Promise> userPromises = promiseRetriever.findPromiseByUserIdAndMeetingId(userId, meetingId);
List<Promise> promises;

if (Boolean.TRUE.equals(isParticipant)) {
promises = userPromises;
} else if (Boolean.FALSE.equals(isParticipant)) {
promises = allPromises.stream()
.filter(promise -> !userPromises.contains(promise))
.collect(Collectors.toList());
} else {
promises = allPromises;
}
List<Promise> promises = promiseRetriever.findPromisesByConditions(userId, meetingId, done, isParticipant);

return PromisesDto.of(promises, done);
return PromisesDto.of(promises);
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import lombok.NoArgsConstructor;
import org.kkumulkkum.server.domain.base.BaseTimeEntity;
import org.kkumulkkum.server.domain.meeting.Meeting;
import org.kkumulkkum.server.domain.participant.Participant;

import java.time.LocalDateTime;
import java.util.List;

@Entity
@Getter
Expand Down Expand Up @@ -44,6 +46,9 @@ public class Promise extends BaseTimeEntity {
@JoinColumn(name = "meeting_id")
private Meeting meeting;

@OneToMany(mappedBy = "promise")
private List<Participant> participants;

@Builder
public Promise(
String name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ public boolean existsByArrivedAtIsNull(final Long promiseId) {
return promiseRepository.existsByArrivedAtIsNull(promiseId);
}

public List<Promise> findPromiseByUserIdAndMeetingId(final Long userId, final Long meetingId) {
return promiseRepository.findPromiseByUserIdAndMeetingId(userId, meetingId);
}

public Promise findByUserIdAndPromiseId(final Long userId, final Long promiseId) {
return promiseRepository.findByUserIdAndPromiseId(userId, promiseId);
}

public List<Promise> findPromisesByConditions(final Long userId, final Long meetingId, final Boolean done, final Boolean isParticipant) {
return promiseRepository.findPromiseByConditions(userId, meetingId, done, isParticipant);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.kkumulkkum.server.domain.promise.repository;

import org.kkumulkkum.server.domain.promise.Promise;
import org.kkumulkkum.server.domain.promise.repository.custom.PromiseRepositoryCustom;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -9,7 +10,7 @@
import java.time.LocalDateTime;
import java.util.List;

public interface PromiseRepository extends JpaRepository<Promise, Long> {
public interface PromiseRepository extends JpaRepository<Promise, Long>, PromiseRepositoryCustom {

List<Promise> findAllByMeetingIdOrderByTimeAscCreatedAtAsc(Long meetingId);

Expand Down Expand Up @@ -52,15 +53,6 @@ public interface PromiseRepository extends JpaRepository<Promise, Long> {
THEN TRUE ELSE FALSE END FROM Participant p""")
boolean existsByArrivedAtIsNull(Long promiseId);

@Query("""
SELECT p FROM Participant pt
JOIN pt.member m
JOIN pt.promise p
WHERE p.meeting.id = :meetingId
AND m.user.id = :userId
ORDER BY p.time ASC, p.createdAt ASC""")
List<Promise> findPromiseByUserIdAndMeetingId(Long userId, Long meetingId);

@Query("""
SELECT p FROM Participant pt
JOIN pt.member m
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.kkumulkkum.server.domain.promise.repository.custom;

import org.kkumulkkum.server.domain.promise.Promise;

import java.util.List;

public interface PromiseRepositoryCustom {
List<Promise> findPromiseByConditions(Long userId, Long meetingId, Boolean done, Boolean isParticipant);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.kkumulkkum.server.domain.promise.repository.custom;

import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.kkumulkkum.server.domain.promise.Promise;
import org.springframework.stereotype.Repository;

import java.util.List;

import static org.kkumulkkum.server.domain.member.QMember.member;
import static org.kkumulkkum.server.domain.participant.QParticipant.participant;
import static org.kkumulkkum.server.domain.promise.QPromise.promise;

@Repository
@RequiredArgsConstructor
public class PromiseRepositoryCustomImpl implements PromiseRepositoryCustom {

private final JPAQueryFactory queryFactory;

@Override
public List<Promise> findPromiseByConditions(Long userId, Long meetingId, Boolean done, Boolean isParticipant) {
return queryFactory
.selectFrom(promise)
.leftJoin(promise.participants, participant).fetchJoin()
.leftJoin(participant.member, member).fetchJoin()
.where(
meetingIdEq(meetingId),
isParticipantEq(userId, meetingId, isParticipant),
doneEq(done)
)
.orderBy(promise.time.asc(), promise.createdAt.asc())
.fetch();
}

private BooleanExpression meetingIdEq(Long meetingId) {
return meetingId != null ? promise.meeting.id.eq(meetingId) : null;
}

private BooleanExpression isParticipantEq(Long userId, Long meetingId, Boolean isParticipant) {
if (isParticipant == null) return null;

if (isParticipant) {
return promise.id.in(
JPAExpressions.select(participant.promise.id)
.from(participant)
.join(participant.member, member)
.where(member.user.id.eq(userId),
participant.promise.meeting.id.eq(meetingId)
)
);
} else {
return promise.id.notIn(
JPAExpressions.select(participant.promise.id)
.from(participant)
.join(participant.member, member)
.where(member.user.id.eq(userId),
participant.promise.meeting.id.eq(meetingId)
)
);
}
}

private BooleanExpression doneEq(Boolean done) {
return done != null ? promise.isCompleted.eq(done) : null;
}

}