#31 loan: create counsel
This commit is contained in:
@@ -21,6 +21,8 @@ repositories {
|
|||||||
dependencies {
|
dependencies {
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||||
|
implementation 'org.modelmapper:modelmapper:3.1.0'
|
||||||
|
|
||||||
compileOnly 'org.projectlombok:lombok'
|
compileOnly 'org.projectlombok:lombok'
|
||||||
runtimeOnly 'com.h2database:h2'
|
runtimeOnly 'com.h2database:h2'
|
||||||
runtimeOnly 'com.mysql:mysql-connector-j'
|
runtimeOnly 'com.mysql:mysql-connector-j'
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package com.example.loan.config;
|
||||||
|
|
||||||
|
import org.modelmapper.ModelMapper;
|
||||||
|
import org.modelmapper.convention.MatchingStrategies;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class ModelMapperConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ModelMapper modelMapper() {
|
||||||
|
ModelMapper modelMapper = new ModelMapper();
|
||||||
|
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
|
||||||
|
modelMapper.getConfiguration().setSkipNullEnabled(true);
|
||||||
|
return modelMapper;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.example.loan.controller;
|
||||||
|
|
||||||
|
import com.example.loan.dto.CounselDto;
|
||||||
|
import com.example.loan.dto.ResponseDto;
|
||||||
|
import com.example.loan.service.CounselService;
|
||||||
|
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;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@RequestMapping("/counsels")
|
||||||
|
public class CounselController extends AbstractController {
|
||||||
|
|
||||||
|
private final CounselService counselService;
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public ResponseDto<CounselDto.Response> create(@RequestBody CounselDto.Request request) {
|
||||||
|
return ok(counselService.create(request));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
43
loan/src/main/java/com/example/loan/dto/CounselDto.java
Normal file
43
loan/src/main/java/com/example/loan/dto/CounselDto.java
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package com.example.loan.dto;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
public class CounselDto {
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Getter
|
||||||
|
@Builder
|
||||||
|
public static class Request {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private String cellPhone;
|
||||||
|
private String email;
|
||||||
|
private String memo;
|
||||||
|
private String address;
|
||||||
|
private String addressDetail;
|
||||||
|
private String zipCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Builder
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public static class Response {
|
||||||
|
|
||||||
|
private Long counselId;
|
||||||
|
private String name;
|
||||||
|
private String cellPhone;
|
||||||
|
private String email;
|
||||||
|
private String memo;
|
||||||
|
private String address;
|
||||||
|
private String addressDetail;
|
||||||
|
private String zipCode;
|
||||||
|
private LocalDateTime appliedAt;
|
||||||
|
private LocalDateTime createdAt;
|
||||||
|
private LocalDateTime updatedAt;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.example.loan.repository;
|
||||||
|
|
||||||
|
import com.example.loan.domain.Counsel;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface CounselRepository extends JpaRepository<Counsel, Long> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.example.loan.service;
|
||||||
|
|
||||||
|
import com.example.loan.dto.CounselDto;
|
||||||
|
|
||||||
|
public interface CounselService {
|
||||||
|
|
||||||
|
CounselDto.Response create(CounselDto.Request request);
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package com.example.loan.service;
|
||||||
|
|
||||||
|
import com.example.loan.domain.Counsel;
|
||||||
|
import com.example.loan.dto.CounselDto;
|
||||||
|
import com.example.loan.repository.CounselRepository;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.modelmapper.ModelMapper;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class CounselServiceImpl implements CounselService {
|
||||||
|
|
||||||
|
private final CounselRepository counselRepository;
|
||||||
|
|
||||||
|
private final ModelMapper modelMapper;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CounselDto.Response create(CounselDto.Request request) {
|
||||||
|
Counsel counsel = modelMapper.map(request, Counsel.class);
|
||||||
|
counsel.setAppliedAt(LocalDateTime.now());
|
||||||
|
|
||||||
|
Counsel created = counselRepository.save(counsel);
|
||||||
|
return modelMapper.map(created, CounselDto.Response.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package com.example.loan.service;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import com.example.loan.domain.Counsel;
|
||||||
|
import com.example.loan.dto.CounselDto;
|
||||||
|
import com.example.loan.repository.CounselRepository;
|
||||||
|
import org.assertj.core.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.ArgumentMatchers;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.Spy;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
import org.modelmapper.ModelMapper;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class CounselServiceTest {
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
CounselServiceImpl counselService;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private CounselRepository counselRepository;
|
||||||
|
|
||||||
|
@Spy
|
||||||
|
private ModelMapper modelMapper;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void Should_ReturnResponseOfNewCounselEntity_When_RequestCounsel() {
|
||||||
|
Counsel entity = Counsel.builder()
|
||||||
|
.name("kim")
|
||||||
|
.cellPhone("010-1234-1234")
|
||||||
|
.email("email@email.com")
|
||||||
|
.memo("대출 해줭")
|
||||||
|
.zipCode("12345")
|
||||||
|
.address("서울특별시")
|
||||||
|
.addressDetail("101동 101호")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
CounselDto.Request request = CounselDto.Request.builder()
|
||||||
|
.name("kim")
|
||||||
|
.cellPhone("010-1234-1234")
|
||||||
|
.email("email@email.com")
|
||||||
|
.memo("대출 해줭")
|
||||||
|
.zipCode("12345")
|
||||||
|
.address("서울특별시")
|
||||||
|
.addressDetail("101동 101호")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
|
||||||
|
when(counselRepository.save(ArgumentMatchers.any(Counsel.class))).thenReturn(entity);
|
||||||
|
|
||||||
|
CounselDto.Response actual = counselService.create(request);
|
||||||
|
|
||||||
|
assertThat(actual.getName()).isSameAs(entity.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user