implement search
This commit is contained in:
parent
0840eda369
commit
3f5b1efa0c
|
@ -85,11 +85,6 @@ public class RequestController {
|
||||||
return "redirect:/";
|
return "redirect:/";
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/shop/search")
|
|
||||||
public String shopSearch() {
|
|
||||||
return "shop/search";
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/intern/")
|
@GetMapping("/intern/")
|
||||||
public String intern() {
|
public String intern() {
|
||||||
return "intern/index";
|
return "intern/index";
|
||||||
|
|
|
@ -5,6 +5,7 @@ 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;
|
||||||
|
import org.hso.ecommerce.repos.shop.CategoryRepository;
|
||||||
import org.hso.ecommerce.repos.warehouse.WarehouseBookingPositionSlotEntryRepository;
|
import org.hso.ecommerce.repos.warehouse.WarehouseBookingPositionSlotEntryRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
|
@ -30,12 +31,17 @@ public class ShopArticleController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private final WarehouseBookingPositionSlotEntryRepository warehouseBookingPositionSlotEntryRepository = null;
|
private final WarehouseBookingPositionSlotEntryRepository warehouseBookingPositionSlotEntryRepository = null;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private final CategoryRepository categoryRepository = null;
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
public String shopArticlesById(Model model,
|
public String shopArticlesById(Model model,
|
||||||
@PathVariable("id") Long id,
|
@PathVariable("id") Long id,
|
||||||
HttpServletRequest request,
|
HttpServletRequest request,
|
||||||
HttpServletResponse response
|
HttpServletResponse response
|
||||||
) {
|
) {
|
||||||
|
model.addAttribute("categories", categoryRepository.getCategories());
|
||||||
|
|
||||||
Article article = articleRepository.findArticleById(id);
|
Article article = articleRepository.findArticleById(id);
|
||||||
|
|
||||||
if (article == null) {
|
if (article == null) {
|
||||||
|
|
|
@ -1,8 +1,48 @@
|
||||||
package org.hso.ecommerce.controller.shop;
|
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.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
//@RequestMapping("...")
|
@RequestMapping("/shop/search")
|
||||||
public class ShopSearchController {
|
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<Article> articles = articleRepository.getArticlesByTerm(term);
|
||||||
|
model.addAttribute("articles", articles);
|
||||||
|
|
||||||
|
} else if (category != null) { //if search by Category
|
||||||
|
|
||||||
|
List<Article> articles = articleRepository.getArticlesByCategory(category);
|
||||||
|
model.addAttribute("articles", articles);
|
||||||
|
|
||||||
|
}
|
||||||
|
//TODO: hier eventuell noch Errorhandling für nix von beidem
|
||||||
|
|
||||||
|
return "/shop/search";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -27,4 +27,11 @@ public interface ArticleRepository extends JpaRepository<Article, Long> {
|
||||||
List<Article> getOrderedArticles(long customerId);
|
List<Article> getOrderedArticles(long customerId);
|
||||||
|
|
||||||
|
|
||||||
|
@Query("SELECT a FROM Article a WHERE a.title LIKE %:term%")
|
||||||
|
List<Article> getArticlesByTerm(String term);
|
||||||
|
|
||||||
|
@Query("SELECT a FROM Article a JOIN a.categories c WHERE :category = c.name") //TODO: Wahrscheinlich falsch!
|
||||||
|
List<Article>getArticlesByCategory(String category);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,4 +14,6 @@ public interface CategoryRepository extends JpaRepository<Category, Long> {
|
||||||
@Query("SELECT a FROM Category a WHERE a.name = :name")
|
@Query("SELECT a FROM Category a WHERE a.name = :name")
|
||||||
Optional<Category> findCategoryByName(@Param("name") String name);
|
Optional<Category> findCategoryByName(@Param("name") String name);
|
||||||
|
|
||||||
}
|
@Query("SELECT c FROM Category c")
|
||||||
|
List<Category> getCategories();
|
||||||
|
}
|
|
@ -11,7 +11,7 @@
|
||||||
<div class='content-width bar-flex'>
|
<div class='content-width bar-flex'>
|
||||||
<a class="button no-padding" href="/"><img class="logo" th:src="@{/img/ecom-logo-base.svg}"></a>
|
<a class="button no-padding" href="/"><img class="logo" th:src="@{/img/ecom-logo-base.svg}"></a>
|
||||||
<form class='spacer input-icon secondary' th:action="@{/shop/search}" method="GET">
|
<form class='spacer input-icon secondary' th:action="@{/shop/search}" method="GET">
|
||||||
<input type="text" placeholder="Nach Produkten suchen..."/>
|
<input type="text" name="term" placeholder="Nach Produkten suchen..."/>
|
||||||
<button>Finden</button>
|
<button>Finden</button>
|
||||||
</form>
|
</form>
|
||||||
<a th:unless="${user}" class="button" th:href="@{/login}">Login</a>
|
<a th:unless="${user}" class="button" th:href="@{/login}">Login</a>
|
||||||
|
|
|
@ -7,15 +7,10 @@
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<nav th:fragment="sidebar">
|
<nav th:fragment="sidebar (categories)">
|
||||||
<h1>Kategorien</h1>
|
<h1>Kategorien</h1>
|
||||||
<ul class="secondary">
|
<ul class="secondary">
|
||||||
<li><a href="/shop/search">Aufnahmegeräte</a></li>
|
<li th:each="category: ${categories}"><a th:href="@{/shop/search(category=${category.name})}" th:text="${category.name}"></a></li>
|
||||||
<li><a href="/shop/search">Computer</a></li>
|
|
||||||
<li><a href="/shop/search">Fernseher</a></li>
|
|
||||||
<li><a href="/shop/search">Handys</a></li>
|
|
||||||
<li><a href="/shop/search">Unterhaltungselektronik</a></li>
|
|
||||||
<li><a href="/shop/search">Sonstiges</a></li>
|
|
||||||
</ul>
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
</body>
|
</body>
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
<nav th:replace="fragments/header :: header">Header</nav>
|
<nav th:replace="fragments/header :: header">Header</nav>
|
||||||
<main class=" content-width">
|
<main class=" content-width">
|
||||||
<div class="sidebar-layout" style="min-height: 75vh;">
|
<div class="sidebar-layout" style="min-height: 75vh;">
|
||||||
<nav th:replace="fragments/shop :: sidebar"></nav>
|
<nav th:replace="fragments/shop :: sidebar(categories=${categories})"></nav>
|
||||||
<div class="content-width">
|
<div class="content-width">
|
||||||
<div class="detailgrid">
|
<div class="detailgrid">
|
||||||
<div class="s">
|
<div class="s">
|
||||||
|
|
|
@ -22,14 +22,14 @@
|
||||||
</div>
|
</div>
|
||||||
<div class='grid m base shadow' th:if="${commercialArticles.size() != 0}">
|
<div class='grid m base shadow' th:if="${commercialArticles.size() != 0}">
|
||||||
<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})}"/>
|
<img th:src="@{/shop/articles/{id}/image.jpg(id=${article.id})}"/>
|
||||||
<h2 th:text="${article.title}" />
|
<h2 th:text="${article.title}" />
|
||||||
<p class='price'><span th:text="${#numbers.formatDecimal(article.getPriceGross() * 0.01, 1, 'POINT', 2, 'COMMA')}"></span><span> EUR</span></p>
|
<p class='price'><span th:text="${#numbers.formatDecimal(article.getPriceGross() * 0.01, 1, 'POINT', 2, 'COMMA')}"></span><span> EUR</span></p>
|
||||||
<p th:text="${article.description}" />
|
<p th:text="${article.description}" />
|
||||||
</a>
|
</a>
|
||||||
</section>
|
</section>
|
||||||
<section class="spacer"></section>
|
<section class="spacer"></section>
|
||||||
<section class="spacer"></section>
|
<section class="spacer"></section>
|
||||||
<section class="spacer"></section>
|
<section class="spacer"></section>
|
||||||
|
@ -57,7 +57,7 @@
|
||||||
|
|
||||||
<!-- If User is logged in but hasn't ordered anything yet-->
|
<!-- If User is logged in but hasn't ordered anything yet-->
|
||||||
<div th:if="${isLoggedIn == true and hasOrders == false}">
|
<div th:if="${isLoggedIn == true and hasOrders == false}">
|
||||||
<h1>Jetzt Shoppen und Empfehlungen erhalten!</h1>
|
<h2>Jetzt Shoppen und Empfehlungen erhalten!</h2>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- If User is logged in and has ordered something before-->
|
<!-- If User is logged in and has ordered something before-->
|
||||||
|
|
|
@ -21,70 +21,27 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<main class="sidebar-layout content-width">
|
<main class="sidebar-layout content-width">
|
||||||
<nav th:replace="fragments/shop :: sidebar"></nav>
|
<nav th:replace="fragments/shop :: sidebar(categories=${categories})"></nav>
|
||||||
<div class="content-width">
|
<div class="content-width">
|
||||||
<div class='grid m condensed'>
|
<div class='grid m condensed' th:if="${articles.size() != 0}">
|
||||||
<section><a th:href="@{/shop/articles/1234}" class="section">
|
<section th:each="article: ${articles}">
|
||||||
<img th:src="@{/img/product-1.jpg}">
|
<a th:href="@{/shop/articles/{id}(id=${article.id})}" class="section">
|
||||||
<h2>Tolle Kamera</h2>
|
|
||||||
<p class='price'> 25.14 EUR</p>
|
|
||||||
<p>
|
|
||||||
Eine TOLLE Kamera <br>
|
|
||||||
Jaja du denkst jetzt bestimmt: "Bei dem Preis kann sie gar nich sooo TOLL sein". <br>
|
|
||||||
Aber glaub mir, sie is echt echt TOLL! <br>
|
|
||||||
Indianerehrenwort!
|
|
||||||
</p>
|
|
||||||
</a>
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<section><a th:href="@{/shop/articles/1234}" class="section">
|
<img th:src="@{/shop/articles/{id}/image.jpg(id=${article.id})}"/>
|
||||||
<img th:src="@{/img/product-2.jpg}">
|
<h2 th:text="${article.title}" />
|
||||||
<h2>Bluetooth Kopfhörer</h2>
|
<p class='price'><span th:text="${#numbers.formatDecimal(article.getPriceGross() * 0.01, 1, 'POINT', 2, 'COMMA')}"></span><span> EUR</span></p>
|
||||||
<p class='price'> 10.14 EUR</p>
|
<p th:text="${article.description}" />
|
||||||
<p>
|
</a>
|
||||||
Sind halt Kopfhörer ohne Kabel, mehr gibts da nich zu sagen.
|
|
||||||
</p>
|
|
||||||
</a>
|
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<section><a th:href="@{/shop/articles/1234}" class="section">
|
|
||||||
<img th:src="@{/img/product-3.jpg}">
|
|
||||||
<h2>???</h2>
|
|
||||||
<p class='price'> 25.14 EUR</p>
|
|
||||||
<p>
|
|
||||||
Ich weiß selbst nich was das genau sein soll.<br>
|
|
||||||
Wenn dus willst kannst es gern haben, musst nur das Geld überweisen.
|
|
||||||
</p>
|
|
||||||
</a>
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<section><a th:href="@{/shop/articles/1234}" class="section">
|
|
||||||
<img th:src="@{/img/product-4.jpg}">
|
|
||||||
<h2>Kamera Stativ.</h2>
|
|
||||||
<p class='price'> 25.14 EUR</p>
|
|
||||||
<p>
|
|
||||||
Das Stativ der Zukunft! Jetzt kaufen und verwenden für
|
|
||||||
wackelfreie Bilder aus der Zukunft!.
|
|
||||||
</p>
|
|
||||||
</a>
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<section><a th:href="@{/shop/articles/1234}" class="section">
|
|
||||||
<img th:src="@{/img/product-5.jpg}">
|
|
||||||
<h2>Bluetooth Ersatzfernbedinung</h2>
|
|
||||||
<p class='price'> 10.14 EUR</p>
|
|
||||||
<p>
|
|
||||||
Kann alles und jeden ausknippsen.
|
|
||||||
</p>
|
|
||||||
</a>
|
|
||||||
</section>
|
|
||||||
|
|
||||||
|
|
||||||
<section class="spacer"></section>
|
<section class="spacer"></section>
|
||||||
<section class="spacer"></section>
|
<section class="spacer"></section>
|
||||||
<section class="spacer"></section>
|
<section class="spacer"></section>
|
||||||
<section class="spacer"></section>
|
<section class="spacer"></section>
|
||||||
</div>
|
</div>
|
||||||
|
<div th:if="${articles.size() == 0}">
|
||||||
|
<h2>Keine Ergebnisse für diesen Suchterm gefunden!</h2>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
<footer th:replace="fragments/footer :: footer"></footer>
|
<footer th:replace="fragments/footer :: footer"></footer>
|
||||||
|
|
Reference in New Issue