package sales.goods.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import sales.goods.domain.Good;
import sales.goods.service.GoodsService;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;


@RestController
@RequestMapping("/goods")
public class GoodsController {

    protected static Logger logger = Logger.getLogger(GoodsController.class.getName());

    @Autowired
    private GoodsService service;

    @RequestMapping(value = "",
            method = RequestMethod.GET,
            produces = "application/json; charset=UTF-8")
    @ResponseStatus(HttpStatus.OK)
    public
    @ResponseBody
    Good getGoods(@RequestParam(value = "id") int id) {
        logger.info("Good: get by id using hibernate");
        return service.get(id);
    }

    @RequestMapping(value = "/",
            method = RequestMethod.GET,
            produces = "application/json; charset=UTF-8")
    @ResponseStatus(HttpStatus.OK)
    public
    @ResponseBody
    List<Good> getAllGoods() {
        logger.info("Good: get all by id using hibernate");
        return service.getAll();
    }

    @RequestMapping(value = "/",
            method = RequestMethod.POST,
            produces = "application/json; charset=UTF-8",
            consumes = "application/json")
    @ResponseStatus(HttpStatus.CREATED)
    public
    @ResponseBody
    Good putGoods(@RequestBody Good goods) throws IOException {
        logger.info("Good: save or update using hibernate");
        return service.save(goods);
    }

    @RequestMapping(value = "",
            method = RequestMethod.DELETE,
            produces = "application/json; charset=UTF-8")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public
    @ResponseBody
    void removeGoods(@RequestParam(value = "id") int id) throws IOException {
        logger.info("Good: save or update using hibernate");
        service.delete(id);
    }

    @RequestMapping(value = "/page",
            method = RequestMethod.POST,
            produces = "application/json; charset=UTF-8",
            consumes = "application/json")
    @ResponseStatus(HttpStatus.OK)
    public
    @ResponseBody
    List<Good> goodsPagination(
            @RequestParam(required = false, value = "number", defaultValue = "0") int page,
            @RequestParam(required = false, value = "amount", defaultValue = "5") int amount,
            @RequestBody Map<String, String> sort) throws IOException {
        logger.info("Good: pagination using hibernate");
        return service.page(page, amount, sort);
    }

    @RequestMapping(value = "/search",
            method = RequestMethod.GET,
            produces = "application/json; charset=UTF-8")
    @ResponseStatus(HttpStatus.OK)
    public
    @ResponseBody
    List<Good> goodsSearch(@RequestParam(value = "name") String name) throws IOException {
        logger.info("Good: search by name using hibernate");
        return service.searchByName(name);
    }


    @RequestMapping(value = "/filter",
            method = RequestMethod.GET,
            produces = "application/json; charset=UTF-8")
    public
    @ResponseBody
    List<Good> goodsSearchByNameBetweenPrice(
            @RequestParam(value = "from") int from,
            @RequestParam(value = "to") int to) throws IOException {
        logger.info("Good: search by price using between, using hibernate");
        return service.filterPriceScope(from, to);
    }
}

@@ -753,38 +753,16 @@
   public
-%0A    @ResponseBody%0A   
  Good ge
@@ -802,24 +802,24 @@
 ) int id) %7B%0A
+
         logg
@@ -1077,38 +1077,16 @@
   public
-%0A    @ResponseBody%0A   
  List%3CGo
@@ -1430,38 +1430,16 @@
   public
-%0A    @ResponseBody%0A   
  Good pu
@@ -1781,38 +1781,16 @@
   public
-%0A    @ResponseBody%0A   
  void re
@@ -2176,38 +2176,16 @@
   public
-%0A    @ResponseBody%0A   
  List%3CGo
@@ -2758,38 +2758,16 @@
   public
-%0A    @ResponseBody%0A   
  List%3CGo
@@ -3091,24 +3091,24 @@
 set=UTF-8%22)%0A
+
     public%0A 
@@ -3109,30 +3109,8 @@
 blic
-%0A    @ResponseBody%0A   
  Lis

create generator for damain goods