Skip to content

Commit

Permalink
fix: session order by id desc
Browse files Browse the repository at this point in the history
  • Loading branch information
gracefulBrown committed Dec 27, 2024
1 parent 280fd5b commit 3d900f1
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 9 deletions.
1 change: 0 additions & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ COPY ./gradlew .
RUN ./gradlew clean build -x test -x acceptanceTest
RUN mv /work/build/libs/*[!-plain].jar ./app.jar


FROM eclipse-temurin:17-jre

COPY --from=build /work/app.jar .
Expand Down
1 change: 0 additions & 1 deletion backend/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ services:
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/prolog?useSSL=false&allowPublicKeyRetrieval=true&characterEncoding=UTF-8&serverTimezone=UTC
SPRING_FLYWAY_ENABLED: true

db:
platform: linux/x86_64
image: library/mysql:8.0.28
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,16 @@ public List<SessionResponse> findAll() {
.collect(toList());
}

public List<SessionResponse> findAllByOrderByIdDesc() {
return sessionRepository.findAllByOrderByIdDesc().stream()
.map(SessionResponse::of)
.collect(toList());
}

public List<SessionResponse> findMySessions(LoginMember member) {
List<Long> sessionIds = findMySessionIds(member.getId());

return sessionRepository.findAllById(sessionIds).stream()
return sessionRepository.findAllByIdInOrderByIdDesc(sessionIds).stream()
.map(SessionResponse::of)
.collect(toList());
}
Expand All @@ -76,11 +82,11 @@ public List<Long> findMySessionIds(Long memberId) {

public List<SessionResponse> findAllWithMySessionFirst(LoginMember loginMember) {
if (loginMember.isAnonymous()) {
return findAll();
return findAllByOrderByIdDesc();
}

List<SessionResponse> mySessions = findMySessions(loginMember);
List<SessionResponse> allSessions = findAll();
List<SessionResponse> allSessions = findAllByOrderByIdDesc();
allSessions.removeAll(mySessions);

return Stream.of(mySessions, allSessions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ public interface SessionRepository extends JpaRepository<Session, Long> {
Optional<Session> findByName(String name);

List<Session> findAllByCurriculumId(Long curriculumId);

List<Session> findAllByOrderByIdDesc();

List<Session> findAllByIdInOrderByIdDesc(List<Long> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ public class FilterService {
private final MissionService missionService;

public FilterResponse showAll(LoginMember loginMember) {
List<SessionResponse> sessionResponses = sessionService.findAllWithMySessionFirst(
loginMember); // my session으로 대체
List<MissionResponse> missionResponses = missionService.findAllWithMyMissionFirst(
loginMember);
List<SessionResponse> sessionResponses = sessionService.findAllWithMySessionFirst(loginMember);
List<MissionResponse> missionResponses = missionService.findAllWithMyMissionFirst(loginMember);
return new FilterResponse(sessionResponses, missionResponses);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package wooteco.prolog.session.domain.repository;

import static org.assertj.core.api.Assertions.assertThat;

import com.google.common.collect.Lists;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import wooteco.prolog.session.domain.Session;
import wooteco.support.utils.RepositoryTest;

@RepositoryTest
class SessionRepositoryTest {

@Autowired
private SessionRepository sessionRepository;

@Test
void findAllOrderByIdDesc() {
sessionRepository.save(new Session("name1"));
sessionRepository.save(new Session("name2"));

List<Session> sessions = sessionRepository.findAllByOrderByIdDesc();

assertThat(sessions.get(0).getName()).isEqualTo("name2");
assertThat(sessions.get(1).getName()).isEqualTo("name1");
}

@Test
void findAllByIdInOrderByIdDesc() {
Session session1 = sessionRepository.save(new Session("name1"));
Session session2 = sessionRepository.save(new Session("name2"));
Session session3 = sessionRepository.save(new Session("name3"));
Session session4 = sessionRepository.save(new Session("name4"));

List<Session> sessions = sessionRepository.findAllByIdInOrderByIdDesc(
Lists.newArrayList(session4.getId(), session2.getId()));

assertThat(sessions.get(0).getName()).isEqualTo("name4");
assertThat(sessions.get(1).getName()).isEqualTo("name2");
}
}

0 comments on commit 3d900f1

Please sign in to comment.