package io.ar.metechium.web;

import io.ar.metechium.domain.Vegetable;
import io.ar.metechium.domain.VegetableRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


@Controller
public class VeggieController {

    @Autowired
    protected VegetableRepository veggieRepository;

    @RequestMapping("/")
    public String index(@RequestParam(value= "sound", required = false, defaultValue= "Bam") String sound, Model model){
        List<Vegetable> vegs = (List<Vegetable>) veggieRepository.findAll();
        model.addAttribute("sound", sound);
        return "index";
    }

    @RequestMapping("/veggie")
    public String listAll(Model model) {
        List<Vegetable> vegs = (List<Vegetable>) veggieRepository.findAll();
        model.addAttribute("vegetables", vegs);
        return "veggieHello";
    }

    @RequestMapping(value = "/veggie/add", method = RequestMethod.GET)
    public String createOrUpdate(@RequestParam(value = "id", required = false) Long id, Model model){
        if(id != null){
         Vegetable veg  = veggieRepository.findOne(id);
            model.addAttribute("vegetable", veg);
        }else {
            model.addAttribute("vegetable", new Vegetable());
        }
        return "veggieForm";
    }

    @RequestMapping(value = "/veggie/add", method = RequestMethod.POST)
    public String createOrUpdateSave(@ModelAttribute Vegetable vegetable, Model model){
        veggieRepository.save(vegetable);
        return "redirect:/veggie";
    }

}

@@ -115,16 +115,73 @@
 sitory;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import o
@@ -642,16 +642,102 @@
 ller %7B%0A%0A
+    public static final Logger logger = LoggerFactory.getLogger(%22VeggieController%22);%0A%0A
     @Aut
@@ -1317,13 +1317,12 @@
 ggie
-Hello
+List
 %22;%0A 
@@ -1356,35 +1356,38 @@
 alue = %22/veggie/
-add
+create
 %22, method = Requ
@@ -1579,24 +1579,65 @@
 indOne(id);%0A
+            logger.info(veg.toString());%0A
             
@@ -1762,16 +1762,17 @@
       %7D%0A
+%0A
         
@@ -1785,16 +1785,22 @@
  %22veggie
+Update
 Form%22;%0A 
@@ -1797,32 +1797,62 @@
 teForm%22;%0A    %7D%0A%0A
+    //TODO update not working%0A
     @RequestMapp
@@ -1876,41 +1876,15 @@
 gie/
-add%22, method = RequestMethod.POST
+create%22
 )%0A  
@@ -1918,16 +1918,71 @@
 ateSave(
+@RequestParam(value = %22id%22, required = false) Long id, 
 @ModelAt
@@ -2014,32 +2014,59 @@
 le, Model model)
+ %7B%0A        if (null != id) 
 %7B%0A        veggie
@@ -2059,16 +2059,20 @@
         
+    
 veggieRe
@@ -2084,22 +2084,678 @@
 ory.
-save(vegetable
+findOne(id).setName(vegetable.getName());%0A            veggieRepository.findOne(id).setPrice(vegetable.getPrice());%0A            veggieRepository.findOne(id).setColor(vegetable.getColor());%0A            veggieRepository.findOne(id).setQuantity(vegetable.getQuantity());%0A            logger.info(%22Veggie update%22);%0A        %7D else %7B%0A            logger.info(%22Veggie Creation%22);%0A            veggieRepository.save(vegetable);%0A        %7D%0A        return %22redirect:/veggie%22;%0A    %7D%0A%0A    @RequestMapping(value = %22/veggie/delete%22, method = RequestMethod.GET)%0A    public String delete(@RequestParam(value = %22id%22, required = true) Long id, Model model) %7B%0A        veggieRepository.delete(id
 );%0A 

update controller with new views name