Implement add Items to ShoppingCart
This commit is contained in:
parent
4338f188f0
commit
7d83d723c1
|
@ -0,0 +1,6 @@
|
|||
INSERT INTO article_offers ("manufacturer", "article_number", "vat_percent")
|
||||
VALUES ("McDonalds", "1", 7);
|
||||
|
||||
INSERT INTO articles ("related_id", "shop_price_per_unit_net_cent", "warehouse_units_per_slot", "should_reorder", "reorder_max_price", "title", "description", "image_id")
|
||||
VALUES (1, 19.99, 10, 1, 15, "Huge Hamburger", "This huge Hamburger is awesome!", NULL);
|
||||
|
|
@ -3,6 +3,7 @@ package org.hso.ecommerce.app;
|
|||
import org.hso.ecommerce.components.ErrorDemoInterceptor;
|
||||
import org.hso.ecommerce.components.InfoDemoInterceptor;
|
||||
import org.hso.ecommerce.components.LoginIntercepter;
|
||||
import org.hso.ecommerce.components.ShoppingCartInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
|
@ -23,5 +24,7 @@ public class Config implements WebMvcConfigurer {
|
|||
registry.addInterceptor(buildLoginIntercepter());
|
||||
registry.addInterceptor(new ErrorDemoInterceptor());
|
||||
registry.addInterceptor(new InfoDemoInterceptor());
|
||||
registry.addInterceptor(new ShoppingCartInterceptor());
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
package org.hso.ecommerce.components;
|
||||
|
||||
import org.hso.ecommerce.entities.shop.ShoppingCart;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
public class ShoppingCartInterceptor implements HandlerInterceptor {
|
||||
|
||||
@Override
|
||||
public boolean preHandle(
|
||||
HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
|
||||
HttpSession session = request.getSession();
|
||||
Object shoppingCart = session.getAttribute("shoppingCart");
|
||||
|
||||
if (shoppingCart == null) {
|
||||
shoppingCart = new ShoppingCart();
|
||||
}
|
||||
|
||||
request.setAttribute("shoppingCart", shoppingCart);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postHandle(
|
||||
HttpServletRequest request, HttpServletResponse response, Object handler,
|
||||
ModelAndView modelAndView) throws Exception {
|
||||
|
||||
HttpSession session = request.getSession();
|
||||
Object shoppingCart = request.getAttribute("shoppingCart");
|
||||
session.setAttribute("shoppingCart", shoppingCart);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
|
||||
Object handler, Exception exception) throws Exception {
|
||||
}
|
||||
}
|
|
@ -1,7 +1,9 @@
|
|||
package org.hso.ecommerce.controller.shop;
|
||||
|
||||
import org.hso.ecommerce.entities.shop.Article;
|
||||
import org.hso.ecommerce.entities.user.User;
|
||||
import org.hso.ecommerce.entities.shop.ShoppingCart;
|
||||
import org.hso.ecommerce.repos.shop.ArticleRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
@ -9,11 +11,15 @@ import org.springframework.web.bind.annotation.*;
|
|||
import javax.servlet.http.HttpSession;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/shop/articles")
|
||||
public class ShopArticleController {
|
||||
|
||||
@Autowired
|
||||
private final ArticleRepository articleRepository = null;
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public String shopArticlesById(Model model, @PathVariable("id") Integer Id) {
|
||||
|
||||
|
@ -54,22 +60,23 @@ public class ShopArticleController {
|
|||
|
||||
@PostMapping("/{id}")
|
||||
public String shopArticlesByIdBuy(HttpSession session,
|
||||
@RequestAttribute(value = "user", required = false) User customer,
|
||||
// @RequestAttribute(value = "shoppingCart", required = true) ShoppingCart shoppingCart,
|
||||
@PathVariable("id") Integer id,
|
||||
@RequestAttribute(value = "shoppingCart") ShoppingCart shoppingCart,
|
||||
@PathVariable("id") Long id,
|
||||
@RequestParam("quantity") Integer quantity,
|
||||
@RequestParam("fastcheckout") Boolean fastcheckout
|
||||
) {
|
||||
if (customer != null) {
|
||||
//TODO: Add Article to Shopping Cart
|
||||
|
||||
if (!fastcheckout) {
|
||||
return "shop/articles/post_add";
|
||||
} else {
|
||||
return "shop/checkout";
|
||||
}
|
||||
|
||||
Optional<Article> article = articleRepository.findById(id);
|
||||
if (!article.isPresent()) {
|
||||
throw new RuntimeException("Article not found!");
|
||||
}
|
||||
|
||||
shoppingCart.addArticle(article.get(), quantity);
|
||||
|
||||
if (!fastcheckout) {
|
||||
return "shop/articles/post_add";
|
||||
} else {
|
||||
session.setAttribute("afterLogin", "/shop/articles/" + id);
|
||||
return "redirect:/login";
|
||||
return "shop/checkout";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,30 @@
|
|||
package org.hso.ecommerce.controller.shop;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
@Controller
|
||||
//@RequestMapping("...")
|
||||
@RequestMapping("/shop/")
|
||||
public class ShopCheckoutController {
|
||||
|
||||
@GetMapping("/checkout")
|
||||
public String shopCheckout(HttpSession session, HttpServletRequest request) {
|
||||
session.setAttribute("afterLogin", request.getRequestURI());
|
||||
return "shop/checkout";
|
||||
}
|
||||
|
||||
@PostMapping("/checkoutFinish")
|
||||
public String shopCheckoutFinish() {
|
||||
return "shop/checkoutFinish";
|
||||
}
|
||||
|
||||
@GetMapping("/checkoutFinish")
|
||||
public String shopCheckoutFinishGET() {
|
||||
return "shop/checkoutFinish";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,64 @@
|
|||
package org.hso.ecommerce.entities.shop;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
// Not a db entity. Just for session storage
|
||||
public class ShoppingCart {
|
||||
|
||||
private int revision;
|
||||
private ArrayList<ShoppingCartItem> items;
|
||||
|
||||
public ShoppingCart() {
|
||||
revision = (int) Math.round(Math.random() * 0xFFFF);
|
||||
items = new ArrayList<>();
|
||||
}
|
||||
|
||||
public int getItemCount() {
|
||||
int count = 0;
|
||||
|
||||
for (ShoppingCartItem i : items) {
|
||||
count += i.getAmount();
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
public int getRevision() {
|
||||
return revision;
|
||||
}
|
||||
|
||||
public void addArticle(Article article, int quantity) {
|
||||
this.revision++;
|
||||
|
||||
for (ShoppingCartItem i : items) {
|
||||
if (i.getArticleId() == article.id) {
|
||||
i.addAmount(quantity);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
items.add(new ShoppingCartItem(quantity, article));
|
||||
}
|
||||
|
||||
private static class ShoppingCartItem {
|
||||
private int amount;
|
||||
private final long articleId;
|
||||
|
||||
public ShoppingCartItem(int amount, Article article) {
|
||||
this.amount = amount;
|
||||
this.articleId = article.id;
|
||||
}
|
||||
|
||||
public int getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public void addAmount(int amount) {
|
||||
this.amount += amount;
|
||||
}
|
||||
|
||||
public long getArticleId() {
|
||||
return articleId;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package org.hso.ecommerce.repos.shop;
|
||||
|
||||
import org.hso.ecommerce.entities.shop.Article;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface ArticleRepository extends JpaRepository<Article, Long> {
|
||||
|
||||
}
|
||||
|
|
@ -26,7 +26,7 @@
|
|||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<a class="button" th:href="@{/shop/checkout}">Warenkorb</a>
|
||||
<a class="button" th:href="@{/shop/checkout}">Warenkorb (<span th:text="${shoppingCart.itemCount}"></span>)</a>
|
||||
</div>
|
||||
<div th:if="${error}" class="error" id="error-msg">
|
||||
<div class="content-width bar-flex">
|
||||
|
|
|
@ -34,10 +34,9 @@
|
|||
<div class="detailgrid m">
|
||||
<h2 th:text="${article.shopPricePerUnitNetCent}"></h2>
|
||||
<div>
|
||||
|
||||
<label class="nolinebreak">Menge:</label>
|
||||
<select size="1">
|
||||
<option th:each="quantity : ${#numbers.sequence(1,100)}"
|
||||
<select name="quantity" size="1">
|
||||
<option th:each="quantity : ${#numbers.sequence(1,10)}"
|
||||
th:value="${quantity}"
|
||||
th:text="${quantity}"></option>
|
||||
</select>
|
||||
|
|
Reference in New Issue