@RequestMapping( value = "/doctors", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF_8) public PaginationResponse<DoctorDto> getDoctors( @RequestParam(value = "limit", required = false) Integer limit, @RequestParam(value = "offset", required = false) Integer offset) { LOGGER.debug("Retrieving doctors..."); int fistResult = FIRST_RESULT; if (offset != null) { fistResult = offset; } int maxResult = MAX_RESULT; if (limit != null) { if (limit > MAX_RESULT) { ValidationException exception = new ValidationException(); exception.errorField("limit", "Could not be greater than " + MAX_RESULT); throw exception; } maxResult = limit; } LOGGER.debug("Retrieve doctors from:{}, limit:{}", fistResult, maxResult); PaginationResponse<DoctorDto> response = new PaginationResponse<>(); List<Doctor> doctors = doctorService.find(fistResult, maxResult); if (CollectionUtils.isNotEmpty(doctors)) { for (Doctor doctor : doctors) { response.addItem(new DoctorDto(doctor)); } } Paging paging = new Paging(); paging.setLimit(maxResult); paging.setOffset(fistResult); paging.setTotal(doctorService.count()); response.setPaging(paging); return response; }
@RequestMapping( value = "/doctors/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF_8) public DoctorDto getDoctor(@PathVariable("id") Long id) { LOGGER.debug("Retrieving doctor: {}", id); Doctor doctor = doctorService.findById(id); if (doctor == null) { LOGGER.debug("Could not found doctor: {}", id); EntityNotFoundException exception = new EntityNotFoundException(Doctor.ENTITY); exception.setSearchMessage("id = " + id); throw exception; } return new DoctorDto(doctor); }
@RequestMapping( value = "/doctors/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON_UTF_8) public DoctorResponse updateDoctor( @PathVariable("id") Long id, @Valid @RequestBody DoctorRequest doctorRequest) { LOGGER.debug("Updating doctor: {}", id); Doctor doctor = doctorService.findById(id); if (doctor == null) { LOGGER.debug("Could not found doctor: {}", id); EntityNotFoundException exception = new EntityNotFoundException(Doctor.ENTITY); exception.setSearchMessage("id = " + id); throw exception; } saveOrUpdate(doctor, doctorRequest); LOGGER.debug("Updated doctor: {}", id); return new DoctorResponse(id); }
private void saveOrUpdate(Doctor doctor, DoctorRequest doctorRequest) { doctor.setFirstName(doctorRequest.getFirstName()); doctor.setLastName(doctorRequest.getLastName()); doctor.setEmail(doctorRequest.getEmail()); doctorService.save(doctor); }