WIP feature/listedArticles #15

Merged
Seil0 merged 33 commits from feature/listedArticles into master 2020-05-15 19:48:22 +02:00
5 changed files with 31 additions and 7 deletions
Showing only changes of commit 82dacef4c0 - Show all commits

View File

@ -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<Article> getRandomArticles(int quantity, List<Article> advertisedArticles) {
List<Article> randomisedArticles = new ArrayList<Article>();
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;
}
}

View File

@ -1,6 +1,7 @@
package org.hso.ecommerce.controller.shop; package org.hso.ecommerce.controller.shop;
import org.apache.tomcat.util.http.fileupload.IOUtils; 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.Article;
import org.hso.ecommerce.entities.shop.ShoppingCart; import org.hso.ecommerce.entities.shop.ShoppingCart;
import org.hso.ecommerce.repos.shop.ArticleRepository; import org.hso.ecommerce.repos.shop.ArticleRepository;
@ -16,6 +17,7 @@ import javax.servlet.http.HttpSession;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@Controller @Controller
@ -36,7 +38,7 @@ public class ShopArticleController {
Article article = articleRepository.findArticleById(id); Article article = articleRepository.findArticleById(id);
if(article == null) { if (article == null) {
request.setAttribute("error", "Der Artikel wurde nicht gefunden."); request.setAttribute("error", "Der Artikel wurde nicht gefunden.");
response.setStatus(HttpServletResponse.SC_NOT_FOUND); response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return "error/404"; return "error/404";
@ -51,7 +53,7 @@ public class ShopArticleController {
model.addAttribute("inStock", false); model.addAttribute("inStock", false);
} }
List<Article> commercialArticles = articleRepository.getAdvertisedArticles(); List<Article> commercialArticles = GetRandomArticlesAction.getRandomArticles(4, articleRepository.getAdvertisedArticles());
model.addAttribute("commercialArticles", commercialArticles); model.addAttribute("commercialArticles", commercialArticles);

View File

@ -1,5 +1,6 @@
package org.hso.ecommerce.controller.shop; package org.hso.ecommerce.controller.shop;
import org.hso.ecommerce.action.shop.GetRandomArticlesAction;
import org.hso.ecommerce.entities.shop.Article; import org.hso.ecommerce.entities.shop.Article;
import org.hso.ecommerce.repos.shop.ArticleRepository; import org.hso.ecommerce.repos.shop.ArticleRepository;
import org.springframework.beans.factory.annotation.Autowired; 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 org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@Controller @Controller
@ -26,7 +28,7 @@ public class ShopIndexController {
@GetMapping("/shop/") @GetMapping("/shop/")
public String shop(Model model, HttpSession session) { public String shop(Model model, HttpSession session) {
List<Article> commercialArticles = articleRepository.getAdvertisedArticles(); List<Article> commercialArticles = GetRandomArticlesAction.getRandomArticles(8, articleRepository.getAdvertisedArticles());
model.addAttribute("commercialArticles", commercialArticles); model.addAttribute("commercialArticles", commercialArticles);
//check if logged in //check if logged in
@ -39,7 +41,7 @@ public class ShopIndexController {
if (isLoggedIn) { if (isLoggedIn) {
List<Article> suggestedArticles = articleRepository.getLastOrderedArticles("4"); List<Article> suggestedArticles = articleRepository.getLastOrderedArticles("4");
if(suggestedArticles.size() > 0) { if (suggestedArticles.size() > 0) {
model.addAttribute("suggestedArticles", suggestedArticles); model.addAttribute("suggestedArticles", suggestedArticles);
hasOrders = true; hasOrders = true;
} }

View File

@ -27,7 +27,7 @@
<p th:text="${article.description}"></p> <p th:text="${article.description}"></p>
</div> </div>
<div class="s"> <div class="s">
<img th:src="@{/shop/articles/{id}/image.jpg(id=${article.id})}" class="s"/> <img th:src="@{/shop/articles/{id}/image.jpg(id=${article.id})}"/>
</div> </div>
<div class="s"></div> <div class="s"></div>
<form class="s" method="POST"> <form class="s" method="POST">
@ -56,7 +56,7 @@
<h1>Weitere Schnäppchen</h1> <h1>Weitere Schnäppchen</h1>
<div class='grid m base shadow'> <div class='grid m base shadow'>
<section th:each="article: ${commercialArticles}"><a th:href="@{/shop/articles/{id}(id = ${article.id})}" class="section"> <section th:each="article: ${commercialArticles}"><a th:href="@{/shop/articles/{id}(id = ${article.id})}" class="section">
<img th:src="@{/shop/articles/{id}/image.jpg(id=${article.id})}" class="s"/> <img th:src="@{/shop/articles/{id}/image.jpg(id=${article.id})}"/>
<h2 th:text="${article.title}"></h2> <h2 th:text="${article.title}"></h2>
<p class='price' th:text="${article.shopPricePerUnitNetCent}"></p> <p class='price' th:text="${article.shopPricePerUnitNetCent}"></p>
<p th:text="${article.description}"></p> <p th:text="${article.description}"></p>

View File

@ -21,7 +21,7 @@
<section th:each="article: ${commercialArticles}"> <section th:each="article: ${commercialArticles}">
<a th:href="@{/shop/articles/{id}(id=${article.id})}" class="section"> <a th:href="@{/shop/articles/{id}(id=${article.id})}" class="section">
<img th:src="@{/shop/articles/{id}/image.jpg(id=${article.id})}" class="s"/> <img th:src="@{/shop/articles/{id}/image.jpg(id=${article.id})}"/>
<h2 th:text="${article.title}" /> <h2 th:text="${article.title}" />
<p class='price' th:text="${article.shopPricePerUnitNetCent}" /> <p class='price' th:text="${article.shopPricePerUnitNetCent}" />
<p th:text="${article.description}" /> <p th:text="${article.description}" />