diff --git a/prototype/gradlew b/prototype/gradlew old mode 100755 new mode 100644 diff --git a/prototype/src/main/java/org/hso/ecommerce/app/RequestController.java b/prototype/src/main/java/org/hso/ecommerce/app/RequestController.java index a4c4627..36b47ce 100644 --- a/prototype/src/main/java/org/hso/ecommerce/app/RequestController.java +++ b/prototype/src/main/java/org/hso/ecommerce/app/RequestController.java @@ -85,11 +85,6 @@ public class RequestController { return "redirect:/"; } - @GetMapping("/shop/search") - public String shopSearch() { - return "shop/search"; - } - @GetMapping("/intern/") public String intern() { return "intern/index"; 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 869b237..235669c 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 @@ -5,6 +5,7 @@ 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; +import org.hso.ecommerce.repos.shop.CategoryRepository; import org.hso.ecommerce.repos.warehouse.WarehouseBookingPositionSlotEntryRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; @@ -30,12 +31,17 @@ public class ShopArticleController { @Autowired private final WarehouseBookingPositionSlotEntryRepository warehouseBookingPositionSlotEntryRepository = null; + @Autowired + private final CategoryRepository categoryRepository = null; + @GetMapping("/{id}") public String shopArticlesById(Model model, @PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response ) { + model.addAttribute("categories", categoryRepository.getCategories()); //for sidebar + Article article = articleRepository.findArticleById(id); if (article == null) { @@ -45,13 +51,13 @@ public class ShopArticleController { } model.addAttribute("article", article); - if (warehouseBookingPositionSlotEntryRepository.getByArticle(id).get(0).newSumSlot > 0) { + if (warehouseBookingPositionSlotEntryRepository.getByArticle(id).get(0).newSumSlot > 0) { //check if in Stock model.addAttribute("inStock", true); } else { model.addAttribute("inStock", false); } - List
commercialArticles = GetRandomArticlesAction.getRandomArticles(3, articleRepository.getAdvertisedArticles()); + List
commercialArticles = GetRandomArticlesAction.getRandomArticles(3, articleRepository.getAdvertisedArticles()); //get 3 advertised Articles model.addAttribute("commercialArticles", commercialArticles); return "shop/articles/id"; @@ -98,5 +104,4 @@ public class ShopArticleController { response.setContentType(MediaType.IMAGE_JPEG_VALUE); IOUtils.copy(in, response.getOutputStream()); } -} - +} \ No newline at end of file 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 15b6067..71ecf70 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 @@ -3,6 +3,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.repos.shop.ArticleRepository; +import org.hso.ecommerce.repos.warehouse.WarehouseBookingPositionSlotEntryRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -19,6 +20,9 @@ public class ShopIndexController { @Autowired private final ArticleRepository articleRepository = null; + @Autowired + private final WarehouseBookingPositionSlotEntryRepository warehouseBookingPositionSlotEntryRepository = null; + @GetMapping("/") public String home() { return "redirect:/shop/"; @@ -27,18 +31,18 @@ public class ShopIndexController { @GetMapping("/shop/") public String shop(Model model, HttpSession session) { - List
commercialArticles = GetRandomArticlesAction.getRandomArticles(8, articleRepository.getAdvertisedArticles()); + List
commercialArticles = GetRandomArticlesAction.getRandomArticles(8, articleRepository.getAdvertisedArticles()); //get random advertised Articles model.addAttribute("commercialArticles", commercialArticles); boolean isLoggedIn = false; boolean hasOrders = false; - if (session != null && session.getAttribute("userId") != null) { + if (session != null && session.getAttribute("userId") != null) { //check if logged in long userId = (long) session.getAttribute("userId"); isLoggedIn = true; List
suggestedArticles = articleRepository.getOrderedArticles(userId); - suggestedArticles = suggestedArticles.size() > 3 ? suggestedArticles.subList(0, 4) : suggestedArticles; //only latest 4 articles + suggestedArticles = suggestedArticles.size() > 3 ? suggestedArticles.subList(0, 4) : suggestedArticles; //only latest 4 ordered articles if (suggestedArticles.size() > 0) { model.addAttribute("suggestedArticles", suggestedArticles); hasOrders = true; @@ -65,5 +69,4 @@ public class ShopIndexController { public String privacy() { return "privacy"; } - } diff --git a/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopSearchController.java b/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopSearchController.java index 05a795f..1d14c61 100644 --- a/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopSearchController.java +++ b/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopSearchController.java @@ -1,8 +1,48 @@ package org.hso.ecommerce.controller.shop; +import org.hso.ecommerce.entities.shop.Article; +import org.hso.ecommerce.repos.shop.ArticleRepository; +import org.hso.ecommerce.repos.shop.CategoryRepository; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.List; @Controller -//@RequestMapping("...") +@RequestMapping("/shop/search") public class ShopSearchController { -} + + @Autowired + private final ArticleRepository articleRepository = null; + + @Autowired + private final CategoryRepository categoryRepository = null; + + @GetMapping("") + public String searchArticles(@RequestParam(required = false, value = "term") String term, + @RequestParam(required = false, value = "category") String category, + Model model, + HttpServletRequest request, + HttpServletResponse response + ) { + model.addAttribute("categories", categoryRepository.getCategories()); //for sidebar + + if (term != null) { //if search by Term + List
articles = articleRepository.getArticlesByTerm(term); //search by Term + model.addAttribute("articles", articles); + } else if (category != null) { //if search by Category + List
articles = articleRepository.getArticlesByCategory(category); //search by Category + model.addAttribute("articles", articles); + } else { + request.setAttribute("error", "Es wurden keine Suchparameter angegeben."); + response.setStatus(HttpServletResponse.SC_NOT_FOUND); + return "error/404"; + } + + return "/shop/search"; + } +} \ No newline at end of file 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 6ac9e1e..0340f59 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 @@ -18,7 +18,7 @@ public interface ArticleRepository extends JpaRepository { @Query("SELECT a FROM Article a") List
findAll(); - @Query("SELECT a FROM Article a JOIN a.related ao WHERE ao.shouldBeAdvertised = true") + @Query(value = "Select a.* from articles as a, article_offers as ao, warehouse_booking_position_entries as wbpe where a.related_id = ao.id and wbpe.article_id = a.id and ao.should_be_advertised = true group by wbpe.slot_id having max(wbpe.id) and wbpe.new_sum_slot != 0", nativeQuery = true) List
getAdvertisedArticles(); @Query("SELECT a FROM CustomerOrderPosition cop JOIN cop.order co JOIN co.customer c JOIN cop.article a ORDER BY co.id DESC") @@ -30,4 +30,9 @@ public interface ArticleRepository extends JpaRepository { @Query(value = "SELECT a.id FROM articles a WHERE a.related_id = :relatedId", nativeQuery = true) Optional findArticleIDByRelatedID(@Param("relatedId") long relatedId); -} + @Query(value = "Select a.* from articles as a, warehouse_booking_position_entries as wbpe where wbpe.article_id = a.id and a.title LIKE %:term% group by wbpe.slot_id having max(wbpe.id) and wbpe.new_sum_slot != 0", nativeQuery = true) + List
getArticlesByTerm(String term); + + @Query(value = "Select a.* from articles as a, categories as c, article_categories_bindings as acb, warehouse_booking_position_entries as wbpe where wbpe.article_id = a.id and acb.articles_id = a.id and acb.categories_id = c.id and c.name = :category group by wbpe.slot_id having max(wbpe.id) and wbpe.new_sum_slot != 0", nativeQuery = true) + List
getArticlesByCategory(String category); +} \ No newline at end of file diff --git a/prototype/src/main/java/org/hso/ecommerce/repos/shop/CategoryRepository.java b/prototype/src/main/java/org/hso/ecommerce/repos/shop/CategoryRepository.java index b345d7d..4f4133a 100644 --- a/prototype/src/main/java/org/hso/ecommerce/repos/shop/CategoryRepository.java +++ b/prototype/src/main/java/org/hso/ecommerce/repos/shop/CategoryRepository.java @@ -6,6 +6,7 @@ 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 @@ -14,4 +15,6 @@ public interface CategoryRepository extends JpaRepository { @Query("SELECT a FROM Category a WHERE a.name = :name") Optional findCategoryByName(@Param("name") String name); -} + @Query("SELECT c FROM Category c") + List getCategories(); +} \ No newline at end of file diff --git a/prototype/src/main/resources/templates/fragments/header.html b/prototype/src/main/resources/templates/fragments/header.html index 7b6ec7c..054c332 100644 --- a/prototype/src/main/resources/templates/fragments/header.html +++ b/prototype/src/main/resources/templates/fragments/header.html @@ -11,7 +11,7 @@
- +
Login @@ -38,7 +38,6 @@ function toggle(id) { document.getElementById(id).classList.toggle("invisible"); } - X
@@ -54,7 +53,6 @@ function toggle(id) { document.getElementById(id).classList.toggle("invisible"); } - X @@ -62,5 +60,4 @@ - diff --git a/prototype/src/main/resources/templates/fragments/shop.html b/prototype/src/main/resources/templates/fragments/shop.html index 7a87073..d6dea6a 100644 --- a/prototype/src/main/resources/templates/fragments/shop.html +++ b/prototype/src/main/resources/templates/fragments/shop.html @@ -7,15 +7,10 @@ -