package com.bd.controleur;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import org.apache.xpath.operations.String;
import org.hibernate.mapping.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
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.servlet.ModelAndView;
import com.bd.entity.Article;
import com.bd.service.ArticleService;
@Controller
@RequestMapping("/Article/GererArticle")
public class ArticleControleur {
	@Autowired
	private ArticleService articleService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView listAricle(){
		
	
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("articles",  articleService.getAllArticles());
		
		//new ModelAndView("nom de la page jsp", model qui contient l'ensemble des beans que tu vas passer a cette page)
		return new ModelAndView("GererArticle", model);
	
}
	//formulaire vide d'ajout d'un event
	@RequestMapping("/articleAdd")
		public ModelAndView addArticle(@ModelAttribute(value="article") Article article, 
				                        HttpServletRequest request, 
				                        ModelMap modelMap) {
		
			Map<String, Object> model = new HashMap<String, Object>();
		
			model.put("articles", article);
			return new ModelAndView("articleAdd", model);
		}
		
		//enregistrer le formulaire
		@RequestMapping(value = "/save", method = RequestMethod.POST)
		public ModelAndView saveArticle(@ModelAttribute("article") Article  article,BindingResult result) {
		
			
			if (result.hasErrors()) {
				Map<String, Object> model = new HashMap<String, Object>();
				String a="existe deja";
				model.put("existe",a);
				model.put("article", article);
				return new ModelAndView("articleAdd",model);
			    } else {
			
			
			articleService.saveArticle(article);}
		
}
}
			
		
 
	
Partager