#31 loan: get judgement
This commit is contained in:
@@ -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<JudgementDto.Response> create(@RequestBody JudgementDto.Request request) {
|
||||
return ok(judgementService.create(request));
|
||||
}
|
||||
|
||||
@GetMapping("/{judgementId}")
|
||||
public ResponseDto<JudgementDto.Response> get(
|
||||
@PathVariable Long judgementId
|
||||
) {
|
||||
return ok(judgementService.get(judgementId));
|
||||
}
|
||||
|
||||
@GetMapping("/applications/{applicationId}")
|
||||
public ResponseDto<JudgementDto.Response> getJudgementOfApplication(
|
||||
@PathVariable Long applicationId
|
||||
) {
|
||||
return ok(judgementService.getJudgementOfApplication(applicationId));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Judgement, Long> {
|
||||
|
||||
Optional<Judgement> findByApplicationId(Long applicationId);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user