diff --git a/prototype/src/main/java/org/hso/ecommerce/action/shop/GetRandomArticlesAction.java b/prototype/src/main/java/org/hso/ecommerce/action/shop/GetRandomArticlesAction.java new file mode 100644 index 0000000..2c6fd55 --- /dev/null +++ b/prototype/src/main/java/org/hso/ecommerce/action/shop/GetRandomArticlesAction.java @@ -0,0 +1,20 @@ +package org.hso.ecommerce.action.shop; + +import org.hso.ecommerce.entities.shop.Article; + +import java.util.ArrayList; +import java.util.List; + +public class GetRandomArticlesAction { + + public static List
getRandomArticles(int quantity, List
advertisedArticles) { + List
randomisedArticles = new ArrayList
(); + for (int i = 0; i < quantity; i++) { + int index = (int) (Math.random() * advertisedArticles.size()); + randomisedArticles.add(advertisedArticles.remove(index)); + if(advertisedArticles.size() == 0) + break; + } + return randomisedArticles; + } +} 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 6d8cea9..eb13de9 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,6 +1,7 @@ package org.hso.ecommerce.controller.shop; import org.apache.tomcat.util.http.fileupload.IOUtils; +import org.hso.ecommerce.action.shop.GetRandomArticlesAction; import org.hso.ecommerce.entities.shop.Article; import org.hso.ecommerce.entities.shop.ShoppingCart; import org.hso.ecommerce.repos.shop.ArticleRepository; @@ -16,6 +17,7 @@ import javax.servlet.http.HttpSession; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.util.ArrayList; import java.util.List; @Controller @@ -36,7 +38,7 @@ public class ShopArticleController { Article article = articleRepository.findArticleById(id); - if(article == null) { + if (article == null) { request.setAttribute("error", "Der Artikel wurde nicht gefunden."); response.setStatus(HttpServletResponse.SC_NOT_FOUND); return "error/404"; @@ -51,7 +53,7 @@ public class ShopArticleController { model.addAttribute("inStock", false); } - List
commercialArticles = articleRepository.getAdvertisedArticles(); + List
commercialArticles = GetRandomArticlesAction.getRandomArticles(4, articleRepository.getAdvertisedArticles()); model.addAttribute("commercialArticles", commercialArticles); 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 73c3241..8844c8b 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,5 +1,6 @@ package org.hso.ecommerce.controller.shop; +import org.hso.ecommerce.action.shop.GetRandomArticlesAction; import org.hso.ecommerce.entities.shop.Article; import org.hso.ecommerce.repos.shop.ArticleRepository; import org.springframework.beans.factory.annotation.Autowired; @@ -9,6 +10,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpSession; +import java.util.ArrayList; import java.util.List; @Controller @@ -26,7 +28,7 @@ public class ShopIndexController { @GetMapping("/shop/") public String shop(Model model, HttpSession session) { - List
commercialArticles = articleRepository.getAdvertisedArticles(); + List
commercialArticles = GetRandomArticlesAction.getRandomArticles(8, articleRepository.getAdvertisedArticles()); model.addAttribute("commercialArticles", commercialArticles); //check if logged in @@ -39,7 +41,7 @@ public class ShopIndexController { if (isLoggedIn) { List
suggestedArticles = articleRepository.getLastOrderedArticles("4"); - if(suggestedArticles.size() > 0) { + if (suggestedArticles.size() > 0) { model.addAttribute("suggestedArticles", suggestedArticles); hasOrders = true; } diff --git a/prototype/src/main/resources/templates/shop/articles/id.html b/prototype/src/main/resources/templates/shop/articles/id.html index c82aedb..73de367 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