From 3d900f126358e385419ad1e410ad27cee04276a6 Mon Sep 17 00:00:00 2001 From: gracefulBrown Date: Fri, 27 Dec 2024 18:38:05 +0900 Subject: [PATCH] fix: session order by id desc --- backend/Dockerfile | 1 - backend/docker/docker-compose.yml | 1 - .../session/application/SessionService.java | 12 ++++-- .../domain/repository/SessionRepository.java | 4 ++ .../studylog/application/FilterService.java | 6 +-- .../repository/SessionRepositoryTest.java | 42 +++++++++++++++++++ 6 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 backend/src/test/java/wooteco/prolog/session/domain/repository/SessionRepositoryTest.java diff --git a/backend/Dockerfile b/backend/Dockerfile index 0e8a3e4e2..fc2d4ec31 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -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 . diff --git a/backend/docker/docker-compose.yml b/backend/docker/docker-compose.yml index 9f75fe6e3..2c21a4ad5 100644 --- a/backend/docker/docker-compose.yml +++ b/backend/docker/docker-compose.yml @@ -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 diff --git a/backend/src/main/java/wooteco/prolog/session/application/SessionService.java b/backend/src/main/java/wooteco/prolog/session/application/SessionService.java index 7d0592ed9..9f82f7509 100644 --- a/backend/src/main/java/wooteco/prolog/session/application/SessionService.java +++ b/backend/src/main/java/wooteco/prolog/session/application/SessionService.java @@ -59,10 +59,16 @@ public List findAll() { .collect(toList()); } + public List findAllByOrderByIdDesc() { + return sessionRepository.findAllByOrderByIdDesc().stream() + .map(SessionResponse::of) + .collect(toList()); + } + public List findMySessions(LoginMember member) { List sessionIds = findMySessionIds(member.getId()); - return sessionRepository.findAllById(sessionIds).stream() + return sessionRepository.findAllByIdInOrderByIdDesc(sessionIds).stream() .map(SessionResponse::of) .collect(toList()); } @@ -76,11 +82,11 @@ public List findMySessionIds(Long memberId) { public List findAllWithMySessionFirst(LoginMember loginMember) { if (loginMember.isAnonymous()) { - return findAll(); + return findAllByOrderByIdDesc(); } List mySessions = findMySessions(loginMember); - List allSessions = findAll(); + List allSessions = findAllByOrderByIdDesc(); allSessions.removeAll(mySessions); return Stream.of(mySessions, allSessions) diff --git a/backend/src/main/java/wooteco/prolog/session/domain/repository/SessionRepository.java b/backend/src/main/java/wooteco/prolog/session/domain/repository/SessionRepository.java index 0099caa8d..2011727ed 100644 --- a/backend/src/main/java/wooteco/prolog/session/domain/repository/SessionRepository.java +++ b/backend/src/main/java/wooteco/prolog/session/domain/repository/SessionRepository.java @@ -10,4 +10,8 @@ public interface SessionRepository extends JpaRepository { Optional findByName(String name); List findAllByCurriculumId(Long curriculumId); + + List findAllByOrderByIdDesc(); + + List findAllByIdInOrderByIdDesc(List ids); } diff --git a/backend/src/main/java/wooteco/prolog/studylog/application/FilterService.java b/backend/src/main/java/wooteco/prolog/studylog/application/FilterService.java index 8a5a59daa..199f76358 100644 --- a/backend/src/main/java/wooteco/prolog/studylog/application/FilterService.java +++ b/backend/src/main/java/wooteco/prolog/studylog/application/FilterService.java @@ -18,10 +18,8 @@ public class FilterService { private final MissionService missionService; public FilterResponse showAll(LoginMember loginMember) { - List sessionResponses = sessionService.findAllWithMySessionFirst( - loginMember); // my session으로 대체 - List missionResponses = missionService.findAllWithMyMissionFirst( - loginMember); + List sessionResponses = sessionService.findAllWithMySessionFirst(loginMember); + List missionResponses = missionService.findAllWithMyMissionFirst(loginMember); return new FilterResponse(sessionResponses, missionResponses); } } diff --git a/backend/src/test/java/wooteco/prolog/session/domain/repository/SessionRepositoryTest.java b/backend/src/test/java/wooteco/prolog/session/domain/repository/SessionRepositoryTest.java new file mode 100644 index 000000000..3ef309015 --- /dev/null +++ b/backend/src/test/java/wooteco/prolog/session/domain/repository/SessionRepositoryTest.java @@ -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 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 sessions = sessionRepository.findAllByIdInOrderByIdDesc( + Lists.newArrayList(session4.getId(), session2.getId())); + + assertThat(sessions.get(0).getName()).isEqualTo("name4"); + assertThat(sessions.get(1).getName()).isEqualTo("name2"); + } +}