Skip to content

Instantly share code, notes, and snippets.

@RichardHightower
Created August 21, 2015 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardHightower/d52358dc3393464e32fd to your computer and use it in GitHub Desktop.
Save RichardHightower/d52358dc3393464e32fd to your computer and use it in GitHub Desktop.
resource-url-example
@RequestMapping("/hr")
public class HRService {
final Map<Integer, Department> departmentMap = new HashMap<>();
@RequestMapping("/department/")
public List<Department> getDepartments() {
return new ArrayList<>(departmentMap.values());
}
@RequestMapping(value = "/department/{departmentId}/", method = RequestMethod.POST)
public boolean addDepartment(@PathVariable("departmentId") Integer departmentId,
final Department department) {
departmentMap.put(departmentId, department);
return true;
}
@RequestMapping(value = "/department/{departmentId}/employee/", method = RequestMethod.POST)
public boolean addEmployee(@PathVariable("departmentId") Integer departmentId,
final Employee employee) {
final Department department = departmentMap.get(departmentId);
if (department == null) {
throw new IllegalArgumentException("Department " + departmentId + " does not exist");
}
department.addEmployee(employee);
return true;
}
@RequestMapping(value = "/department/{departmentId}/employee/{employeeId}", method = RequestMethod.GET)
public Employee getEmployee(@PathVariable("departmentId") Integer departmentId,
@PathVariable("employeeId") Long employeeId) {
final Department department = departmentMap.get(departmentId);
if (department == null) {
throw new IllegalArgumentException("Department " + departmentId + " does not exist");
}
Optional<Employee> employee = department.getEmployeeList().stream().filter(
employee1 -> employee1.getId() == employeeId).findFirst();
if (employee.isPresent()){
return employee.get();
} else {
throw new IllegalArgumentException("Employee with id " + employeeId + " Not found ");
}
}
@RequestMapping(value = "/department/{departmentId}/employee/{employeeId}/phoneNumber/",
method = RequestMethod.POST)
public boolean addPhoneNumber(@PathVariable("departmentId") Integer departmentId,
@PathVariable("employeeId") Long employeeId,
PhoneNumber phoneNumber) {
Employee employee = getEmployee(departmentId, employeeId);
employee.addPhoneNumber(phoneNumber);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment