diff --git a/prototype/scripts/addarticles.sql b/prototype/scripts/addarticles.sql new file mode 100644 index 0000000..8f976a7 --- /dev/null +++ b/prototype/scripts/addarticles.sql @@ -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); + diff --git a/prototype/src/main/java/org/hso/ecommerce/app/Config.java b/prototype/src/main/java/org/hso/ecommerce/app/Config.java index 7eda415..961f7ba 100644 --- a/prototype/src/main/java/org/hso/ecommerce/app/Config.java +++ b/prototype/src/main/java/org/hso/ecommerce/app/Config.java @@ -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()); + } } diff --git a/prototype/src/main/java/org/hso/ecommerce/components/ShoppingCartInterceptor.java b/prototype/src/main/java/org/hso/ecommerce/components/ShoppingCartInterceptor.java new file mode 100644 index 0000000..3a67897 --- /dev/null +++ b/prototype/src/main/java/org/hso/ecommerce/components/ShoppingCartInterceptor.java @@ -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 { + } +} diff --git a/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopArticleController.java b/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopArticleController.java index 653286e..bd544b3 100644 --- a/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopArticleController.java +++ b/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopArticleController.java @@ -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 = 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"; } } } diff --git a/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopCheckoutController.java b/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopCheckoutController.java index dd127da..1573b1b 100644 --- a/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopCheckoutController.java +++ b/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopCheckoutController.java @@ -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"; + } } diff --git a/prototype/src/main/java/org/hso/ecommerce/entities/shop/ShoppingCart.java b/prototype/src/main/java/org/hso/ecommerce/entities/shop/ShoppingCart.java new file mode 100644 index 0000000..f7955ec --- /dev/null +++ b/prototype/src/main/java/org/hso/ecommerce/entities/shop/ShoppingCart.java @@ -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 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; + } + } +} diff --git a/prototype/src/main/java/org/hso/ecommerce/repos/shop/ArticleRepository.java b/prototype/src/main/java/org/hso/ecommerce/repos/shop/ArticleRepository.java new file mode 100644 index 0000000..0ac4bd2 --- /dev/null +++ b/prototype/src/main/java/org/hso/ecommerce/repos/shop/ArticleRepository.java @@ -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 { + +} + diff --git a/prototype/src/main/resources/templates/fragments/header.html b/prototype/src/main/resources/templates/fragments/header.html index b0b52bb..7b6ec7c 100644 --- a/prototype/src/main/resources/templates/fragments/header.html +++ b/prototype/src/main/resources/templates/fragments/header.html @@ -26,7 +26,7 @@ - Warenkorb + Warenkorb ()
diff --git a/prototype/src/main/resources/templates/shop/articles/id.html b/prototype/src/main/resources/templates/shop/articles/id.html index 2a7df6d..46b7c93 100644 --- a/prototype/src/main/resources/templates/shop/articles/id.html +++ b/prototype/src/main/resources/templates/shop/articles/id.html @@ -34,10 +34,9 @@

- -