diff --git a/src/main/java/umc_haekathon_4/demo/service/MissionService.java b/src/main/java/umc_haekathon_4/demo/service/MissionService.java index 4399798..7d2cdea 100644 --- a/src/main/java/umc_haekathon_4/demo/service/MissionService.java +++ b/src/main/java/umc_haekathon_4/demo/service/MissionService.java @@ -1,16 +1,40 @@ package umc_haekathon_4.demo.service; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import umc_haekathon_4.demo.domain.Mission; +import umc_haekathon_4.demo.domain.TreasureBox; import umc_haekathon_4.demo.repository.MissionRepository; +import java.util.Optional; + +@Service public class MissionService { - private MissionRepository missionRepository; + private final MissionRepository missionRepository; + @Autowired + public MissionService(MissionRepository missionRepository) { + this.missionRepository = missionRepository; + } +/* @Transactional(readOnly = true) public Mission findMissionById(Long missionId) { return missionRepository.findById(missionId) - .orElseThrow(()->new RuntimeException("Mission not found")); + .orElseThrow(() -> new RuntimeException("Mission not found with id: " + missionId)); + }*/ + + @Transactional(readOnly = true) + public Mission findMissionByIdOrDefault(Long missionId) { + return missionRepository.findById(missionId) + .orElse(getDefaultMission()); + } + + private Mission getDefaultMission() { + Mission defaultMission = new Mission(); + defaultMission.setBody("This is a default mission."); + return defaultMission; } + } diff --git a/src/main/java/umc_haekathon_4/demo/web/controller/MissionController.java b/src/main/java/umc_haekathon_4/demo/web/controller/MissionController.java index ad6ab70..c3ae91a 100644 --- a/src/main/java/umc_haekathon_4/demo/web/controller/MissionController.java +++ b/src/main/java/umc_haekathon_4/demo/web/controller/MissionController.java @@ -5,9 +5,12 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import umc_haekathon_4.demo.apiPayload.ApiResponse; +import umc_haekathon_4.demo.apiPayload.code.ErrorReasonDTO; import umc_haekathon_4.demo.domain.Mission; import umc_haekathon_4.demo.service.MissionService; +import java.util.Optional; + @RestController @RequiredArgsConstructor @RequestMapping("/") @@ -15,11 +18,13 @@ public class MissionController { private final MissionService missionService; + //미션 주기 @GetMapping("/mission") public ApiResponse getMission(Long missionId){ - Mission mission = missionService.findMissionById(missionId); + Mission mission = missionService.findMissionByIdOrDefault(missionId); return ApiResponse.onSuccess(mission); } + }