Commit f5a9e3dc authored by Ravinder Pannala's avatar Ravinder Pannala

Added Real time scenarios for Webflux

parent 9df1fc68
package com.example.nisum.webfluxmongodb.zipping.DTO;
import com.example.nisum.webfluxmongodb.zipping.model.Employee;
import lombok.*;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter
public class OrgEmployeeDTO {
private String name;
private List<Employee> employeeList;
}
......@@ -2,6 +2,7 @@ package com.example.nisum.webfluxmongodb.zipping.controller;
import com.example.nisum.webfluxmongodb.zipping.DTO.DepartmentEmployeeDto;
import com.example.nisum.webfluxmongodb.zipping.model.Department;
import com.example.nisum.webfluxmongodb.zipping.model.Employee;
import com.example.nisum.webfluxmongodb.zipping.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
......@@ -10,6 +11,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;
import java.util.List;
@RestController
@RequestMapping("/api/dept")
......@@ -28,14 +32,21 @@ public class DepartmentController {
return departmentService.getAll();
}
@RequestMapping("/get/{orgId}")
@RequestMapping("/get/org/{orgId}")
private Flux<Department> getByOrgId(@PathVariable int orgId){
return departmentService.findByOrganizationId(orgId);
}
//Get Department by Id
@RequestMapping("/get/{deptId}")
private Mono<DepartmentEmployeeDto> findByDepartmentId(@PathVariable int id){
return departmentService.findById(id);
private Mono<DepartmentEmployeeDto> findByDepartmentId(@PathVariable int deptId){
return departmentService.findById(deptId);
}
@RequestMapping("/getAll")
private Flux<DepartmentEmployeeDto> findAllDepartmentAndEmployees(){
return departmentService.findAllDepartmentEmployees();
}
}
......@@ -28,12 +28,12 @@ public class EmployeeController {
}
@RequestMapping("/get/{orgId}")
@RequestMapping("/get/org/{orgId}")
private Flux<Employee> findByOrganizationId(@PathVariable int orgId) {
return employeeService.findByOrganizationId(orgId);
}
@RequestMapping("/get/{Id}")
@RequestMapping("/get/dept/{departmentId}")
private Flux<Employee> findByDepartmentId(@PathVariable int departmentId) {
return employeeService.findByDepartmentId(departmentId);
}
......
package com.example.nisum.webfluxmongodb.zipping.controller;
import com.example.nisum.webfluxmongodb.zipping.DTO.OrgEmployeeDTO;
import com.example.nisum.webfluxmongodb.zipping.Exception.ResourceNotFoundException;
import com.example.nisum.webfluxmongodb.zipping.model.Employee;
import com.example.nisum.webfluxmongodb.zipping.model.Organization;
import com.example.nisum.webfluxmongodb.zipping.service.EmployeeService;
import com.example.nisum.webfluxmongodb.zipping.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.BooleanOperators;
import org.springframework.web.bind.annotation.PathVariable;
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.client.ResourceAccessException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
......@@ -32,10 +27,22 @@ public class OrganizationController {
return organizationService.getAll();
}
@RequestMapping("/get/{id}")
//Get All employees based on Organization Id
@GetMapping("/get/{id}")
private Mono<Organization> findById(@PathVariable int id) {
Mono<Organization> organizationMono = organizationService.findById(id);
organizationMono.switchIfEmpty(Mono.error(new ResourceNotFoundException("Resource not found")));
return organizationMono;
}
//Get ALl EMployees by OrganizationName
@GetMapping("/getByName/{name}")
private Mono<OrgEmployeeDTO> findByOrgName(@PathVariable String name){
return organizationService.findByOrgName(name);
}
@DeleteMapping("/delete/{id}")
private Mono<ResponseEntity<Void>> delete(@PathVariable int id){
return organizationService.deleteById(id);
}
}
package com.example.nisum.webfluxmongodb.zipping.repository;
import com.example.nisum.webfluxmongodb.zipping.model.Employee;
import com.example.nisum.webfluxmongodb.zipping.model.Organization;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.ResponseStatus;
import reactor.core.publisher.Mono;
@Repository
public interface OrganizationRepository extends ReactiveMongoRepository<Organization,Integer> {
Mono<Organization> findByName(String name);
}
package com.example.nisum.webfluxmongodb.zipping.service;
import com.example.nisum.webfluxmongodb.zipping.DTO.DepartmentEmployeeDto;
import com.example.nisum.webfluxmongodb.zipping.Exception.ResourceNotFoundException;
import com.example.nisum.webfluxmongodb.zipping.model.Department;
import com.example.nisum.webfluxmongodb.zipping.model.Employee;
import com.example.nisum.webfluxmongodb.zipping.repository.DepartmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.Disposable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;
import java.util.ArrayList;
import java.util.List;
@Service
......@@ -37,19 +34,42 @@ public class DepartmentService {
}
public Mono<DepartmentEmployeeDto> findById(int id) {
List<Employee> employees = new ArrayList<>();
Mono<DepartmentEmployeeDto> map = departmentRepository
.findById(id).zipWhen(dept -> {
Flux<Employee> employeeFlux = employeeService.findByDepartmentId(dept.getId()).switchIfEmpty(Flux.just(new Employee()));
Mono<DepartmentEmployeeDto> map = departmentRepository.findById(id).zipWhen(dept -> employeeService.findByDepartmentId(dept.getId()).collectList()).map(tuple -> {
DepartmentEmployeeDto departmentEmployeeDto = new DepartmentEmployeeDto();
Department dept = tuple.getT1();
List<Employee> employeeList = tuple.getT2();
departmentEmployeeDto.setName(dept.getName());
departmentEmployeeDto.setEmployeeList(employeeList);
return departmentEmployeeDto;
});
return map;
}
public Flux<DepartmentEmployeeDto> findAllDepartmentEmployees() {
Flux<DepartmentEmployeeDto> map = departmentRepository.findAll().flatMap(this::apply)
.map(tuple -> {
DepartmentEmployeeDto departmentEmployeeDto = new DepartmentEmployeeDto();
departmentEmployeeDto.setName(dept.getName());
employeeFlux.collectList().subscribe(employees::addAll);
departmentEmployeeDto.setEmployeeList(employees);
return Mono.just(departmentEmployeeDto);
}).switchIfEmpty(Mono.error(new ResourceNotFoundException("Department id not found"))).map(tuple -> {
DepartmentEmployeeDto t2 = tuple.getT2();
return t2;
departmentEmployeeDto.setName(tuple.getT1().getName());
departmentEmployeeDto.setEmployeeList(tuple.getT2());
return departmentEmployeeDto;
});
return map;
}
private Mono<Tuple2<Department, List<Employee>>> apply(Department department) {
return Mono.just(department)
.zipWith(employeeService.getAll().filter(it -> it.getDepartmentId() == department.getId()).collectList())
.map(tuple -> {
tuple.getT1().setEmployees(tuple.getT2());
return tuple;
});
}
public Mono<Void> deleteDepartment(Department dept) {
return departmentRepository.delete(dept);
}
public Mono<Void> delete(Flux<Department> departmentFlux) {
return departmentRepository.deleteAll(departmentFlux);
}
}
......@@ -22,10 +22,19 @@ public class EmployeeService {
}
public Flux<Employee> findByOrganizationId(int orgId) {
return employeeRepository.findByOrganizationId(orgId);
Flux<Employee> employeeFlux = employeeRepository.findByOrganizationId(orgId);
return employeeFlux;
}
public Flux<Employee> findByDepartmentId(int departmentId) {
return employeeRepository.findByDepartmentId(departmentId);
}
public Mono<Void> deleteEmployee(Employee emp) {
return employeeRepository.delete(emp);
}
public Mono<Void> delete(Flux<Employee> employeeFlux) {
return employeeRepository.deleteAll(employeeFlux);
}
}
package com.example.nisum.webfluxmongodb.zipping.service;
import com.example.nisum.webfluxmongodb.zipping.DTO.OrgEmployeeDTO;
import com.example.nisum.webfluxmongodb.zipping.Exception.ResourceNotFoundException;
import com.example.nisum.webfluxmongodb.zipping.model.Department;
import com.example.nisum.webfluxmongodb.zipping.model.Employee;
import com.example.nisum.webfluxmongodb.zipping.model.Organization;
import com.example.nisum.webfluxmongodb.zipping.repository.OrganizationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;
import java.util.ArrayList;
import java.util.List;
@Service
......@@ -49,4 +51,31 @@ public class OrganizationService {
return organizationMono;
}
public Mono<OrgEmployeeDTO> findByOrgName(String name) {
Mono<Tuple2<Organization, List<Employee>>> tuple2Mono = organizationRepository.findByName(name)
.switchIfEmpty(Mono.error(new ResourceNotFoundException("Organization details not found with Name")))
.zipWhen(org -> employeeService.findByOrganizationId(org.getId()).collectList());
Mono<OrgEmployeeDTO> orgEmployeeDTOMono = tuple2Mono.switchIfEmpty(Mono.error(new ResourceNotFoundException("Employee details not found"))).map(tuple -> {
Organization org = tuple.getT1();
List<Employee> employeeList = tuple.getT2();
OrgEmployeeDTO orgEmployeeDTO = new OrgEmployeeDTO();
orgEmployeeDTO.setName(org.getName());
orgEmployeeDTO.setEmployeeList(employeeList);
return orgEmployeeDTO;
});
return orgEmployeeDTOMono;
}
public Mono<ResponseEntity<Void>> deleteById(int id) {
return organizationRepository.findById(id).flatMap(org -> {
Flux<Department> departmentFlux = departmentService.findByOrganizationId(org.getId());
departmentService.delete(departmentFlux);
Flux<Employee> employeeFlux = employeeService.findByOrganizationId(org.getId());
Mono<Void> delete = employeeService.delete(employeeFlux);
return organizationRepository.delete(org).then(Mono.just(new ResponseEntity<Void>(HttpStatus.OK)));
}).defaultIfEmpty(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
}
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