package com.nisum.mongodb.controller;

import com.nisum.mongodb.model.Employee;
import com.nisum.mongodb.service.EmployeeListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;



import java.util.List;

@RestController
@RequestMapping("api/v2")
public class EmployeeListController {

    @Autowired
    EmployeeListService employeeListService;

    @GetMapping("/employees")
    public List<Employee> getAllEmployees() {
        return employeeListService.findAllEmployees();
    }

    @PostMapping("/employees")
    public Employee createEmployee(@RequestBody Employee employee) {
        return employeeListService.addEmployee(employee);
    }

    @GetMapping("/employees/{id}")
    public ResponseEntity<Employee> getEmployeeById(@PathVariable(value = "id") Long employeeId) {
        Employee employee = employeeListService.getEmployeeById(employeeId).get();
        return ResponseEntity.ok().body(employee);
    }

     @DeleteMapping("/employees/{id}")
    public void deleteEmployee(@PathVariable(value = "id") Long employeeId){
        employeeListService.deleteEmployee(employeeId);

    }

    @PutMapping ("/employees/{id}")
    public Employee updateEmployee(@PathVariable(value = "id") Long employeeId,@RequestBody Employee employee) {
        return employeeListService.updateEmployee(employeeId, employee);
    }
}