Commit b421c25e authored by Syed Javed Ali's avatar Syed Javed Ali

Added map(), flatmap(), filter operators to the employee controller class

parent 7036f8c9
......@@ -4,7 +4,6 @@ import com.nisum.example.mongodb.model.Employee;
import com.nisum.example.mongodb.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
......@@ -27,6 +26,7 @@ public class EmployeeController {
@PostMapping(path = "/create")
@ResponseStatus(HttpStatus.CREATED)
public Mono<Employee> create(@RequestBody Employee e) {
return employeeService.insert(e).log();
}
......@@ -36,10 +36,12 @@ public class EmployeeController {
* @return Mono
*/
@GetMapping("/{id}")
public ResponseEntity<Mono<Employee>> findById(@PathVariable("id") Integer id) {
Mono<Employee> emp = employeeService.fetchById(id).log();
HttpStatus status = emp!=null ?HttpStatus.OK:HttpStatus.NOT_FOUND;
return new ResponseEntity<Mono<Employee>>(emp, status);
public Mono<ResponseEntity<Employee>> findById(@PathVariable("id") Integer id) {
return employeeService.fetchById(id)
.log()
.map(employee -> new ResponseEntity<>(employee,HttpStatus.OK))
.log()
.defaultIfEmpty(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
/**
......@@ -48,10 +50,8 @@ public class EmployeeController {
* @return Flux
*/
@GetMapping("/name/{name}")
public ResponseEntity<Flux<Employee>> findByName(@PathVariable("name") String name) {
Flux<Employee> employeeFlux=employeeService.fetchByName(name).log();
HttpStatus status=employeeFlux!=null?HttpStatus.OK:HttpStatus.NOT_FOUND;
return new ResponseEntity<>(employeeFlux,status);
public Flux<Employee> findByName(@PathVariable("name") String name) {
return employeeService.fetchByName(name).log();
}
......@@ -59,9 +59,11 @@ public class EmployeeController {
* GET method to fetch all records
* @return Flux
*/
@GetMapping(value = "", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
@GetMapping(value = "")
public Flux<Employee> findAll() {
Flux<Employee> emps = employeeService.fetchAll().log();
Flux<Employee> emps = employeeService.fetchAll()
.log()
.filter(employee -> employee.getSalary()>2000);
return emps;
}
......@@ -75,6 +77,20 @@ public class EmployeeController {
return employeeService.modify(emp).log();
}
@PutMapping("/{id}")
public Mono<ResponseEntity<Employee>> updateById(@PathVariable("id") Integer id,
@RequestBody Employee employee){
return employeeService.modifyById(id)
.flatMap(emp->{
emp.setSalary(employee.getSalary());
return employeeService.insert(emp);
})
.log()
.map(updateEmp-> new ResponseEntity<>(updateEmp,HttpStatus.OK))
.log()
.defaultIfEmpty(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
/**
* DELETE method to remove record based on Id
* @param id
......
......@@ -50,19 +50,18 @@ public class EmployeeServiceImpl implements IEmployeeService {
return employeeRepository.findByName(name);
}
@Override
public Mono<Employee> modifyById(Integer id) {
return employeeRepository.findById(id);
}
/**
* perform fetch all records
* @return Flux
*/
@Override
public Flux<Employee> fetchAll() {
return employeeRepository.findAll()
/*.delayElements(Duration.ofSeconds(1)).map(
employee -> {
employee.setSalary(4000L);
System.out.println(employee);
return employee;
})*/;
return employeeRepository.findAll();
}
/**
......
......@@ -11,5 +11,6 @@ public interface IEmployeeService {
Flux<Employee> fetchByName(String name);
Flux<Employee> fetchAll();
Mono<Employee> modify(Employee emp);
Mono<Employee> modifyById(Integer id);
Mono<Void> removeById(Integer id);
}
......@@ -126,8 +126,7 @@ public class EmployeeControllerTest {
.uri("")
.exchange()
.expectStatus().isOk()
.expectBodyList(Employee.class)
.hasSize(2);
.expectBodyList(Employee.class);
Mockito.verify(repository,times(1)).findAll();
}
......
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