package com.blstream.myhoard.controller;

import com.blstream.myhoard.biz.exception.ErrorCode;
import com.blstream.myhoard.biz.exception.MyHoardException;
import com.blstream.myhoard.biz.model.ItemDTO;
import com.blstream.myhoard.biz.service.ResourceService;
import java.util.List;
import javax.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
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.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@Controller
@RequestMapping("/items")
public class ItemController {

    private ResourceService<ItemDTO> itemService;

    public void setItemService(ResourceService<ItemDTO> itemService) {
        this.itemService = itemService;
    }

    @RequestMapping(method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public List<ItemDTO> getItems() {
        try {
            return itemService.getList();
        } catch (Exception ex) {
            throw new MyHoardException(320, "Nieznany błąd: " + ex.toString() + " > " + ex.getCause().toString());
        }
    }

    @RequestMapping(method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    @ResponseBody
    public ItemDTO createItem(@Valid @RequestBody ItemDTO obj, BindingResult result) {
        if (result.hasErrors())
            throw new MyHoardException(320, result.getFieldError().getDefaultMessage());
        try {
            itemService.create(obj);
            return obj;
        } catch (Exception ex) {
            throw new MyHoardException(320, "Nieznany błąd: " + ex.toString() + " > " + ex.getCause().toString());
        }
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public ItemDTO getItem(@PathVariable String id) {
        try {
            return itemService.get(Integer.parseInt(id));
        } catch (NumberFormatException ex) {
            throw new MyHoardException(320, "Niepoprawne id: " + id);
        } catch (RuntimeException ex) {
            throw new MyHoardException(320, "Nieznany błąd: " + ex.toString() + " > " + ex.getCause().toString());
        }
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public ItemDTO updateItem(@PathVariable String id, @Valid @RequestBody ItemDTO obj, BindingResult result) {
        if (obj.getName() != null && result.hasFieldErrors("name") || obj.getCollection() != null && result.hasFieldErrors("collection"))
            throw new MyHoardException(320, result.getFieldError(obj.getName() != null ? "name" : "collection").getDefaultMessage());
        try {
            obj.setId(id);
            itemService.update(obj);
            return obj;
        } catch (Exception ex) {
            throw new MyHoardException(320, "Nieznany błąd: " + ex.toString() + " > " + ex.getCause().toString());
        }
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void removeItem(@PathVariable String id) {
        try {
            itemService.remove(Integer.parseInt(id));
        } catch (NumberFormatException ex) {
            throw new MyHoardException(320, "Niepoprawne id: " + id);
        } catch (RuntimeException ex) {
            throw new MyHoardException(320, "Nieznany błąd: " + ex.toString() + " > " + ex.getCause().toString());
        }
    }

    @ExceptionHandler(MyHoardException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public @ResponseBody ErrorCode errorHandler(MyHoardException e) {
        return new ErrorCode(e.getErrorCode(), e.getErrorMsg());
    }
}

@@ -269,20 +269,68 @@
 va.util.
-List
+HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0Aimport
@@ -812,16 +812,77 @@
 Method;%0A
+import org.springframework.web.bind.annotation.RequestParam;%0A
 import o
@@ -1571,32 +1571,32 @@
 ().toString());%0A
-
         %7D%0A    %7D%0A
@@ -1588,32 +1588,828 @@
        %7D%0A    %7D%0A%0A
+    @RequestMapping(method = RequestMethod.GET, params = %7B%22name%22, %22collection%22%7D)%0A    @ResponseStatus(HttpStatus.OK)%0A    @ResponseBody%0A    public List%3CItemDTO%3E findItems(@RequestParam(value = %22name%22) String name,%0A            @RequestParam(value = %22collection%22) String collection) %7B%0A        if (name.length() %3C 2 %7C%7C name.length() %3E 20)%0A            throw new MyHoardException(400, %22Zbyt kr%C3%B3tka/d%C5%82uga nazwa do wyszukiwania%22);%0A        Map%3CString, Object%3E params = new HashMap%3C%3E();%0A        params.put(%22name%22, name);%0A        try %7B%0A            params.put(%22collection%22, Integer.parseInt(collection));%0A        %7D catch (NumberFormatException ex) %7B%0A            throw new MyHoardException(400, %22Niepoprawny identyfikaotr kolekcji: %22 + collection);%0A        %7D%0A        return itemService.getList(params);%0A    %7D%0A%0A
     @RequestMapp

Update ItemController.java