#31 loan: create judgement
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package com.example.loan.controller;
|
||||
|
||||
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;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@RestController
|
||||
@RequestMapping("/judgements")
|
||||
public class JudgementController extends AbstractController {
|
||||
|
||||
private final JudgementService judgementService;
|
||||
|
||||
@PostMapping
|
||||
public ResponseDto<JudgementDto.Response> create(@RequestBody JudgementDto.Request request) {
|
||||
return ok(judgementService.create(request));
|
||||
}
|
||||
}
|
||||
45
loan/src/main/java/com/example/loan/dto/JudgementDto.java
Normal file
45
loan/src/main/java/com/example/loan/dto/JudgementDto.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package com.example.loan.dto;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class JudgementDto implements Serializable {
|
||||
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
@Getter
|
||||
@Setter
|
||||
public static class Request {
|
||||
|
||||
private Long applicationId;
|
||||
|
||||
private String name;
|
||||
|
||||
private BigDecimal approvalAmount;
|
||||
|
||||
}
|
||||
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
@Getter
|
||||
@Setter
|
||||
public static class Response {
|
||||
|
||||
private Long judgementId;
|
||||
|
||||
private Long applicationId;
|
||||
|
||||
private String name;
|
||||
|
||||
private BigDecimal approvalAmount;
|
||||
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
private LocalDateTime updatedAt;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.example.loan.repository;
|
||||
|
||||
import com.example.loan.domain.Judgement;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface JudgementRepository extends JpaRepository<Judgement, Long> {
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.example.loan.service;
|
||||
|
||||
import com.example.loan.dto.JudgementDto;
|
||||
|
||||
public interface JudgementService {
|
||||
|
||||
JudgementDto.Response create(JudgementDto.Request request);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.example.loan.service;
|
||||
|
||||
import com.example.loan.domain.Judgement;
|
||||
import com.example.loan.dto.JudgementDto;
|
||||
import com.example.loan.exception.BaseException;
|
||||
import com.example.loan.exception.ResultType;
|
||||
import com.example.loan.repository.ApplicationRepository;
|
||||
import com.example.loan.repository.JudgementRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class JudgementServiceImpl implements JudgementService {
|
||||
|
||||
private final JudgementRepository judgementRepository;
|
||||
private final ApplicationRepository applicationRepository;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
@Override
|
||||
public JudgementDto.Response create(JudgementDto.Request request) {
|
||||
// 신청 정보 검증
|
||||
Long applicationId = request.getApplicationId();
|
||||
if (!isPresentApplication(applicationId)) {
|
||||
throw new BaseException(ResultType.SYSTEM_ERROR);
|
||||
}
|
||||
|
||||
// request dto -> entity -> save
|
||||
Judgement judgement = modelMapper.map(request, Judgement.class);
|
||||
|
||||
Judgement saved = judgementRepository.save(judgement);
|
||||
|
||||
// save -> response dto
|
||||
return modelMapper.map(saved, JudgementDto.Response.class);
|
||||
}
|
||||
|
||||
private boolean isPresentApplication(Long applicationId) {
|
||||
return applicationRepository.findById(applicationId).isPresent();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.example.loan.service;
|
||||
|
||||
import com.example.loan.domain.Application;
|
||||
import com.example.loan.domain.Judgement;
|
||||
import com.example.loan.dto.JudgementDto;
|
||||
import com.example.loan.repository.ApplicationRepository;
|
||||
import com.example.loan.repository.JudgementRepository;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.*;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.modelmapper.ModelMapper;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class JudgementServiceImplTest {
|
||||
|
||||
@InjectMocks
|
||||
private JudgementServiceImpl judgementService;
|
||||
|
||||
@Mock
|
||||
private JudgementRepository judgementRepository;
|
||||
|
||||
@Mock
|
||||
private ApplicationRepository applicationRepository;
|
||||
|
||||
@Spy
|
||||
private ModelMapper modelMapper;
|
||||
|
||||
@Test
|
||||
void Should_ReturnResponseOfNewJudgementEntity_When_RequestNewJudgement() {
|
||||
Judgement judgement = Judgement.builder()
|
||||
.applicationId(1L)
|
||||
.name("member kim")
|
||||
.approvalAmount(BigDecimal.valueOf(5000000))
|
||||
.build();
|
||||
|
||||
JudgementDto.Request request = JudgementDto.Request.builder()
|
||||
.applicationId(1L)
|
||||
.name("member kim")
|
||||
.approvalAmount(BigDecimal.valueOf(5000000))
|
||||
.build();
|
||||
|
||||
when(applicationRepository.findById(1L)).thenReturn(Optional.ofNullable(Application.builder().build()));
|
||||
when(judgementRepository.save(ArgumentMatchers.any(Judgement.class))).thenReturn(judgement);
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user