Skip to content

Commit

Permalink
Feat : DIG-97 루틴 조회 시 분할 필터링 기능 추가
Browse files Browse the repository at this point in the history
- 쿼리 파라미터 값을 통해 루틴을 필터링 할 수 있도록 구현
- 분할을 명시하지 않을 시 모든 분할을 포함하여 반환하도록 구현
  • Loading branch information
bstaran committed Nov 10, 2023
1 parent e77c469 commit 606566e
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.ogjg.daitgym.comment.routine.dto;

import com.ogjg.daitgym.domain.RoutineComment;
import com.ogjg.daitgym.domain.routine.RoutineComment;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.ogjg.daitgym.comment.routine.dto;

import com.ogjg.daitgym.domain.RoutineComment;
import com.ogjg.daitgym.domain.routine.RoutineComment;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.ogjg.daitgym.comment.routine.dto.response;

import com.ogjg.daitgym.comment.routine.dto.RoutineChildDto;
import com.ogjg.daitgym.domain.RoutineComment;
import com.ogjg.daitgym.domain.routine.RoutineComment;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.ogjg.daitgym.comment.routine.dto.response;

import com.ogjg.daitgym.comment.routine.dto.RoutineCommentDto;
import com.ogjg.daitgym.domain.RoutineComment;
import com.ogjg.daitgym.domain.routine.RoutineComment;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Page;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.ogjg.daitgym.comment.routine.repository;

import com.ogjg.daitgym.domain.RoutineComment;
import com.ogjg.daitgym.domain.routine.RoutineComment;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import com.ogjg.daitgym.comment.routine.exception.NotFoundRoutine;
import com.ogjg.daitgym.comment.routine.exception.NotFoundRoutineComment;
import com.ogjg.daitgym.comment.routine.repository.RoutineCommentRepository;
import com.ogjg.daitgym.comment.routine.repository.RoutineRepository;
import com.ogjg.daitgym.domain.Routine;
import com.ogjg.daitgym.domain.RoutineComment;
import com.ogjg.daitgym.domain.User;
import com.ogjg.daitgym.domain.routine.Routine;
import com.ogjg.daitgym.domain.routine.RoutineComment;
import com.ogjg.daitgym.routine.repository.RoutineRepository;
import com.ogjg.daitgym.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/ogjg/daitgym/domain/BaseEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@Getter
public abstract class BaseEntity {

@CreatedDate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ public class ExerciseDetail extends BaseEntity {

private int weight;

private int order;
private int exerciseOrder;

private LocalTime restTime;

@Builder
public ExerciseDetail(Day day, Exercise exercise, int setCount, int repetitionCount, int weight, int order, LocalTime restTime) {
public ExerciseDetail(Day day, Exercise exercise, int setCount, int repetitionCount, int weight, int exerciseOrder, LocalTime restTime) {
this.day = day;
this.exercise = exercise;
this.setCount = setCount;
this.repetitionCount = repetitionCount;
this.weight = weight;
this.order = order;
this.exerciseOrder = exerciseOrder;
this.restTime = restTime;
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/ogjg/daitgym/domain/routine/Routine.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class Routine extends BaseEntity {

private int duration;

private int division;

@Enumerated(STRING)
private UnitType unitType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ public interface FollowRepository extends JpaRepository<Follow, Follow.PK>, Foll

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

}

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

import com.ogjg.daitgym.comment.feedExerciseJournal.exception.NotFoundUser;
import com.ogjg.daitgym.comment.routine.exception.NotFoundRoutine;
import com.ogjg.daitgym.comment.routine.repository.RoutineRepository;
import com.ogjg.daitgym.domain.Routine;
import com.ogjg.daitgym.domain.RoutineLike;
import com.ogjg.daitgym.domain.User;
import com.ogjg.daitgym.domain.routine.Routine;
import com.ogjg.daitgym.domain.routine.RoutineLike;
import com.ogjg.daitgym.like.routine.dto.RoutineLikeResponse;
import com.ogjg.daitgym.like.routine.repository.RoutineLikeRepository;
import com.ogjg.daitgym.routine.repository.RoutineRepository;
import com.ogjg.daitgym.user.repository.UserRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@
import com.ogjg.daitgym.routine.service.RoutineService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.awt.print.Pageable;

@Slf4j
@RestController
Expand All @@ -27,27 +24,30 @@ public class RoutineController {
@GetMapping
public ApiResponse<RoutineListResponseDto> getRoutines(
Pageable pageable,
@RequestParam(value = "division", required = false) Integer division,
@AuthenticationPrincipal OAuth2JwtUserDetails oAuth2JwtUserDetails) {
RoutineListResponseDto routines = routineService.getRoutines(pageable, oAuth2JwtUserDetails.getEmail());
RoutineListResponseDto routines = routineService.getRoutines(pageable, division, oAuth2JwtUserDetails.getEmail());

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

@GetMapping("/{userEmail}")
public ApiResponse<RoutineListResponseDto> getUserRoutines(
@PathVariable("userEmail") String userEmail,
Pageable pageable) {
RoutineListResponseDto userRoutines = routineService.getUserRoutines(userEmail, pageable);
Pageable pageable,
@RequestParam(value = "division", required = false) Integer division) {
RoutineListResponseDto userRoutines = routineService.getUserRoutines(userEmail, division, pageable);

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

@GetMapping("/following")
public ApiResponse<RoutineListResponseDto> getFollowerRoutines(
Pageable pageable,
@RequestParam(value = "division", required = false) Integer division,
@AuthenticationPrincipal OAuth2JwtUserDetails oAuth2JwtUserDetails) {

RoutineListResponseDto routinesOfFollowing = routineService.getFollowerRoutines(pageable, oAuth2JwtUserDetails.getEmail());
RoutineListResponseDto routinesOfFollowing = routineService.getFollowerRoutines(pageable, division, oAuth2JwtUserDetails.getEmail());

return new ApiResponse<>(ErrorCode.SUCCESS, routinesOfFollowing);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package com.ogjg.daitgym.routine.repository;

import com.ogjg.daitgym.domain.routine.Routine;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

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

public interface RoutineRepository extends JpaRepository<Routine, String> {

Optional<Slice<Routine>> findAll(Pageable pageable);
@Query("SELECT r FROM Routine r WHERE (:division IS NULL OR r.division = :division)")
Optional<Slice<Routine>> findAllByDivision(@Param("division") Integer division, Pageable pageable);

Optional<Slice<Routine>> findAllByUserEmail(String email, Pageable pageable);
@Query("SELECT r FROM Routine r WHERE (:division IS NULL OR r.division = :division) AND r.user.email = :email")
Optional<Slice<Routine>> findByDivisionAndUserEmail(@Param("division") Integer division, @Param("email") String email, Pageable pageable);

Optional<Slice<Routine>> findByUserEmailIn(List<String> followerEmails, Pageable pageable);
@Query("SELECT r FROM Routine r WHERE (:division IS NULL OR r.division = :division) AND r.user.email IN :followerEmails")
Optional<Slice<Routine>> findByDivisionAndUserEmailIn(@Param("division") Integer division, @Param("followerEmails") List<String> followerEmails, Pageable pageable);

Optional<Routine> findById(Long routineId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
import com.ogjg.daitgym.routine.repository.RoutineRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.print.Pageable;
import java.util.Collections;
import java.util.List;
import java.util.Set;
Expand All @@ -38,9 +38,9 @@ public class RoutineService {
private final RoutineLikeRepository routineLikeRepository;

@Transactional(readOnly = true)
public RoutineListResponseDto getRoutines(Pageable pageable, String email) {
public RoutineListResponseDto getRoutines(Pageable pageable, Integer division, String email) {

Slice<Routine> routines = routineRepository.findAll(pageable)
Slice<Routine> routines = routineRepository.findAllByDivision(division, pageable)
.orElseThrow(NotFoundRoutine::new);

return getRoutineListResponseDto(routines, email);
Expand Down Expand Up @@ -74,23 +74,23 @@ private RoutineListResponseDto getRoutineListResponseDto(Slice<Routine> routines
}

@Transactional(readOnly = true)
public RoutineListResponseDto getUserRoutines(String userEmail, Pageable pageable) {
Slice<Routine> routines = routineRepository.findAllByUserEmail(userEmail, pageable)
public RoutineListResponseDto getUserRoutines(String userEmail, Integer division, Pageable pageable) {
Slice<Routine> routines = routineRepository.findByDivisionAndUserEmail(division, userEmail, pageable)
.orElseThrow(NotFoundRoutine::new);

return getRoutineListResponseDto(routines, userEmail);
}

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

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

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

return getRoutineListResponseDto(routines, myEmail);
Expand Down Expand Up @@ -138,7 +138,7 @@ private static List<DayDto> getDayDtos(List<Day> days) {

return ExerciseDto.builder()
.id(exerciseDetail.getExercise().getId())
.order(exerciseDetail.getOrder())
.order(exerciseDetail.getExerciseOrder())
.name(exerciseDetail.getExercise().getName())
.part(exerciseDetail.getExercise().getExercisePart().getPart())
.restTime(new RestTimeDto(
Expand Down

0 comments on commit 606566e

Please sign in to comment.