import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
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.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.doit.dagama.domain.Shop;
import com.doit.dagama.svr.ShopService;
@Controller
@RequestMapping(value = "/shop")
public class ShopController {
@Autowired
private ShopService shopService;
@RequestMapping(value="/create", method=RequestMethod.GET)
public ModelAndView newShopPage() {
ModelAndView mav = new ModelAndView("shop-new", "shop", new Shop());
return mav;
}
@RequestMapping(value="/create", method=RequestMethod.POST)
public ModelAndView createNewShop(@ModelAttribute Shop shop,
final RedirectAttributes redirectAttribute){
ModelAndView mav = new ModelAndView();
String message = "New Shop " + shop.getName() + " was successfully created. ";
shopService.create(shop);
mav.setViewName("redirect:/index.html");
redirectAttribute.addFlashAttribute("message", message);
return mav;
}
@RequestMapping(value="/list", method=RequestMethod.GET)
@ResponseBody
public Object shopListPage(){
// ModelAndView mav = new ModelAndView("shop-list");
List<Shop> shopList = shopService.findAll();
// mav.addObject("shopList", shopList);
// return mav;
return shopList;
}
@RequestMapping(value="/edit/{id}",method=RequestMethod.GET)
@ResponseBody
public Object editShopPage(@PathVariable Integer id){
// ModelAndView mav = new ModelAndView("shop-edit");
Shop shop = shopService.findById(id);
// mav.addObject("shop", shop);
// return mav;
return shop;
}
@RequestMapping(value="/edit/{id}", method=RequestMethod.POST)
public ModelAndView editShop(@ModelAttribute Shop shop, @PathVariable Integer id, final RedirectAttributes redirectAttribute){
ModelAndView mav = new ModelAndView("redirect:/index.html");
String message = "Shop was successfully updated.";
shopService.update(shop);
redirectAttribute.addFlashAttribute("message", message);
return mav;
}
@RequestMapping(value="/delete/{id}", method=RequestMethod.GET)
public ModelAndView deleteShop(@PathVariable Integer id,
final RedirectAttributes redirectAttribute){
ModelAndView mav = new ModelAndView("redirect:/index.html");
Shop shop = shopService.delete(id);
String message = "The shop " + shop.getName() + " was successfully delted.";
redirectAttribute.addFlashAttribute("message", message);
return mav;
}
}