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 eb13de9..3b906d0 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 @@ -74,11 +74,11 @@ public class ShopArticleController { 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 (article == null) { + request.setAttribute("error", "Der Artikel wurde nicht gefunden."); + response.setStatus(HttpServletResponse.SC_NOT_FOUND); + return "error/404"; + } if (setAmount != null && setAmount) { shoppingCart.setArticleCount(article, quantity); 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 8844c8b..9fa8c37 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 @@ -2,6 +2,7 @@ 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.user.User; import org.hso.ecommerce.repos.shop.ArticleRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -31,7 +32,7 @@ public class ShopIndexController { List
commercialArticles = GetRandomArticlesAction.getRandomArticles(8, articleRepository.getAdvertisedArticles()); model.addAttribute("commercialArticles", commercialArticles); - //check if logged in + boolean isLoggedIn = false; boolean hasOrders = false; if (session != null && session.getAttribute("id") != null) { @@ -40,7 +41,8 @@ public class ShopIndexController { } if (isLoggedIn) { - List
suggestedArticles = articleRepository.getLastOrderedArticles("4"); + List
suggestedArticles = articleRepository.getLastOrderedArticles(); + suggestedArticles = suggestedArticles.size() > 3 ? suggestedArticles.subList(0,3) : suggestedArticles; //only latest 4 articles if (suggestedArticles.size() > 0) { model.addAttribute("suggestedArticles", suggestedArticles); hasOrders = true; 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 a22f191..933959f 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 @@ -13,16 +13,16 @@ import java.util.Optional; @Repository public interface ArticleRepository extends JpaRepository { - @Query("Select a FROM Article a where a.id = :articleId") + @Query("SELECT a FROM Article a WHERE a.id = :articleId") Article findArticleById(@Param("articleId") long articleId); - @Query("SELECT a from Article a join a.related ao where ao.should_be_advertised = true") + @Query("SELECT a FROM Article a JOIN a.related ao WHERE ao.should_be_advertised = true") List
getAdvertisedArticles(); - @Query("SELECT c FROM User c WHERE c.email = :quantity") - List
getLastOrderedArticles(@Param("quantity") String quantity); + @Query("SELECT a FROM CustomerOrderPosition cop JOIN cop.order co JOIN co.customer c JOIN cop.article a ORDER BY co.id DESC") + List
getLastOrderedArticles(); }