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.CollectionDTO;
import com.blstream.myhoard.biz.service.CollectionService;
import java.util.List;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
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(value = "/collections")
public class CollectionController {

	@Autowired
	private CollectionService collectionService;

	@RequestMapping(method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)
	public @ResponseBody List<CollectionDTO> getCollections() {
		return collectionService.getList();
	}

	@RequestMapping(method = RequestMethod.POST)
	@ResponseStatus(HttpStatus.CREATED)
	public @ResponseBody CollectionDTO addCollection(@RequestBody CollectionDTO collection) {
		try {
			collectionService.create(collection);
			return collection;
		} catch (Exception ex) {
			throw new MyHoardException(400);
		}
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)
	public @ResponseBody CollectionDTO getCollection(@PathVariable String id) {
		try {
			return collectionService.get(Integer.parseInt(id));
		} catch (Exception ex) {
			throw new MyHoardException(300);
		}
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
	@ResponseStatus(HttpStatus.OK)
	public @ResponseBody CollectionDTO updateCollection(@PathVariable String id, @RequestBody CollectionDTO collection) {
		try {
			collection.setId(id);
			collectionService.update(collection);
			return collection;
		} catch (Exception ex) {
			throw new MyHoardException(111);
		}
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	@ResponseStatus(HttpStatus.NO_CONTENT)
	public void removeCollection(@PathVariable String id) {
		try {
			collectionService.remove(Integer.parseInt(id));
		} catch (Exception ex) {
			throw new MyHoardException(400);
		}
	}

	@ExceptionHandler(MyHoardException.class)
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	public @ResponseBody ErrorCode returnCode(MyHoardException exception) {
		return new ErrorCode(exception.getError_code());
	}
}
@@ -260,16 +260,59 @@
 ervice;%0A
+import com.blstream.myhoard.validation.*;%0A%0A
 import j
@@ -326,16 +326,17 @@
 l.List;%0A
+%0A
 import o
@@ -526,24 +526,77 @@
 Controller;%0A
+import org.springframework.validation.BindingResult;%0A
 import org.s
@@ -1166,17 +1166,97 @@
 ervice;%0A
+%09%0A        @Autowired%0A        CollectionDTOValidator collectionDTOValidator;%0A    
 %0A
-
 %09@Reques
@@ -1589,35 +1589,216 @@
 onDTO collection
-) %7B
+, BindingResult result) %7B%0A        collectionDTOValidator.validate(collection, result);%0A        if (result.hasErrors()) %7B%0A            throw new MyHoardException(400);    %0A        %7D%09    
 %0A%09%09try %7B%0A%09%09%09coll
@@ -2438,19 +2438,189 @@
 llection
-) %7B
+, BindingResult result) %7B%0A%09    collectionDTOValidator.validate(collection, result);%0A        if (result.hasErrors()) %7B%0A            throw new MyHoardException(400);%09%0A        %7D
 %0A%09%09try %7B
@@ -3282,9 +3282,10 @@
 ());%0A%09%7D%0A
-
 %7D
+%0A

Update CollectionController.java