diff --git a/loan/src/main/java/com/example/loan/controller/JudgementController.java b/loan/src/main/java/com/example/loan/controller/JudgementController.java index 33750f8e..e87a89b2 100644 --- a/loan/src/main/java/com/example/loan/controller/JudgementController.java +++ b/loan/src/main/java/com/example/loan/controller/JudgementController.java @@ -4,10 +4,7 @@ import com.example.loan.dto.JudgementDto; import com.example.loan.dto.ResponseDto; import com.example.loan.service.JudgementService; import lombok.RequiredArgsConstructor; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; @RequiredArgsConstructor @RestController @@ -20,4 +17,18 @@ public class JudgementController extends AbstractController { public ResponseDto create(@RequestBody JudgementDto.Request request) { return ok(judgementService.create(request)); } + + @GetMapping("/{judgementId}") + public ResponseDto get( + @PathVariable Long judgementId + ) { + return ok(judgementService.get(judgementId)); + } + + @GetMapping("/applications/{applicationId}") + public ResponseDto getJudgementOfApplication( + @PathVariable Long applicationId + ) { + return ok(judgementService.getJudgementOfApplication(applicationId)); + } } diff --git a/loan/src/main/java/com/example/loan/repository/JudgementRepository.java b/loan/src/main/java/com/example/loan/repository/JudgementRepository.java index de5ef292..5aa77a8e 100644 --- a/loan/src/main/java/com/example/loan/repository/JudgementRepository.java +++ b/loan/src/main/java/com/example/loan/repository/JudgementRepository.java @@ -4,6 +4,10 @@ import com.example.loan.domain.Judgement; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.Optional; + @Repository public interface JudgementRepository extends JpaRepository { + + Optional findByApplicationId(Long applicationId); } diff --git a/loan/src/main/java/com/example/loan/service/JudgementService.java b/loan/src/main/java/com/example/loan/service/JudgementService.java index 2ab10c06..03785e04 100644 --- a/loan/src/main/java/com/example/loan/service/JudgementService.java +++ b/loan/src/main/java/com/example/loan/service/JudgementService.java @@ -5,4 +5,8 @@ import com.example.loan.dto.JudgementDto; public interface JudgementService { JudgementDto.Response create(JudgementDto.Request request); + + JudgementDto.Response get(Long judgementId); + + JudgementDto.Response getJudgementOfApplication(Long applicationId); } diff --git a/loan/src/main/java/com/example/loan/service/JudgementServiceImpl.java b/loan/src/main/java/com/example/loan/service/JudgementServiceImpl.java index 4bbfc523..d835a0ff 100644 --- a/loan/src/main/java/com/example/loan/service/JudgementServiceImpl.java +++ b/loan/src/main/java/com/example/loan/service/JudgementServiceImpl.java @@ -35,6 +35,26 @@ public class JudgementServiceImpl implements JudgementService { return modelMapper.map(saved, JudgementDto.Response.class); } + @Override + public JudgementDto.Response get(Long judgementId) { + Judgement judgement = judgementRepository.findById(judgementId) + .orElseThrow(() -> new BaseException(ResultType.SYSTEM_ERROR)); + + return modelMapper.map(judgement, JudgementDto.Response.class); + } + + @Override + public JudgementDto.Response getJudgementOfApplication(Long applicationId) { + if (!isPresentApplication(applicationId)) { + throw new BaseException(ResultType.SYSTEM_ERROR); + } + + Judgement judgement = judgementRepository.findByApplicationId(applicationId) + .orElseThrow(() -> new BaseException(ResultType.SYSTEM_ERROR)); + + return modelMapper.map(judgement, JudgementDto.Response.class); + } + private boolean isPresentApplication(Long applicationId) { return applicationRepository.findById(applicationId).isPresent(); } diff --git a/loan/src/test/java/com/example/loan/service/JudgementServiceImplTest.java b/loan/src/test/java/com/example/loan/service/JudgementServiceImplTest.java index 3733f8ec..bea9bc5e 100644 --- a/loan/src/test/java/com/example/loan/service/JudgementServiceImplTest.java +++ b/loan/src/test/java/com/example/loan/service/JudgementServiceImplTest.java @@ -15,6 +15,7 @@ import org.modelmapper.ModelMapper; import java.math.BigDecimal; import java.util.Optional; +import static org.assertj.core.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; @@ -52,8 +53,42 @@ class JudgementServiceImplTest { JudgementDto.Response actual = judgementService.create(request); - Assertions.assertThat(actual.getName()).isSameAs(judgement.getName()); - Assertions.assertThat(actual.getApplicationId()).isSameAs(judgement.getApplicationId()); - Assertions.assertThat(actual.getApprovalAmount()).isSameAs(judgement.getApprovalAmount()); + assertThat(actual.getName()).isSameAs(judgement.getName()); + assertThat(actual.getApplicationId()).isSameAs(judgement.getApplicationId()); + assertThat(actual.getApprovalAmount()).isSameAs(judgement.getApprovalAmount()); + } + + @Test + void Should_ReturnResponseOfExistJudgementEntity_When_RequestExistJudgementId() { + + Judgement entity = Judgement.builder() + .judgementId(1L) + .build(); + + when(judgementRepository.findById(1L)).thenReturn(Optional.ofNullable(entity)); + + JudgementDto.Response actual = judgementService.get(1L); + + assertThat(actual.getJudgementId()).isSameAs(1L); + } + + @Test + void Should_ReturnResponseOfExistJudgementEntity_When_RequestExistApplicationId() { + + Judgement judgementEntity + = Judgement.builder() + .judgementId(1L) + .build(); + + Application applicationEntity = Application.builder() + .applicationId(1L) + .build(); + + when(applicationRepository.findById(1L)).thenReturn(Optional.ofNullable(applicationEntity)); + when(judgementRepository.findByApplicationId(1L)).thenReturn(Optional.ofNullable(judgementEntity)); + + JudgementDto.Response actual = judgementService.getJudgementOfApplication(1L); + + assertThat(actual.getJudgementId()).isSameAs(1L); } } \ No newline at end of file