-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathEmployeeController.java
74 lines (59 loc) · 2.35 KB
/
EmployeeController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.example.demo.controller;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.dto.EmployeeDto;
import com.example.demo.model.Employee;
import com.example.demo.service.EmployeeService;
import lombok.RequiredArgsConstructor;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/")
public class EmployeeController {
private final EmployeeService employeeService;
// get all data
@CrossOrigin(origins = "http://localhost:4200")
@GetMapping("/employees")
public List<Employee> getAllEmployees() {
return employeeService.findAllEmployee();
}
// create
@CrossOrigin(origins = "http://localhost:4200")
@PostMapping("/employees")
public Employee createEmployee(@RequestBody EmployeeDto employee)
{
return employeeService.addEmployee(employee);
}
// get data by id
@CrossOrigin(origins = "http://localhost:4200")
@GetMapping("/employees/{id}")
public ResponseEntity<Employee> getByID(@PathVariable Long id) {
return new ResponseEntity<>(employeeService.getEmployeeById(id), HttpStatus.OK);
}
// update data
@CrossOrigin(origins = "http://localhost:4200")
@PutMapping("/employees/{id}")
public ResponseEntity<Employee> updateEmployeeByID(@PathVariable Long id,
@RequestBody EmployeeDto employeeDetails) {
return new ResponseEntity<>(employeeService.updateEmployee(id, employeeDetails), HttpStatus.OK);
}
@CrossOrigin(origins = "http://localhost:4200")
@DeleteMapping("/employees/{id}")
public ResponseEntity<Map<String, Boolean>> deleteEmployee(@PathVariable Long id) {
employeeService.deleteEmployee(id);
Map<String, Boolean> response = new HashMap<>();
response.put("Deleted", Boolean.TRUE);
return new ResponseEntity<>(response,HttpStatus.OK);
}
}