Created
November 6, 2015 13:06
-
-
Save javaeeeee/9c15264afc4017624852 to your computer and use it in GitHub Desktop.
A database-backed resource class for a Dropwizard application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Path("/employees") | |
| @Produces(MediaType.APPLICATION_JSON) | |
| public class EmployeesResource { | |
| /** | |
| * The DAO object to manipulate employees. | |
| */ | |
| private EmployeeDAO employeeDAO; | |
| /** | |
| * Constructor. | |
| * | |
| * @param employeeDAO DAO object to manipulate employees. | |
| */ | |
| public EmployeesResource(EmployeeDAO employeeDAO) { | |
| this.employeeDAO = employeeDAO; | |
| } | |
| /** | |
| * Looks for employees whose first or last name contains the passed | |
| * parameter as a substring. If name argument was not passed, returns all | |
| * employees stored in the database. | |
| * | |
| * @param name query parameter | |
| * @return list of employees whose first or last name contains the passed | |
| * parameter as a substring or list of all employees stored in the database. | |
| */ | |
| @GET | |
| @UnitOfWork | |
| public List<Employee> findByName( | |
| @QueryParam("name") Optional<String> name | |
| ) { | |
| if (name.isPresent()) { | |
| return employeeDAO.findByName(name.get()); | |
| } else { | |
| return employeeDAO.findAll(); | |
| } | |
| } | |
| /** | |
| * Method looks for an employee by her id. | |
| * | |
| * @param id the id of an employee we are looking for. | |
| * @return Optional containing the found employee or an empty Optional | |
| * otherwise. | |
| */ | |
| @GET | |
| @Path("/{id}") | |
| @UnitOfWork | |
| public Optional<Employee> findById(@PathParam("id") LongParam id) { | |
| return employeeDAO.findById(id.get()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment