From 4fd80bb3c76639c46640550498309da340afb447 Mon Sep 17 00:00:00 2001 From: Hannes Date: Thu, 7 May 2020 15:48:00 +0200 Subject: [PATCH] implement imageserving and first part of ArticleRepository --- .gitignore | 2 + prototype/.gitignore | 2 +- .../shop/ShopArticleController.java | 84 +++++++++++-------- .../controller/shop/ShopIndexController.java | 64 ++++---------- .../hso/ecommerce/entities/shop/Article.java | 1 + .../hso/ecommerce/entities/shop/Image.java | 5 +- .../repos/shop/ArticleRepository.java | 20 +++++ .../resources/templates/shop/articles/id.html | 4 +- .../main/resources/templates/shop/index.html | 8 +- 9 files changed, 98 insertions(+), 92 deletions(-) diff --git a/.gitignore b/.gitignore index 3b718f8..3912d2e 100644 --- a/.gitignore +++ b/.gitignore @@ -89,3 +89,5 @@ local.properties # SQLite prototype/*.db + +prototype/images diff --git a/prototype/.gitignore b/prototype/.gitignore index 59529a7..4fac9a9 100644 --- a/prototype/.gitignore +++ b/prototype/.gitignore @@ -1,4 +1,4 @@ -./test.db +./e-commerce.db ./build ./gradle ./out 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 3cb860f..a6159d9 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,9 +1,11 @@ package org.hso.ecommerce.controller.shop; +import org.apache.tomcat.util.http.fileupload.IOUtils; import org.hso.ecommerce.entities.shop.Article; import org.hso.ecommerce.entities.shop.ShoppingCart; import org.hso.ecommerce.repos.shop.ArticleRepository; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; @@ -11,7 +13,10 @@ import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; -import java.util.ArrayList; +import java.io.ByteArrayInputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; import java.util.List; import java.util.Optional; @@ -23,40 +28,35 @@ public class ShopArticleController { private final ArticleRepository articleRepository = null; @GetMapping("/{id}") - public String shopArticlesById(Model model, @PathVariable("id") Integer Id) { + public String shopArticlesById(Model model, + @PathVariable("id") Long id, + HttpServletRequest request, + HttpServletResponse response + ) { - //TODO: Get Article by Id instead of this dummy shit - Article d1 = new Article(); - d1.description = "this is dummy1"; - d1.title = "dummy1"; - d1.shopPricePerUnitNetCent = 1500; - d1.id = 1234; - model.addAttribute("article", d1); + Article article = articleRepository.findArticleById(id); + + + if(article == null) { + request.setAttribute("error", "Der Artikel wurde nicht gefunden."); + response.setStatus(HttpServletResponse.SC_NOT_FOUND); + return "error/404"; + } + + model.addAttribute("article", article); //TODO: Check if in Stock - if(false){ + if (false) { model.addAttribute("inStock", true); - }else{ + } else { model.addAttribute("inStock", false); } - //TODO: Get 2 Commercialised Articles - List
commercialArticles = new ArrayList
(); - Article d2 = new Article(); - d2.description = "this is dummy2"; - d2.title = "dummy2"; - d2.shopPricePerUnitNetCent = 2000; - d2.id = 2345; - Article d3 = new Article(); - d3.description = "this is dummy3"; - d3.title = "dummy3"; - d3.shopPricePerUnitNetCent = 2500; - d3.id = 3456; - commercialArticles.add(d2); - commercialArticles.add(d3); + List
commercialArticles = articleRepository.getCommercialisedArticles("2"); model.addAttribute("commercialArticles", commercialArticles); + return "shop/articles/id"; } @@ -71,17 +71,19 @@ public class ShopArticleController { @RequestParam("fastcheckout") Boolean fastcheckout ) { - Optional
article = articleRepository.findById(id); - if (!article.isPresent()) { - request.setAttribute("error", "Der Artikel wurde nicht gefunden."); - response.setStatus(HttpServletResponse.SC_NOT_FOUND); - return "error/404"; - } + + Article article = articleRepository.findArticleById(id); + +// if (!article.isPresent()) { +// request.setAttribute("error", "Der Artikel wurde nicht gefunden."); +// response.setStatus(HttpServletResponse.SC_NOT_FOUND); +// return "error/404"; +// } if (setAmount != null && setAmount) { - shoppingCart.setArticleCount(article.get(), quantity); + shoppingCart.setArticleCount(article, quantity); } else { - shoppingCart.addArticle(article.get(), quantity); + shoppingCart.addArticle(article, quantity); } if (!fastcheckout) { @@ -90,4 +92,20 @@ public class ShopArticleController { return "redirect:/shop/checkout"; } } + + @GetMapping("/{id}/image.jpg") + public void getImageAsByteArray(HttpServletRequest request, + HttpServletResponse response, + @PathVariable("id") Long id + ) throws IOException { + + Article article = articleRepository.findArticleById(id); + + InputStream in = new FileInputStream(article.image.path); + response.setContentType(MediaType.IMAGE_JPEG_VALUE); + IOUtils.copy(in, response.getOutputStream()); + } + + } + diff --git a/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopIndexController.java b/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopIndexController.java index 0be1a50..69dce5d 100644 --- a/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopIndexController.java +++ b/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopIndexController.java @@ -1,6 +1,8 @@ package org.hso.ecommerce.controller.shop; import org.hso.ecommerce.entities.shop.Article; +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.GetMapping; @@ -8,13 +10,15 @@ import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpSession; import javax.swing.*; -import java.awt.*; -import java.util.ArrayList; +import java.util.List; @Controller @RequestMapping("/") public class ShopIndexController { + @Autowired + private final ArticleRepository articleRepository = null; + @GetMapping("/") public String home() { return "redirect:/shop/"; @@ -23,8 +27,7 @@ public class ShopIndexController { @GetMapping("/shop/") public String shop(Model model, HttpSession session) { - //TODO: get commercialised Articles - ArrayList
commercialArticles = getArticles(); + List
commercialArticles = articleRepository.getCommercialisedArticles("4"); model.addAttribute("commercialArticles", commercialArticles); //check if logged in @@ -33,20 +36,19 @@ public class ShopIndexController { if (session != null && session.getAttribute("id") != null) { long userId = (long) session.getAttribute("id"); isLoggedIn = true; - if (false) { - hasOrders = true; //TODO: Find out whether user has orders! + } + + if (isLoggedIn) { + List
suggestedArticles = articleRepository.getLastOrderedArticles("4"); + if(suggestedArticles.size() > 0) { + model.addAttribute("suggestedArticles", suggestedArticles); + hasOrders = true; } } + model.addAttribute("isLoggedIn", isLoggedIn); model.addAttribute("hasOrders", hasOrders); - - if (hasOrders) { - //TODO: get up to last 4 Orders - ArrayList
suggestedArticles = getArticles(); - model.addAttribute("suggestedArticles", suggestedArticles); - } - return "shop/index"; } @@ -65,40 +67,4 @@ public class ShopIndexController { return "privacy"; } - - public ArrayList
getArticles() { - ArrayList
dummyArticles = new ArrayList
(); - - Article d1 = new Article(); - d1.description = "this is dummy1"; - d1.title = "dummy1"; - d1.shopPricePerUnitNetCent = 1500; - d1.id = 1234; - dummyArticles.add(d1); - - Article d2 = new Article(); - d2.description = "this is dummy2"; - d2.title = "dummy2"; - d2.shopPricePerUnitNetCent = 2000; - d2.id = 2345; - dummyArticles.add(d2); - - Article d3 = new Article(); - d3.description = "this is dummy3"; - d3.title = "dummy3"; - d3.shopPricePerUnitNetCent = 2500; - d3.id = 3456; - dummyArticles.add(d3); - - Article d4 = new Article(); - d4.description = "this is dummy4"; - d4.title = "dummy4"; - d4.shopPricePerUnitNetCent = 3000; - d4.id = 4567; - dummyArticles.add(d4); - - return dummyArticles; - } - - } diff --git a/prototype/src/main/java/org/hso/ecommerce/entities/shop/Article.java b/prototype/src/main/java/org/hso/ecommerce/entities/shop/Article.java index 2e066cd..a26cb39 100644 --- a/prototype/src/main/java/org/hso/ecommerce/entities/shop/Article.java +++ b/prototype/src/main/java/org/hso/ecommerce/entities/shop/Article.java @@ -32,6 +32,7 @@ public class Article { public String description; @OneToOne(optional = true) + @Basic(fetch = FetchType.LAZY) public Image image; @ManyToMany diff --git a/prototype/src/main/java/org/hso/ecommerce/entities/shop/Image.java b/prototype/src/main/java/org/hso/ecommerce/entities/shop/Image.java index 33eebdb..d27d415 100644 --- a/prototype/src/main/java/org/hso/ecommerce/entities/shop/Image.java +++ b/prototype/src/main/java/org/hso/ecommerce/entities/shop/Image.java @@ -11,7 +11,6 @@ public class Image { @Basic public long id; - @Lob - @Column(name = "data", columnDefinition = "BLOB", nullable = false) - private byte[] data; + public String path; } + 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 index 0ac4bd2..42ef5ee 100644 --- a/prototype/src/main/java/org/hso/ecommerce/repos/shop/ArticleRepository.java +++ b/prototype/src/main/java/org/hso/ecommerce/repos/shop/ArticleRepository.java @@ -1,11 +1,31 @@ package org.hso.ecommerce.repos.shop; import org.hso.ecommerce.entities.shop.Article; +import org.hso.ecommerce.entities.user.User; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import java.util.List; +import java.util.Optional; + @Repository public interface ArticleRepository extends JpaRepository { + @Query(nativeQuery = true, value = "Select * FROM articles where articles.id = :articleId") + Article findArticleById(@Param("articleId") long articleId); + + + //TODO: getCommercialisedArticles(int quantity) + @Query("SELECT c FROM User c WHERE c.email = :quantity") + List
getCommercialisedArticles(@Param("quantity") String quantity); + + + //TODO: getLastOrderedArticles(int quantity) + @Query("SELECT c FROM User c WHERE c.email = :quantity") + List
getLastOrderedArticles(@Param("quantity") String quantity); + + } diff --git a/prototype/src/main/resources/templates/shop/articles/id.html b/prototype/src/main/resources/templates/shop/articles/id.html index 46b7c93..c82aedb 100644 --- a/prototype/src/main/resources/templates/shop/articles/id.html +++ b/prototype/src/main/resources/templates/shop/articles/id.html @@ -27,7 +27,7 @@

- +
@@ -56,7 +56,7 @@

Weitere Schnäppchen

-