feature/basic_functions #7

Merged
Seil0 merged 30 commits from feature/basic_functions into master 2020-05-10 17:48:31 +02:00
9 changed files with 174 additions and 19 deletions
Showing only changes of commit 31059b4894 - Show all commits

View File

@ -0,0 +1,6 @@
INSERT INTO article_offers ("manufacturer", "article_number", "vat_percent")
VALUES ("McDonalds", "1", 7);
Review

fixed it

fixed it
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);

View File

@ -3,6 +3,7 @@ package org.hso.ecommerce.app;
import org.hso.ecommerce.components.ErrorDemoInterceptor; import org.hso.ecommerce.components.ErrorDemoInterceptor;
import org.hso.ecommerce.components.InfoDemoInterceptor; import org.hso.ecommerce.components.InfoDemoInterceptor;
import org.hso.ecommerce.components.LoginIntercepter; import org.hso.ecommerce.components.LoginIntercepter;
import org.hso.ecommerce.components.ShoppingCartInterceptor;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
@ -23,5 +24,7 @@ public class Config implements WebMvcConfigurer {
registry.addInterceptor(buildLoginIntercepter()); registry.addInterceptor(buildLoginIntercepter());
registry.addInterceptor(new ErrorDemoInterceptor()); registry.addInterceptor(new ErrorDemoInterceptor());
registry.addInterceptor(new InfoDemoInterceptor()); registry.addInterceptor(new InfoDemoInterceptor());
registry.addInterceptor(new ShoppingCartInterceptor());
} }
} }

View File

@ -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 {
Review

Laut Intellij werden die Exceptions nie geworfen.

Laut Intellij werden die Exceptions nie geworfen.
Review

Ist wegen dem Interface, die Methode ist dort so deklariert. Daher würde ich das gerne so lassen, zwecks Konsistenz

Ist wegen dem Interface, die Methode ist dort so deklariert. Daher würde ich das gerne so lassen, zwecks Konsistenz
Review

Ok

Ok
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 {
}
}

View File

@ -1,7 +1,9 @@
package org.hso.ecommerce.controller.shop; package org.hso.ecommerce.controller.shop;
import org.hso.ecommerce.entities.shop.Article; 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.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -9,11 +11,15 @@ import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
@Controller @Controller
@RequestMapping("/shop/articles") @RequestMapping("/shop/articles")
public class ShopArticleController { public class ShopArticleController {
@Autowired
private final ArticleRepository articleRepository = null;
@GetMapping("/{id}") @GetMapping("/{id}")
public String shopArticlesById(Model model, @PathVariable("id") Integer Id) { public String shopArticlesById(Model model, @PathVariable("id") Integer Id) {
@ -54,22 +60,23 @@ public class ShopArticleController {
@PostMapping("/{id}") @PostMapping("/{id}")
public String shopArticlesByIdBuy(HttpSession session, public String shopArticlesByIdBuy(HttpSession session,
@RequestAttribute(value = "user", required = false) User customer, @RequestAttribute(value = "shoppingCart") ShoppingCart shoppingCart,
// @RequestAttribute(value = "shoppingCart", required = true) ShoppingCart shoppingCart, @PathVariable("id") Long id,
@PathVariable("id") Integer id, @RequestParam("quantity") Integer quantity,
@RequestParam("fastcheckout") Boolean fastcheckout @RequestParam("fastcheckout") Boolean fastcheckout
) { ) {
if (customer != null) {
//TODO: Add Article to Shopping Cart Optional<Article> article = articleRepository.findById(id);
if (!article.isPresent()) {
if (!fastcheckout) { throw new RuntimeException("Article not found!");
return "shop/articles/post_add"; }
} else {
return "shop/checkout"; shoppingCart.addArticle(article.get(), quantity);
}
if (!fastcheckout) {
return "shop/articles/post_add";
} else { } else {
session.setAttribute("afterLogin", "/shop/articles/" + id); return "shop/checkout";
return "redirect:/login";
} }
} }
} }

View File

@ -1,8 +1,30 @@
package org.hso.ecommerce.controller.shop; package org.hso.ecommerce.controller.shop;
import org.springframework.stereotype.Controller; 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 @Controller
//@RequestMapping("...") @RequestMapping("/shop/")
public class ShopCheckoutController { 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";
}
} }

View File

@ -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;
}
}
}

View File

@ -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> {
}

View File

@ -26,7 +26,7 @@
</form> </form>
</div> </div>
</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>
<div th:if="${error}" class="error" id="error-msg"> <div th:if="${error}" class="error" id="error-msg">
<div class="content-width bar-flex"> <div class="content-width bar-flex">

View File

@ -34,10 +34,9 @@
<div class="detailgrid m"> <div class="detailgrid m">
<h2 th:text="${article.shopPricePerUnitNetCent}"></h2> <h2 th:text="${article.shopPricePerUnitNetCent}"></h2>
<div> <div>
<label class="nolinebreak">Menge:</label> <label class="nolinebreak">Menge:</label>
<select size="1"> <select name="quantity" size="1">
<option th:each="quantity : ${#numbers.sequence(1,100)}" <option th:each="quantity : ${#numbers.sequence(1,10)}"
th:value="${quantity}" th:value="${quantity}"
th:text="${quantity}"></option> th:text="${quantity}"></option>
</select> </select>