package org.synyx.urlaubsverwaltung.person.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.http.client.utils.URIBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Link;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.synyx.urlaubsverwaltung.person.Person;
import org.synyx.urlaubsverwaltung.person.PersonService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Api("Persons: Get information about the persons of the application")
@RestController("restApiPersonController")
@RequestMapping(PersonApiController.ROOT_URL)
public class PersonApiController {

    static final String ROOT_URL = "/api/persons";
    private static final String PERSON_URL = "/{id}";

    private final PersonService personService;

    @Autowired
    PersonApiController(PersonService personService) {

        this.personService = personService;
    }

    @ApiOperation(
        value = "Get all active persons of the application", notes = "Get all active persons of the application"
    )
    @GetMapping
    public ResponseEntity<List<PersonResponse>> persons(HttpServletRequest request) {

        List<PersonResponse> persons = personService.getActivePersons().stream()
            .map(p -> createPersonResponse(p, request))
            .collect(Collectors.toList());

        return new ResponseEntity<>(persons, HttpStatus.OK);
    }

    @ApiOperation(
        value = "Get one active person by id", notes = "Get one active person by id"
    )
    @GetMapping(PERSON_URL)
    public ResponseEntity<PersonResponse> getPerson(@PathVariable Integer id, HttpServletRequest request) {

        Optional<Person> person = personService.getPersonByID(id);
        if (person.isPresent()) {
            return new ResponseEntity<>(createPersonResponse(person.get(), request), HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    private PersonResponse createPersonResponse(Person person, HttpServletRequest request) {
        final PersonResponse personResponse = PersonResponseMapper.mapToResponse(person);
        final String baseUri = new URIBuilder()
            .setScheme(request.getScheme())
            .setPort(request.getServerPort())
            .setHost(request.getServerName())
            .setPath(ROOT_URL)
            .toString();
        personResponse.add(new Link(baseUri + "/" + person.getId()).withSelfRel());
        return personResponse;
    }
}

@@ -277,52 +277,8 @@
 nk;%0A
-import org.springframework.http.HttpStatus;%0A
 impo
@@ -740,31 +740,39 @@
 .util.List;%0A
+%0A
 import 
+static 
 java.util.Op
@@ -773,52 +773,147 @@
 til.
-Optional;%0Aimport java.util.stream.Collectors
+stream.Collectors.toList;%0Aimport static org.springframework.http.HttpStatus.NOT_FOUND;%0Aimport static org.springframework.http.HttpStatus.OK
 ;%0A%0A@
@@ -1781,27 +1781,16 @@
 collect(
-Collectors.
 toList()
@@ -1838,27 +1838,16 @@
 ersons, 
-HttpStatus.
 OK);%0A   
@@ -2109,33 +2109,14 @@
     
-Optional%3CPerson%3E person =
+return
  per
@@ -2143,17 +2143,16 @@
 ByID(id)
-;
 %0A       
@@ -2156,52 +2156,25 @@
     
-if (person.isPresent()) %7B%0A            return
+    .map(value -%3E
  new
@@ -2216,20 +2216,13 @@
 nse(
-person.get()
+value
 , re
@@ -2233,23 +2233,12 @@
 t), 
-HttpStatus.
 OK)
-;
+)
 %0A   
@@ -2246,89 +2246,62 @@
     
-%7D else %7B%0A            return new ResponseEntity%3C%3E(HttpStatus.NOT_FOUND);%0A        %7D
+    .orElseGet(() -%3E new ResponseEntity%3C%3E(NOT_FOUND));
 %0A   

Improve readability on person controller class