package fr.animenfance.partenaire.controller;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.DELETE;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

import java.util.List;
import java.util.concurrent.Callable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import fr.animenfance.bean.Partenaire;
import fr.animenfance.exception.PartenaireNotFoundException;
import fr.animenfance.partenaire.service.PartenaireService;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
public class PartenaireController {

  @Autowired private PartenaireService service;

  @SuppressWarnings("SpringJavaAutowiringInspection")
  @Autowired private PartenaireIndexerService indexer;

  @RequestMapping(value = "/partenaires/{id}",
    method = GET,
    produces = APPLICATION_JSON_VALUE)
  public Callable<ResponseEntity<Partenaire>> getById(final @PathVariable Integer id) {
    return () -> {
      try {
        return ResponseEntity.ok(service.getById(id));
      } catch (PartenaireNotFoundException e) {
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
      }
    };
  }

  @RequestMapping(value = "/partenaires", method = GET, produces = APPLICATION_JSON_VALUE)
  public Callable<ResponseEntity<List<Partenaire>>> list() {
    return () -> ResponseEntity.ok(service.list());
  }

  @RequestMapping(value = "/partenaires", method = POST, produces = APPLICATION_JSON_VALUE)
  public Callable<ResponseEntity<Integer>> create(@RequestBody final Partenaire partenaire) {
    return () -> ResponseEntity.ok(service.create(partenaire));
  }

  @RequestMapping(value = "/partenaires/{id}", method = DELETE, produces = APPLICATION_JSON_VALUE)
  public Callable<ResponseEntity<Integer>> deleteById(final @PathVariable Integer id) {
    return () -> ResponseEntity.ok(service.deleteById(id));
  }

  @RequestMapping(value = "/partenaires/search",
    method = GET,
    produces = APPLICATION_JSON_VALUE)
  public Callable<ResponseEntity<List<Partenaire>>> searchPartenaire(
    @RequestParam String search, @RequestParam(defaultValue = "20") Integer hitCount) {
    return () -> indexer.searchPartenaire(search, hitCount);
  }

  @RequestMapping(value = "/partenaires/rebuild-index",
    method = GET)
  public Callable<ResponseEntity<String>> rebuildIndex() {
    return () -> {
      indexer.rebuildIndex();
      return ResponseEntity.ok("Done.");
    };
  }
}

@@ -117,155 +117,132 @@
 UE;%0A
+%0A
 import 
-static org.springframework.web.bind.annotation.RequestMethod.DELETE;%0Aimport static org.springframework.web.bind.annotation.RequestMethod.GET
+java.util.List;%0Aimport java.util.concurrent.Callable;%0A%0Aimport org.springframework.beans.factory.annotation.Autowired
 ;%0Aim
@@ -238,39 +238,32 @@
 towired;%0Aimport 
-static 
 org.springframew
@@ -270,264 +270,192 @@
 ork.
-web.bind.annotation.RequestMethod.POST;%0A%0Aimport java.util.List;%0Aimport java.util.concurrent.Callable;%0A%0Aimport org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.http.HttpStatus;%0Aimport org.springframework.http.ResponseEntity
+http.HttpStatus;%0Aimport org.springframework.http.ResponseEntity;%0Aimport org.springframework.web.bind.annotation.DeleteMapping;%0Aimport org.springframework.web.bind.annotation.GetMapping
 ;%0Aim
@@ -560,27 +560,27 @@
 otation.
-RequestBody
+PostMapping
 ;%0Aimport
@@ -627,23 +627,20 @@
 .Request
-Mapping
+Body
 ;%0Aimport
@@ -1171,30 +1171,26 @@
 ndexer;%0A%0A  @
-Reques
+Ge
 tMapping(val
@@ -1215,34 +1215,16 @@
 /%7Bid%7D%22,%0A
-    method = GET,%0A
     prod
@@ -1550,38 +1550,34 @@
 %0A    %7D;%0A  %7D%0A%0A  @
-Reques
+Ge
 tMapping(value =
@@ -1592,30 +1592,16 @@
 naires%22,
- method = GET,
  produce
@@ -1741,37 +1741,34 @@
 ist());%0A  %7D%0A%0A  @
-Reque
+Po
 stMapping(value 
@@ -1788,23 +1788,8 @@
 es%22,
- method = POST,
  pro
@@ -1978,39 +1978,38 @@
 aire));%0A  %7D%0A%0A  @
-Request
+Delete
 Mapping(value = 
@@ -2032,25 +2032,8 @@
 d%7D%22,
- method = DELETE,
  pro
@@ -2212,38 +2212,34 @@
 d(id));%0A  %7D%0A%0A  @
-Reques
+Ge
 tMapping(value =
@@ -2266,26 +2266,8 @@
 h%22,%0A
-    method = GET,%0A
     
@@ -2528,22 +2528,18 @@
   %7D%0A%0A  @
-Reques
+Ge
 tMapping
@@ -2579,26 +2579,8 @@
 dex%22
-,%0A    method = GET
 )%0A  

Switch to the new @Get/Delete/Post Mappings.