Commit 9a5d6a13 authored by Ansal M A's avatar Ansal M A

Commit

parent c28847ef
......@@ -42,6 +42,11 @@
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
......
......@@ -22,13 +22,13 @@ public class EmployeeServiceFacadeApplication {
return WebClient.builder().baseUrl("lb://employee-ws/employee/");
}
@Bean(value = "depWebClient")
@Bean(value = "departmentWebClient")
@LoadBalanced
public WebClient.Builder getDepWebClient(){
return WebClient.builder().baseUrl("lb://department-ws/department/");
}
@Bean(value = "projWebClient")
@Bean(value = "projectWebClient")
@LoadBalanced
public WebClient.Builder getProjWebClient(){
return WebClient.builder().baseUrl("lb://project-ws/project/");
......
package com.nisum.poc.facade.client;
import com.nisum.poc.facade.response.DepartmentDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
public interface IDepartmentClient {
public DepartmentDetails fetchDepartmentById(Long deptId);
public Mono<DepartmentDetails> fetchDepartmentById(Long deptId);
}
package com.nisum.poc.facade.client;
import com.nisum.poc.facade.response.ProjectDetails;
import reactor.core.publisher.Mono;
public interface IProjectClient {
public Mono<ProjectDetails> findProjectDetails(Long projectId);
}
......@@ -6,20 +6,23 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
@Repository
public class DepartmentClient implements IDepartmentClient {
private WebClient.Builder employeeClient;
private final WebClient.Builder departmentWebClient;
@Autowired
public DepartmentClient(@Qualifier("depWebClient") WebClient.Builder employeeClient) {
this.employeeClient = employeeClient;
public DepartmentClient(@Qualifier("departmentWebClient") WebClient.Builder departmentWebClient) {
this.departmentWebClient = departmentWebClient;
}
@Override
public DepartmentDetails fetchDepartmentById(Long deptId) {
return null;
public Mono<DepartmentDetails> fetchDepartmentById(Long departmentId) {
return departmentWebClient.build().get()
.uri(uriBuilder -> uriBuilder.path("/v1/find/{departmentId}").build(departmentId))
.retrieve().bodyToMono(DepartmentDetails.class);
}
}
......@@ -13,22 +13,22 @@ import reactor.core.publisher.Mono;
@Repository
public class EmployeeClient implements IEmployeeClient {
private WebClient.Builder employeeClient;
private WebClient.Builder employeeWebClient;
@Autowired
public EmployeeClient(@Qualifier("empWebClient") WebClient.Builder employeeClient) {
this.employeeClient = employeeClient;
public EmployeeClient(@Qualifier("empWebClient") WebClient.Builder employeeWebClient) {
this.employeeWebClient = employeeWebClient;
}
@Override
public Flux<EmployeeDetails> fetchAllEmployee() {
return employeeClient.build().get().uri("all").retrieve().bodyToFlux(EmployeeDetails.class);
return employeeWebClient.build().get().uri("all").retrieve().bodyToFlux(EmployeeDetails.class);
}
@Override
public Mono<EmployeeDetails> findEmployeeDetails(Long empId) {
return employeeClient.build().get()
return employeeWebClient.build().get()
.uri(uriBuilder -> uriBuilder.path("/v1/{empId}/details").build(empId))
.retrieve().bodyToMono(EmployeeDetails.class);
}
......
package com.nisum.poc.facade.client.impl;
import com.nisum.poc.facade.client.IProjectClient;
import com.nisum.poc.facade.response.EmployeeDetails;
import com.nisum.poc.facade.response.ProjectDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
@Repository
public class ProjectClient implements IProjectClient {
private WebClient.Builder projectWebClient;
@Autowired
public ProjectClient(@Qualifier("projectWebClient") WebClient.Builder projectWebClient) {
this.projectWebClient = projectWebClient;
}
@Override
public Mono<ProjectDetails> findProjectDetails(Long projectId) {
return projectWebClient.build().get()
.uri(uriBuilder -> uriBuilder.path("/v1/find/{projectId}").build(projectId))
.retrieve().bodyToMono(ProjectDetails.class);
}
}
package com.nisum.poc.facade.controller;
import com.nisum.poc.facade.response.EmployeeDetails;
import com.nisum.poc.facade.response.EmployeeFacadeResponse;
import com.nisum.poc.facade.service.IEmployeeService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
......@@ -30,7 +31,7 @@ public class EmployeeFacadeController {
* @return
*/
@GetMapping(value = "/v1/{empId}/employee")
public Mono<EmployeeDetails> fetchEmployeeById(@PathVariable Long empId){
public Mono<EmployeeFacadeResponse> fetchEmployeeById(@PathVariable Long empId){
return employeeService.fetchEmployeeById(empId);
}
}
......@@ -16,4 +16,7 @@ public class EmployeeDetails {
private String lastName;
private String email;
private String phoneNumber;
private Long deptId;
private Long projectId;
}
package com.nisum.poc.facade.response;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class EmployeeFacadeResponse {
private Long employeeId;
private String firstName;
private String lastName;
private String email;
private String phoneNumber;
private DepartmentDetails departmentDetails;
private ProjectDetails projectDetails;
}
package com.nisum.poc.facade.service;
import com.nisum.poc.facade.response.EmployeeDetails;
import com.nisum.poc.facade.response.EmployeeFacadeResponse;
import org.springframework.web.bind.annotation.PathVariable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
......@@ -9,6 +10,6 @@ public interface IEmployeeService {
public Flux<EmployeeDetails> fetchEmployeeDetails();
public Mono<EmployeeDetails> fetchEmployeeById(Long empId);
public Mono<EmployeeFacadeResponse> fetchEmployeeById(Long empId);
}
package com.nisum.poc.facade.service.impl;
import com.nisum.poc.facade.client.IDepartmentClient;
import com.nisum.poc.facade.client.IEmployeeClient;
import com.nisum.poc.facade.client.IProjectClient;
import com.nisum.poc.facade.response.DepartmentDetails;
import com.nisum.poc.facade.response.EmployeeDetails;
import com.nisum.poc.facade.response.EmployeeFacadeResponse;
import com.nisum.poc.facade.response.ProjectDetails;
import com.nisum.poc.facade.service.IEmployeeService;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
......@@ -11,11 +18,17 @@ import reactor.core.publisher.Mono;
@Service
public class EmployeeService implements IEmployeeService {
private IEmployeeClient employeeClient;
private final IEmployeeClient employeeClient;
private final IDepartmentClient departmentClient;
private final IProjectClient projectClient;
@Autowired
public EmployeeService (IEmployeeClient employeeClient){
public EmployeeService (IEmployeeClient employeeClient, IDepartmentClient departmentClient, IProjectClient projectClient){
this.employeeClient = employeeClient;
this.departmentClient = departmentClient;
this.projectClient = projectClient;
}
@Override
......@@ -24,8 +37,45 @@ public class EmployeeService implements IEmployeeService {
}
@Override
public Mono<EmployeeDetails> fetchEmployeeById(Long empId) {
Mono<EmployeeDetails> employeeDetails = employeeClient.findEmployeeDetails(empId);
return null;
public Mono<EmployeeFacadeResponse> fetchEmployeeById(Long empId) {
Mono<EmployeeDetails> monoEmployeeDetails = employeeClient.findEmployeeDetails(empId);
return monoEmployeeDetails.map(this ::findProjectAndDeptDetails);
}
private EmployeeFacadeResponse findProjectAndDeptDetails(EmployeeDetails employeeDetails){
EmployeeFacadeResponse response = constructEmployeeFacadeResponse(employeeDetails);
Long deptId = employeeDetails.getDeptId();
Long projectId = employeeDetails.getProjectId();
departmentClient.fetchDepartmentById(deptId).map(dept -> getFacadeResponse(dept, response));
projectClient.findProjectDetails(projectId).map(proj -> getFacadeResponse(proj, response));
return response;
}
private Object getFacadeResponse(ProjectDetails project, EmployeeFacadeResponse response) {
response.setProjectDetails(project);
return response;
}
private Object getFacadeResponse(DepartmentDetails dept, EmployeeFacadeResponse response) {
response.setDepartmentDetails(dept);
return response;
}
private EmployeeFacadeResponse constructEmployeeFacadeResponse(EmployeeDetails employeeDetails) {
EmployeeFacadeResponse employeeFacadeResponse;
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
employeeFacadeResponse = modelMapper.map(employeeDetails, EmployeeFacadeResponse.class);
return employeeFacadeResponse;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment