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

feat1: mission 구현 완료 #31

Merged
merged 1 commit into from
Jul 4, 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
28 changes: 26 additions & 2 deletions src/main/java/umc_haekathon_4/demo/service/MissionService.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@
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("/")
public class MissionController {

private final MissionService missionService;


//미션 주기
@GetMapping("/mission")
public ApiResponse<Mission> getMission(Long missionId){
Mission mission = missionService.findMissionById(missionId);
Mission mission = missionService.findMissionByIdOrDefault(missionId);
return ApiResponse.onSuccess(mission);
}


}
Loading