|
| 1 | +package com.bloomgroom.domain.smallgoal.application; |
| 2 | + |
| 3 | +import com.bloomgroom.domain.biggoal.domain.BigGoal; |
| 4 | +import com.bloomgroom.domain.biggoal.domain.repository.BigGoalRepository; |
| 5 | +import com.bloomgroom.domain.smallgoal.domain.SmallGoal; |
| 6 | +import com.bloomgroom.domain.smallgoal.domain.repository.SmallGoalRepository; |
| 7 | +import com.bloomgroom.domain.smallgoal.dto.request.CreateSmallGoalReq; |
| 8 | +import com.bloomgroom.domain.smallgoal.dto.request.UpdateSmallGoalReq; |
| 9 | +import lombok.RequiredArgsConstructor; |
| 10 | +import org.springframework.stereotype.Service; |
| 11 | + |
| 12 | +import java.time.LocalDateTime; |
| 13 | +import java.util.List; |
| 14 | +import java.util.stream.Collectors; |
| 15 | + |
| 16 | +import com.bloomgroom.global.payload.ApiResponse; |
| 17 | +import org.springframework.http.ResponseEntity; |
| 18 | + |
| 19 | +@Service |
| 20 | +@RequiredArgsConstructor |
| 21 | +public class SmallGoalService { |
| 22 | + |
| 23 | + private final SmallGoalRepository smallGoalRepository; |
| 24 | + private final BigGoalRepository bigGoalRepository; |
| 25 | + |
| 26 | + // 세부 목표 생성 |
| 27 | + public ResponseEntity<ApiResponse> createSmallGoal(CreateSmallGoalReq request) { |
| 28 | + SmallGoal smallGoal = new SmallGoal(); |
| 29 | + |
| 30 | + BigGoal bigGoal = bigGoalRepository.findById(request.getBigGoalId()) |
| 31 | + .orElseThrow(() -> new RuntimeException("장기 목표를 찾을 수 없습니다.")); |
| 32 | + smallGoal.setBigGoal(bigGoal); |
| 33 | + smallGoal.setContent(request.getContent()); |
| 34 | + smallGoal.setGoalStatus(request.getGoalStatus()); |
| 35 | + smallGoal = smallGoalRepository.save(smallGoal); |
| 36 | + |
| 37 | + ApiResponse response = ApiResponse.toApiResponse(smallGoal); |
| 38 | + return ResponseEntity.ok(response); |
| 39 | + } |
| 40 | + |
| 41 | + // 특정 장기 목표의 세부 목표 조회 |
| 42 | + public ResponseEntity<ApiResponse> getSmallGoalsByBigGoalId(Long bigGoalId) { |
| 43 | + BigGoal bigGoal = bigGoalRepository.findById(bigGoalId) |
| 44 | + .orElseThrow(() -> new RuntimeException("장기 목표를 찾을 수 없습니다.")); |
| 45 | + |
| 46 | + List<SmallGoal> smallGoals = smallGoalRepository.findByBigGoal(bigGoal); |
| 47 | + ApiResponse response = ApiResponse.toApiResponse(smallGoals); |
| 48 | + |
| 49 | + return ResponseEntity.ok(response); |
| 50 | + } |
| 51 | + |
| 52 | + // 세부 목표 수정 |
| 53 | + public ResponseEntity<ApiResponse> updateSmallGoal(Long smallGoalId, UpdateSmallGoalReq request) { |
| 54 | + SmallGoal smallGoal = smallGoalRepository.findById(smallGoalId) |
| 55 | + .orElseThrow(() -> new RuntimeException("세부 목표를 찾을 수 없습니다.")); |
| 56 | + |
| 57 | + smallGoal.setContent(request.getContent()); |
| 58 | + smallGoal.setGoalStatus(request.getGoalStatus()); |
| 59 | + smallGoal = smallGoalRepository.save(smallGoal); |
| 60 | + |
| 61 | + ApiResponse response = ApiResponse.toApiResponse(smallGoal); |
| 62 | + |
| 63 | + return ResponseEntity.ok(response); |
| 64 | + } |
| 65 | + |
| 66 | + // 세부 목표 삭제 |
| 67 | + public ResponseEntity<ApiResponse> deleteSmallGoal(Long smallGoalId) { |
| 68 | + smallGoalRepository.deleteById(smallGoalId); |
| 69 | + ApiResponse response = ApiResponse.toApiResponse("세부 목표가 삭제되었습니다."); |
| 70 | + |
| 71 | + return ResponseEntity.ok(response); |
| 72 | + } |
| 73 | + |
| 74 | + // 날짜로 세부 목표 조회 |
| 75 | + public ResponseEntity<ApiResponse> getSmallGoalsByDate(Long bigGoalId, LocalDateTime smallGoalDate) { |
| 76 | + BigGoal bigGoal = bigGoalRepository.findById(bigGoalId) |
| 77 | + .orElseThrow(() -> new RuntimeException("장기 목표를 찾을 수 없습니다.")); |
| 78 | + |
| 79 | + List<SmallGoal> smallGoals = smallGoalRepository.findByBigGoal(bigGoal).stream() |
| 80 | + .filter(goal -> goal.getSmallGoalDate().toLocalDate().isEqual(smallGoalDate.toLocalDate())) // 날짜 비교 |
| 81 | + .collect(Collectors.toList()); |
| 82 | + |
| 83 | + ApiResponse response = ApiResponse.toApiResponse(smallGoals); |
| 84 | + return ResponseEntity.ok(response); |
| 85 | + } |
| 86 | +} |
0 commit comments