Skip to content

Commit

Permalink
Merge pull request #249 from 9oormthon-univ/feat/#248
Browse files Browse the repository at this point in the history
feat: 목표 탐색 상세 API 추가
  • Loading branch information
koosco authored Dec 5, 2024
2 parents 0fc1a8e + 6bfbf78 commit fc06132
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/groom/orbit/goal/app/MemberGoalService.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,8 @@ public GetMemberGoalResponseDto createOtherGoal(Long memberId, Long memberGoalId
copyMemberGoal.getCompletedDate().toLocalDate(),
questDtos);
}

public List<MemberGoal> findAllMemberGoal(Long goalId) {
return memberGoalRepository.findAllWithQuestsByGoalId(goalId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.groom.orbit.goal.app.dto.response;

import java.util.List;

public record GoalSearchDetailResponseDto(String category, String goalName, List<String> quests) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.groom.orbit.goal.app.query;

import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.groom.orbit.common.exception.CommonException;
import com.groom.orbit.common.exception.ErrorCode;
import com.groom.orbit.goal.app.MemberGoalService;
import com.groom.orbit.goal.app.dto.response.GoalSearchDetailResponseDto;
import com.groom.orbit.goal.dao.entity.Goal;
import com.groom.orbit.goal.dao.entity.MemberGoal;
import com.groom.orbit.goal.dao.entity.Quest;

import lombok.RequiredArgsConstructor;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class GoalSearchService {

private final MemberGoalService memberGoalService;

public GoalSearchDetailResponseDto findGoal(Long goalId) {
List<MemberGoal> memberGoals = memberGoalService.findAllMemberGoal(goalId);
Goal goal = findGoal(memberGoals);
List<String> questTitles =
memberGoals.stream()
.map(MemberGoal::getQuests)
.flatMap(Collection::stream)
.collect(Collectors.toSet())
.stream()
.sorted(Comparator.comparing(Quest::getCreatedAt).reversed())
.map(Quest::getTitle)
.toList();

return new GoalSearchDetailResponseDto(
goal.getCategory().getCategory(), goal.getTitle(), questTitles);
}

private static Goal findGoal(List<MemberGoal> memberGoals) {
if (memberGoals.isEmpty()) {
throw new CommonException(ErrorCode.NOT_FOUND_GOAL);
}
return memberGoals.getFirst().getGoal();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.groom.orbit.goal.controller.query;

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 com.groom.orbit.common.dto.ResponseDto;
import com.groom.orbit.goal.app.dto.response.GoalSearchDetailResponseDto;
import com.groom.orbit.goal.app.query.GoalSearchService;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/goal/search")
public class GoalSearchController {

private final GoalSearchService goalSearchService;

@GetMapping("/{goal_id}")
public ResponseDto<GoalSearchDetailResponseDto> getSearchDetail(
@PathVariable("goal_id") Long goalId) {
return ResponseDto.ok(goalSearchService.findGoal(goalId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,11 @@ List<MemberGoal> findByMemberIdAndIsComplete(

List<MemberGoal> findAllByMemberIdAndIsCompleteFalseAndSequenceGreaterThan(
Long memberId, Long sequence);

@Query(
"select mg from MemberGoal mg"
+ " join fetch mg.goal g"
+ " join fetch mg.quests q"
+ " where g.goalId=:goal_id")
List<MemberGoal> findAllWithQuestsByGoalId(@Param("goal_id") Long goalId);
}

0 comments on commit fc06132

Please sign in to comment.