Created
December 6, 2016 19:08
-
-
Save ricardofiorani/972e1d55e60427a4581874c2cb9c0da3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package br.com.umbernardo.calories.controller; | |
import br.com.umbernardo.calories.dao.DiarioDAO; | |
import br.com.umbernardo.calories.model.Diario; | |
import java.util.List; | |
import javax.persistence.EntityManager; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.servlet.ModelAndView; | |
/** | |
* | |
* @author ricar | |
*/ | |
@Controller | |
public class DiarioController { | |
@Autowired | |
private DiarioDAO diarioDAO; | |
private EntityManager em; | |
@RequestMapping(value="/diario", method=RequestMethod.POST) | |
public ModelAndView listarSucesso() { | |
ModelAndView mv = new ModelAndView("diario/sucesso"); | |
List<Diario> diario = diarioDAO.listarSucesso(); | |
mv.addObject("diario",diario); | |
return mv; | |
} | |
@RequestMapping("removeDiario/{id}") | |
public String remove(int id) { | |
Diario diario = diarioDAO.buscarDiarioPorId(id); | |
dao.remove(diario); | |
return "redirect:diario/list"; | |
} | |
@RequestMapping("/diario/addForm") | |
public String addForm() { | |
return "/diario/addForm"; | |
} | |
@RequestMapping("/diario/list") | |
public ModelAndView listar() { | |
ModelAndView mv = new ModelAndView("diario/list"); | |
List<Diario> diario = diarioDAO.listar(); | |
mv.addObject("diario",diario); | |
return mv; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package br.com.umbernardo.calories.dao; | |
import br.com.umbernardo.calories.model.Diario; | |
import java.util.List; | |
import javax.jms.Session; | |
import javax.persistence.EntityManager; | |
import javax.persistence.PersistenceContext; | |
import javax.transaction.Transaction; | |
import org.springframework.stereotype.Repository; | |
import org.springframework.transaction.annotation.Transactional; | |
/** | |
* | |
* @author ricar | |
*/ | |
@Repository | |
@Transactional | |
public class DiarioDAO { | |
@PersistenceContext | |
private EntityManager em; | |
public void addDiario(Diario diario) { | |
em.persist(diario); | |
} | |
public void removeDiario(Diario diario){ | |
em.remove(diario); | |
} | |
public Diario buscarDiarioPorId(int id){ | |
Diario diario = em.find(diario.class, id); | |
return diario; | |
} | |
public List<Diario> listarSucesso() { | |
return em.createQuery("Select p from Diario p", Diario.class).getResultList(); | |
} | |
public List<Diario> listar() { | |
return em.createQuery("Select p from Diario p", Diario.class).getResultList(); | |
} | |
public void remove(Diario diario) { | |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> | |
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> | |
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> | |
<%-- | |
Document : dashboard | |
Created on : Nov 30, 2016, 3:50:01 PM | |
Author : ricar | |
--%> | |
<jsp:include page ="../_header.jsp"/> | |
<div class="row"> | |
<div class="col-sm-12"> | |
<h1>Contador de Calorias</h1> | |
</div> | |
<div class="col-md-6"> | |
<h3>Alimentos cadastrados em seu diário</h3> | |
<table style="width:100%"> | |
<tr> | |
<th>Nome do Alimento</th> | |
<th>Calorias</th> | |
<th>Quantidade</th> | |
<th>Ações</th> | |
</tr> | |
<c:forEach items="${diario}" var="d"> | |
<tr> | |
<td>${d.nome}</td> | |
<td>${d.calorias}</td> | |
<td>${d.quantidade}</td> | |
<td> <a style="color:#000000!important" href="/Calories/removeDiario?id=${d.id}">Deletar</a> | <a style="color:#000000!important" href="#">Editar</a></td> | |
</tr> | |
</c:forEach> | |
</table> | |
</div> | |
<div class="col-md-6 sidebar-user"> | |
<ul> | |
<li><a href="/Calories/diario/list">Ver Diário</a></li> | |
<li><a href="/Calories/diario/addForm">Cadastrar Diário</a></li> | |
<li><a href="#">Logout</a></li> | |
</ul> | |
</div> | |
</div> | |
<jsp:include page ="../_footer.jsp"/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment