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..34cb03d 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()); + Article article = articleRepository.findArticleById(id); if (article == null) { 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..4819607 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.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 + ) { + + model.addAttribute("categories", categoryRepository.getCategories()); + + if (term != null) { //if search by Term + + List
articles = articleRepository.getArticlesByTerm(term); + model.addAttribute("articles", articles); + + } else if (category != null) { //if search by Category + + List
articles = articleRepository.getArticlesByCategory(category); + model.addAttribute("articles", articles); + + } + //TODO: hier eventuell noch Errorhandling für nix von beidem + + 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 6f79487..3a3f5d7 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 @@ -27,4 +27,11 @@ public interface ArticleRepository extends JpaRepository { List
getOrderedArticles(long customerId); + @Query("SELECT a FROM Article a WHERE a.title LIKE %:term%") + List
getArticlesByTerm(String term); + + @Query("SELECT a FROM Article a JOIN a.categories c WHERE :category = c.name") //TODO: Wahrscheinlich falsch! + List
getArticlesByCategory(String category); + + } 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..756a6c1 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 @@ -14,4 +14,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..1ec92b4 100644 --- a/prototype/src/main/resources/templates/fragments/header.html +++ b/prototype/src/main/resources/templates/fragments/header.html @@ -11,7 +11,7 @@
- +
Login 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 @@ -