Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 목표 탐색 상세 API 추가 #249

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Loading