-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmployeeController.java
180 lines (162 loc) · 7.1 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package kp.company.controller;
import java.lang.invoke.MethodHandles;
import java.net.URI;
import java.util.Optional;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.IanaLinkRelations;
import org.springframework.hateoas.PagedModel;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import kp.Constants;
import kp.SampleDataset;
import kp.company.assembler.EmployeeAssembler;
import kp.company.domain.Department;
import kp.company.domain.Employee;
/**
* The HATEOAS RESTful web service controller for the {@link Employee}.
*
*/
@RestController
@RequestMapping(Constants.COMPANY_PATH)
public class EmployeeController {
private static final Log logger = LogFactory.getLog(MethodHandles.lookup().lookupClass().getName());
private final EmployeeAssembler assembler;
/**
* The constructor.
*
* @param assembler the {@link EmployeeAssembler}
*/
public EmployeeController(EmployeeAssembler assembler) {
super();
this.assembler = assembler;
}
/**
* Creates a new {@link Employee}.
*
* @param employeeOpt the {@link Optional} with the {@link Employee}
* @return the {@link ResponseEntity} with the {@link EntityModel} for the
* {@link Employee}
*/
@PostMapping(Constants.EMPLOYEES_PATH)
public ResponseEntity<EntityModel<Employee>> createEmployee(@RequestBody Optional<Employee> employeeOpt) {
if (employeeOpt.isEmpty()) {
logger.error("createEmployee(): bad request - empty request body");
return ResponseEntity.badRequest().build();
}
final Optional<Department> departmentOpt = employeeOpt.map(Employee::getDepartment).map(Department::getId)
.flatMap(SampleDataset::findDepartmentById);
if (departmentOpt.isEmpty()) {
logger.error("createEmployee(): not found - employee has not existing department");
return ResponseEntity.notFound().build();
}
final ResponseEntity<EntityModel<Employee>> responseEntity = saveEmployee(employeeOpt.get());
logger.info("createEmployee():");
return responseEntity;
}
/**
* Reads the {@link Employee} with given id.
*
* @param idOpt the {@link Optional} with the {@link Employee} id
* @return the {@link ResponseEntity} with the {@link EntityModel} for the
* {@link Employee}
*/
@GetMapping(Constants.EMPLOYEE_ID_PATH)
public ResponseEntity<EntityModel<Employee>> findEmployeeById(@PathVariable("id") Optional<Long> idOpt) {
if (idOpt.isEmpty()) {
logger.error("findEmployeeById(): bad request - empty id parameter");
return ResponseEntity.badRequest().build();
}
final ResponseEntity<EntityModel<Employee>> responseEntity = SampleDataset.findEmployeeById(idOpt.get())
.map(assembler::toModel).map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
logger.info(String.format("findEmployeeById(): employee id[%d]", idOpt.get()));
return responseEntity;
}
/**
* Reads all {@link Employee}s.
*
* @return the {@link ResponseEntity} with the {@link PagedModel}
*/
@GetMapping(Constants.EMPLOYEES_PATH)
public ResponseEntity<CollectionModel<EntityModel<Employee>>> findAllEmployees() {
final ResponseEntity<CollectionModel<EntityModel<Employee>>> responseEntity = SampleDataset.findAllEmployees()
.map(assembler::toCollectionModel).map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
logger.info("findAllEmployees():");
return responseEntity;
}
/**
* Updates the {@link Employee} with given id.
*
* @param employeeOpt the {@link Optional} with the {@link Employee}
* @param idOpt the {@link Optional} with the {@link Employee} id
* @return the {@link ResponseEntity} with the {@link EntityModel} for the
* {@link Employee}
*/
@PatchMapping(Constants.EMPLOYEE_ID_PATH)
public ResponseEntity<EntityModel<Employee>> updateEmployee(@RequestBody Optional<Employee> employeeOpt,
@PathVariable("id") Optional<Long> idOpt) {
if (employeeOpt.isEmpty() || idOpt.isEmpty()) {
logger.error("updateEmployee(): bad request - empty request body or empty parameter");
return ResponseEntity.badRequest().build();
}
final boolean departmentPresent = employeeOpt.map(Employee::getDepartment).map(Department::getId)
.map(SampleDataset::findDepartmentById).isPresent();
if (!departmentPresent) {
logger.error("updateEmployee(): not found - employee has not existing department");
return ResponseEntity.notFound().build();
}
final ResponseEntity<EntityModel<Employee>> responseEntity = SampleDataset.findEmployeeById(idOpt.get())
.map(emp -> new Employee(emp.getId(), //
employeeOpt.map(Employee::getFirstName).orElse(emp.getFirstName()),
employeeOpt.map(Employee::getLastName).orElse(emp.getLastName()),
employeeOpt.map(Employee::getDepartment).orElse(emp.getDepartment())))
.map(this::saveEmployee).orElseGet(() -> ResponseEntity.notFound().build());
logger.info(String.format("updateEmployee(): employee id[%d]", employeeOpt.get().getId()));
return responseEntity;
}
/**
* Deletes the {@link Employee} with given id.
*
* @param idOpt the {@link Optional} with the {@link Employee} id
* @return the {@link ResponseEntity}
*/
@DeleteMapping(Constants.EMPLOYEE_ID_PATH)
public ResponseEntity<EntityModel<Employee>> deleteEmployee(@PathVariable("id") Optional<Long> idOpt) {
if (idOpt.isEmpty()) {
logger.error("deleteEmployee(): bad request - empty id parameter");
return ResponseEntity.badRequest().build();
}
SampleDataset.deleteEmployeeById(idOpt.get());
final ResponseEntity<EntityModel<Employee>> responseEntity = ResponseEntity.noContent().build();
logger.info(String.format("deleteEmployee(): employee id[%d]", idOpt.get()));
return responseEntity;
}
/**
* Saves the {@link Employee}
*
* @param employee the {@link Employee}
* @return the {@link ResponseEntity} with the {@link EntityModel} for the
* {@link Employee}
*/
private ResponseEntity<EntityModel<Employee>> saveEmployee(Employee employee) {
try {
SampleDataset.saveEmployee(employee);
final EntityModel<Employee> entityModel = assembler.toModel(employee);
final URI uri = new URI(entityModel.getRequiredLink(IanaLinkRelations.SELF).getHref());
return ResponseEntity.created(uri).body(entityModel);
} catch (Exception e) {
logger.error(
String.format("saveEmployee(): exception[%s], employee id[%d]", e.getMessage(), employee.getId()));
return ResponseEntity.internalServerError().build();
}
}
}