Created
November 29, 2019 19:46
-
-
Save vinnom/00bd9b905919bd0519a9adee3ce6930d to your computer and use it in GitHub Desktop.
Carregamento do Carrinho
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
package br.com.alura.springmvc.models; | |
import java.io.Serializable; | |
import java.math.BigDecimal; | |
import java.util.*; | |
import org.springframework.context.annotation.Scope; | |
import org.springframework.stereotype.Component; | |
import org.springframework.web.context.WebApplicationContext; | |
@Component | |
@Scope(value = WebApplicationContext.SCOPE_SESSION) | |
public class CarrinhoCompras implements Serializable { | |
private static final long serialVersionUID = 1L; | |
public Map<CarrinhoItem, Integer> carrinho = new LinkedHashMap<CarrinhoItem, Integer>(); | |
public void add(CarrinhoItem item) { | |
carrinho.put(item, this.getQuantidade(item) + 1); | |
} | |
public Integer getQuantidade(CarrinhoItem item) { | |
if (!carrinho.containsKey(item)) { | |
carrinho.put(item, 0); | |
} | |
return carrinho.get(item); | |
} | |
public int getTotal() { | |
return carrinho.values().stream().reduce(0, (proximo, accumulator) -> (proximo + accumulator)); | |
} | |
public Collection<CarrinhoItem> getItens(){ | |
carrinho.keySet().removeIf(Objects::isNull); | |
return carrinho.keySet(); | |
} | |
public BigDecimal getTotalPor(CarrinhoItem item) { | |
return item.getTotalPor(getQuantidade(item)); | |
} | |
public BigDecimal getTotalCompra() { | |
BigDecimal total = BigDecimal.ZERO; | |
for (CarrinhoItem item : carrinho.keySet()) { | |
total = total.add(getTotalPor(item)); | |
} | |
return total; | |
} | |
public void remove(Integer produtoId, TipoPreco tipo) { | |
Produto produto = new Produto(); | |
produto.setId(produtoId); | |
carrinho.remove(new CarrinhoItem(produto, tipo)); | |
} | |
} |
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
package br.com.alura.springmvc.controllers; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.annotation.Scope; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.context.WebApplicationContext; | |
import org.springframework.web.servlet.ModelAndView; | |
import br.com.alura.springmvc.daos.ProdutoDAO; | |
import br.com.alura.springmvc.models.*; | |
@Controller | |
@RequestMapping("/carrinho") | |
@Scope(value = WebApplicationContext.SCOPE_REQUEST) | |
public class CarrinhoComprasController { | |
@Autowired | |
private CarrinhoCompras carrinho; | |
@Autowired | |
private ProdutoDAO produtoDao; | |
@RequestMapping("/add") | |
public ModelAndView add(Integer produtoId, TipoPreco tipo) { | |
CarrinhoItem item = buscaItem(produtoId, tipo); | |
carrinho.add(item); | |
System.out.println("[[[[[[[" + carrinho.getQuantidade(item) + "]]]]]]]]"); | |
System.out.println("[[[[[[[" + carrinho.getItens() + "]]]]]]]]"); | |
System.out.println("[[[[[[[" + carrinho.getTotalPor(item) + "]]]]]]]]"); | |
ModelAndView modelAndView = new ModelAndView("redirect:/carrinho"); | |
return modelAndView; | |
} | |
private CarrinhoItem buscaItem(Integer produtoId, TipoPreco tipo) { | |
Produto produto = produtoDao.acha(produtoId); | |
CarrinhoItem item = new CarrinhoItem(produto, tipo); | |
return item; | |
} | |
@RequestMapping(method = RequestMethod.GET) | |
public ModelAndView itens() { | |
return new ModelAndView("carrinho/itens"); | |
} | |
@RequestMapping("/remove") | |
public ModelAndView remove(Integer produtoId, TipoPreco tipo) { | |
carrinho.remove(produtoId, tipo); | |
return new ModelAndView("redirect:/carrinho"); | |
} | |
} |
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
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> | |
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> | |
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> | |
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%> | |
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="sform"%> | |
<%@ taglib tagdir="/WEB-INF/tags" prefix="ptags"%> | |
<!DOCTYPE html> | |
<ptags:paginaModelo titulo="Livros de Java, SOA, Android, iPhone, Ruby on Rails e muito mais"> | |
<jsp:body> | |
<article id="livro-css-eficiente"> | |
<header id="product-highlight" class="clearfix"> | |
<div id="product-overview" class="container"> | |
<img width="280px" height="395px" src="http://cdn.shopify.com/s/files/1/0155/7645/products/css-eficiente-featured_large.png?v=1435245145" | |
class="product-featured-image" /> | |
<h1 class="product-title">${produto.titulo}</h1> | |
<p class="product-author"> | |
<span class="product-author-link"> </span> | |
</p> | |
<p class="book-description">${produto.descricao}</p> | |
</div> | |
</header> | |
<section class="buy-options clearfix"> | |
<sform:form servletRelativeAction="/carrinho/add" method="post" class="container"> | |
<input type="hidden" value="${produto.id}" name="produtoId" > | |
<ul id="variants" class="clearfix"> | |
<c:forEach items="${produto.precos}" var="preco"> | |
<li class="buy-option"> | |
<input type="radio" name="tipo" class="variant-radio" id="tipo" value="${preco.tipo}" checked="checked" /> | |
<label class="variant-label" >${preco.tipo}</label> | |
<small class="compare-at-price">R$ 39,90</small> | |
<p class="variant-price">${preco.valor}</p> | |
</li> | |
</c:forEach> | |
</ul> | |
<button type="submit" class="submit-image icon-basket-alt" title="Compre Agora ${produto.titulo}"></button> | |
</sform:form> | |
</section> | |
<div class="container"> | |
<section class="summary"> | |
<ul> | |
<li> | |
<h3>E muito mais... <a href='/pages/sumario-java8'>veja o sumário</a>.</h3> | |
</li> | |
</ul> | |
</section> | |
<section class="data product-detail"> | |
<h2 class="section-title">Dados do livro:</h2> | |
<p>Número de páginas: <span>${produto.paginas}</span></p> | |
<p></p> | |
<p>Data de publicação: <fmt:formatDate pattern="dd/MM/yyyy" value="${produto.dataLancamento.time}"/> </p> | |
<p>Encontrou um erro? <a href='/submissao-errata' target='_blank'>Submeta uma errata</a></p> | |
</section> | |
</div> | |
</article> | |
</jsp:body> | |
</ptags:paginaModelo> |
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
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> | |
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> | |
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%> | |
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="sform"%> | |
<%@ taglib tagdir="/WEB-INF/tags" prefix="ptags"%> | |
<!DOCTYPE html> | |
<ptags:paginaModelo titulo="Carrinho de Compras"> | |
<jsp:attribute name="extraScripts"> | |
<script> | |
console.log("Finalização de compra de ${carrinhoCompras.getQuantidade(item)} itens"); | |
</script> | |
</jsp:attribute> | |
<jsp:body> | |
<section class="container middle"> | |
<h2 id="cart-title">Seu carrinho de compras</h2> | |
<table id="cart-table"> | |
<colgroup> | |
<col class="item-col" /> | |
<col class="item-price-col" /> | |
<col class="item-quantity-col" /> | |
<col class="line-price-col" /> | |
<col class="delete-col" /> | |
</colgroup> | |
<thead> | |
<tr> | |
<th class="cart-img-col"></th> | |
<th width="65%">Item</th> | |
<th width="10%">Preço</th> | |
<th width="10%">Quantidade</th> | |
<th width="10%">Total</th> | |
<th width="5%"></th> | |
</tr> | |
</thead> | |
<tbody> | |
<c:forEach items="${carrinhoCompras.itens}" var="item" > | |
<tr> | |
<td class="cart-img-col"> | |
<img src="http://cdn.shopify.com/s/files/1/0155/7645/products/css-eficiente-featured_large.png?v=1435245145" | |
width="71px" height="100px" /> | |
</td> | |
<td class="item-title">${itens.produto.titulo}</td> | |
<td class="numeric-cell">${itens.preco}</td> | |
<td class="quantity-input-cell"> | |
<input type="number" min="0" id="quantidade" name="quantidade" | |
value="${carrinhoCompras.getQuantidade(item)}" /> | |
</td> | |
<td class="numeric-cell">${carrinhoCompras.getTotalPor(item)}</td> | |
<td class="remove-item"> | |
<sform:form servletRelativeAction="${spring:mvcUrl('CCC#remove').arg(0, item.produto.id).arg(1, item.tipo).build()}" method="POST"> | |
<input type="image" src="/resources/imagens/excluir.png" alt="Excluir" title="Excluir" /> | |
</sform:form> | |
</td> | |
</tr> | |
</c:forEach> | |
</tbody> | |
<tfoot> | |
<tr> | |
<td colspan="3"> | |
<sform:form servletRelativeAction="${spring:mvcUrl('PC#finaliza').build()}" method="post"> | |
<input type="submit" class="checkout" name="checkout" value="Finalizar compra" /> | |
</sform:form> | |
</td> | |
<td class="numeric-cell">${carrinhoCompras.totalCompra}</td> | |
<td></td> | |
</tr> | |
</tfoot> | |
</table> | |
<h2>Você já conhece os outros livros da Casa do Código?</h2> | |
<ul id="collection" class="related-books"> | |
<li class="col-left"> | |
<a href="/products/livro-plsql" class="block clearfix book-suggest" | |
data-book="PL/SQL: Domine a linguagem do banco de dados Oracle"> | |
<img width="113px" height="160px" | |
src="http:////cdn.shopify.com/s/files/1/0155/7645/products/plsql-featured_compact.png?v=1434740236" | |
alt="PL/SQL: Domine a linguagem do banco de dados Oracle" /> | |
</a> | |
</li> | |
</ul> | |
<h2> | |
<a href="http://www.casadocodigo.com.br">Veja todos os livros que publicamos!</a> | |
</h2> | |
</section> | |
</jsp:body> | |
</ptags:paginaModelo> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment