package com.nisum.epe.demo.srinivas;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@RestController
public class Resource {
    List<Employee> employees = new ArrayList<>();

    @RequestMapping("/saveEmployeeDetails")
    public void saveEmployeeDetails(@RequestBody Employee employee) {

        //System.out.println("Save Employee details");
        buildEmployeeDetails().add(employee);
        employees.forEach(System.out::println);
        //System.out.println(employee.toString());
    }

    @RequestMapping("/getEmployeeDetails")
    public void getmployeeDetails(@RequestParam String employeeName) {
        //System.out.println("employeeName = " + employeeName);
        List<Employee> employeeList = buildEmployeeDetails().stream()
                .filter(employee -> employee.getEmpName().equalsIgnoreCase(employeeName))
                .collect(Collectors.toList());
        employeeList.forEach(System.out::println);
    //    System.out.println(employeeName);
    }

    private List<Employee> buildEmployeeDetails() {

        IntStream.range(1, 100).forEach(
                value -> {
                    employees.add(new Employee("employee" + value, 1000 * value));
                }
        );

        return employees;
    }
}
