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

Commit

parent c28847ef
...@@ -42,6 +42,11 @@ ...@@ -42,6 +42,11 @@
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>3.1.0</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
......
...@@ -22,13 +22,13 @@ public class EmployeeServiceFacadeApplication { ...@@ -22,13 +22,13 @@ public class EmployeeServiceFacadeApplication {
return WebClient.builder().baseUrl("lb://employee-ws/employee/"); return WebClient.builder().baseUrl("lb://employee-ws/employee/");
} }
@Bean(value = "depWebClient") @Bean(value = "departmentWebClient")
@LoadBalanced @LoadBalanced
public WebClient.Builder getDepWebClient(){ public WebClient.Builder getDepWebClient(){
return WebClient.builder().baseUrl("lb://department-ws/department/"); return WebClient.builder().baseUrl("lb://department-ws/department/");
} }
@Bean(value = "projWebClient") @Bean(value = "projectWebClient")
@LoadBalanced @LoadBalanced
public WebClient.Builder getProjWebClient(){ public WebClient.Builder getProjWebClient(){
return WebClient.builder().baseUrl("lb://project-ws/project/"); return WebClient.builder().baseUrl("lb://project-ws/project/");
......
package com.nisum.poc.facade.client; package com.nisum.poc.facade.client;
import com.nisum.poc.facade.response.DepartmentDetails; 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 interface IDepartmentClient {
public DepartmentDetails fetchDepartmentById(Long deptId); public Mono<DepartmentDetails> fetchDepartmentById(Long deptId);
} }
package com.nisum.poc.facade.client; package com.nisum.poc.facade.client;
import com.nisum.poc.facade.response.ProjectDetails;
import reactor.core.publisher.Mono;
public interface IProjectClient { public interface IProjectClient {
public Mono<ProjectDetails> findProjectDetails(Long projectId);
} }
...@@ -6,20 +6,23 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -6,20 +6,23 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
@Repository @Repository
public class DepartmentClient implements IDepartmentClient { public class DepartmentClient implements IDepartmentClient {
private WebClient.Builder employeeClient; private final WebClient.Builder departmentWebClient;
@Autowired @Autowired
public DepartmentClient(@Qualifier("depWebClient") WebClient.Builder employeeClient) { public DepartmentClient(@Qualifier("departmentWebClient") WebClient.Builder departmentWebClient) {
this.employeeClient = employeeClient; this.departmentWebClient = departmentWebClient;
} }
@Override @Override
public DepartmentDetails fetchDepartmentById(Long deptId) { public Mono<DepartmentDetails> fetchDepartmentById(Long departmentId) {
return null; 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; ...@@ -13,22 +13,22 @@ import reactor.core.publisher.Mono;
@Repository @Repository
public class EmployeeClient implements IEmployeeClient { public class EmployeeClient implements IEmployeeClient {
private WebClient.Builder employeeClient; private WebClient.Builder employeeWebClient;
@Autowired @Autowired
public EmployeeClient(@Qualifier("empWebClient") WebClient.Builder employeeClient) { public EmployeeClient(@Qualifier("empWebClient") WebClient.Builder employeeWebClient) {
this.employeeClient = employeeClient; this.employeeWebClient = employeeWebClient;
} }
@Override @Override
public Flux<EmployeeDetails> fetchAllEmployee() { 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 @Override
public Mono<EmployeeDetails> findEmployeeDetails(Long empId) { public Mono<EmployeeDetails> findEmployeeDetails(Long empId) {
return employeeClient.build().get() return employeeWebClient.build().get()
.uri(uriBuilder -> uriBuilder.path("/v1/{empId}/details").build(empId)) .uri(uriBuilder -> uriBuilder.path("/v1/{empId}/details").build(empId))
.retrieve().bodyToMono(EmployeeDetails.class); .retrieve().bodyToMono(EmployeeDetails.class);
} }
......
package com.nisum.poc.facade.client.impl; package com.nisum.poc.facade.client.impl;
import com.nisum.poc.facade.client.IProjectClient; 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.stereotype.Repository;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
@Repository @Repository
public class ProjectClient implements IProjectClient { 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; package com.nisum.poc.facade.controller;
import com.nisum.poc.facade.response.EmployeeDetails; import com.nisum.poc.facade.response.EmployeeDetails;
import com.nisum.poc.facade.response.EmployeeFacadeResponse;
import com.nisum.poc.facade.service.IEmployeeService; import com.nisum.poc.facade.service.IEmployeeService;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
...@@ -30,7 +31,7 @@ public class EmployeeFacadeController { ...@@ -30,7 +31,7 @@ public class EmployeeFacadeController {
* @return * @return
*/ */
@GetMapping(value = "/v1/{empId}/employee") @GetMapping(value = "/v1/{empId}/employee")
public Mono<EmployeeDetails> fetchEmployeeById(@PathVariable Long empId){ public Mono<EmployeeFacadeResponse> fetchEmployeeById(@PathVariable Long empId){
return employeeService.fetchEmployeeById(empId); return employeeService.fetchEmployeeById(empId);
} }
} }
...@@ -16,4 +16,7 @@ public class EmployeeDetails { ...@@ -16,4 +16,7 @@ public class EmployeeDetails {
private String lastName; private String lastName;
private String email; private String email;
private String phoneNumber; 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; package com.nisum.poc.facade.service;
import com.nisum.poc.facade.response.EmployeeDetails; import com.nisum.poc.facade.response.EmployeeDetails;
import com.nisum.poc.facade.response.EmployeeFacadeResponse;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
...@@ -9,6 +10,6 @@ public interface IEmployeeService { ...@@ -9,6 +10,6 @@ public interface IEmployeeService {
public Flux<EmployeeDetails> fetchEmployeeDetails(); public Flux<EmployeeDetails> fetchEmployeeDetails();
public Mono<EmployeeDetails> fetchEmployeeById(Long empId); public Mono<EmployeeFacadeResponse> fetchEmployeeById(Long empId);
} }
package com.nisum.poc.facade.service.impl; 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.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.EmployeeDetails;
import com.nisum.poc.facade.response.EmployeeFacadeResponse;
import com.nisum.poc.facade.response.ProjectDetails;
import com.nisum.poc.facade.service.IEmployeeService; 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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
...@@ -11,11 +18,17 @@ import reactor.core.publisher.Mono; ...@@ -11,11 +18,17 @@ import reactor.core.publisher.Mono;
@Service @Service
public class EmployeeService implements IEmployeeService { public class EmployeeService implements IEmployeeService {
private IEmployeeClient employeeClient; private final IEmployeeClient employeeClient;
private final IDepartmentClient departmentClient;
private final IProjectClient projectClient;
@Autowired @Autowired
public EmployeeService (IEmployeeClient employeeClient){ public EmployeeService (IEmployeeClient employeeClient, IDepartmentClient departmentClient, IProjectClient projectClient){
this.employeeClient = employeeClient; this.employeeClient = employeeClient;
this.departmentClient = departmentClient;
this.projectClient = projectClient;
} }
@Override @Override
...@@ -24,8 +37,45 @@ public class EmployeeService implements IEmployeeService { ...@@ -24,8 +37,45 @@ public class EmployeeService implements IEmployeeService {
} }
@Override @Override
public Mono<EmployeeDetails> fetchEmployeeById(Long empId) { public Mono<EmployeeFacadeResponse> fetchEmployeeById(Long empId) {
Mono<EmployeeDetails> employeeDetails = employeeClient.findEmployeeDetails(empId);
return null;
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