Skip to content

Commit

Permalink
Feat : DIG-35 팔로우한 유저의 루틴 목록 조회 기능 구현
Browse files Browse the repository at this point in the history
- 팔로우한 유저의 루틴 목록을 조회하는 API
- Slice 방식을 사용하여 구현
  • Loading branch information
bstaran committed Nov 9, 2023
1 parent 20306d7 commit f927d5a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
import com.ogjg.daitgym.domain.follow.Follow;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface FollowRepository extends JpaRepository<Follow, Follow.PK>, FollowRepositoryCustom {

int countByFollowPKTargetEmail(String targetEmail);

int countByFollowPKFollowerEmail(String followerEmail);
}

Optional<List<Follow>> findAllByTargetEmail(String followingEmail);

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.ogjg.daitgym.common.exception.ErrorCode;
import com.ogjg.daitgym.common.response.ApiResponse;
import com.ogjg.daitgym.routine.dto.RoutineDto;
import com.ogjg.daitgym.routine.dto.RoutineListResponseDto;
import com.ogjg.daitgym.routine.repository.RoutineRepository;
import com.ogjg.daitgym.routine.service.RoutineService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -22,17 +24,25 @@ public class RoutineController {
private final RoutineService routineService;

@GetMapping
public ApiResponse<?> getRoutines(Pageable pageable) {
public ApiResponse<RoutineListResponseDto> getRoutines(Pageable pageable) {
RoutineListResponseDto routines = routineService.getRoutines(pageable);

return new ApiResponse<>(ErrorCode.SUCCESS, routines);
}

@GetMapping("/{userEmail}")
public ApiResponse<?> getUserRoutines(@PathVariable("userEmail") String userEmail,
public ApiResponse<RoutineListResponseDto> getUserRoutines(@PathVariable("userEmail") String userEmail,
Pageable pageable) {
RoutineListResponseDto userRoutines = routineService.getUserRoutines(userEmail, pageable);

return new ApiResponse<>(ErrorCode.SUCCESS, userRoutines);
}

@GetMapping("/following")
public ApiResponse<RoutineListResponseDto> getFollowerRoutines(String myEmail, Pageable pageable) {

RoutineListResponseDto routinesOfFollowing = routineService.getFollowerRoutines(myEmail, pageable);

return new ApiResponse<>(ErrorCode.SUCCESS, routinesOfFollowing);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.data.jpa.repository.JpaRepository;

import java.awt.print.Pageable;
import java.util.List;
import java.util.Optional;

public interface RoutineRepository extends JpaRepository<Routine, String> {
Expand All @@ -13,4 +14,6 @@ public interface RoutineRepository extends JpaRepository<Routine, String> {

Optional<Slice<Routine>> findAllByUserEmail(String email, Pageable pageable);

Optional<Slice<Routine>> findByUserEmailIn(List<String> followerEmails, Pageable pageable);

}
19 changes: 19 additions & 0 deletions src/main/java/com/ogjg/daitgym/routine/service/RoutineService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.ogjg.daitgym.comment.routine.exception.NotFoundRoutine;
import com.ogjg.daitgym.domain.routine.Routine;
import com.ogjg.daitgym.follow.repository.FollowRepository;
import com.ogjg.daitgym.routine.dto.RoutineDto;
import com.ogjg.daitgym.routine.dto.RoutineListResponseDto;
import com.ogjg.daitgym.routine.repository.RoutineRepository;
Expand All @@ -12,6 +13,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.awt.print.Pageable;
import java.util.Collections;
import java.util.List;

@Slf4j
Expand All @@ -20,6 +22,7 @@
public class RoutineService {

private final RoutineRepository routineRepository;
private final FollowRepository followRepository;

@Transactional(readOnly = true)
public RoutineListResponseDto getRoutines(Pageable pageable) {
Expand Down Expand Up @@ -63,4 +66,20 @@ public RoutineListResponseDto getUserRoutines(String userEmail, Pageable pageabl

return getRoutineListResponseDto(routines);
}

@Transactional(readOnly = true)
public RoutineListResponseDto getFollowerRoutines(String myEmail, Pageable pageable) {

List<String> followingEmails = followRepository.findAllByTargetEmail(myEmail)
.orElse(Collections.emptyList())
.stream()
.map(follow -> follow.getTarget().getEmail())
.toList();

Slice<Routine> routines = routineRepository.findByUserEmailIn(followingEmails, pageable)
.orElseThrow(NotFoundRoutine::new);

return getRoutineListResponseDto(routines);
}

}

0 comments on commit f927d5a

Please sign in to comment.