implement search

This commit is contained in:
Hannes Huber 2020-05-14 15:43:16 +02:00
parent 0840eda369
commit 3f5b1efa0c
11 changed files with 82 additions and 80 deletions

0
prototype/gradlew vendored Executable file → Normal file
View File

View File

@ -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";

View File

@ -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) {

View File

@ -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<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";
}
}

View File

@ -27,4 +27,11 @@ public interface ArticleRepository extends JpaRepository<Article, Long> {
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);
}

View File

@ -14,4 +14,6 @@ public interface CategoryRepository extends JpaRepository<Category, Long> {
@Query("SELECT a FROM Category a WHERE a.name = :name")
Optional<Category> findCategoryByName(@Param("name") String name);
}
@Query("SELECT c FROM Category c")
List<Category> getCategories();
}

View File

@ -11,7 +11,7 @@
<div class='content-width bar-flex'>
<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">
<input type="text" placeholder="Nach Produkten suchen..."/>
<input type="text" name="term" placeholder="Nach Produkten suchen..."/>
<button>Finden</button>
</form>
<a th:unless="${user}" class="button" th:href="@{/login}">Login</a>

View File

@ -7,15 +7,10 @@
</head>
<body>
<nav th:fragment="sidebar">
<nav th:fragment="sidebar (categories)">
<h1>Kategorien</h1>
<ul class="secondary">
<li><a href="/shop/search">Aufnahmegeräte</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>
<li th:each="category: ${categories}"><a th:href="@{/shop/search(category=${category.name})}" th:text="${category.name}"></a></li>
</ul>
</nav>
</body>

View File

@ -14,7 +14,7 @@
<nav th:replace="fragments/header :: header">Header</nav>
<main class=" content-width">
<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="detailgrid">
<div class="s">

View File

@ -22,14 +22,14 @@
</div>
<div class='grid m base shadow' th:if="${commercialArticles.size() != 0}">
<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})}"/>
<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 th:text="${article.description}" />
</a>
</section>
<img th:src="@{/shop/articles/{id}/image.jpg(id=${article.id})}"/>
<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 th:text="${article.description}" />
</a>
</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-->
<div th:if="${isLoggedIn == true and hasOrders == false}">
<h1>Jetzt Shoppen und Empfehlungen erhalten!</h1>
<h2>Jetzt Shoppen und Empfehlungen erhalten!</h2>
</div>
<!-- If User is logged in and has ordered something before-->

View File

@ -21,70 +21,27 @@
</div>
</div>
<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='grid m condensed'>
<section><a th:href="@{/shop/articles/1234}" class="section">
<img th:src="@{/img/product-1.jpg}">
<h2>Tolle Kamera</h2>
<p class='price'> 25.14&nbsp;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>
<div class='grid m condensed' th:if="${articles.size() != 0}">
<section th:each="article: ${articles}">
<a th:href="@{/shop/articles/{id}(id=${article.id})}" class="section">
<section><a th:href="@{/shop/articles/1234}" class="section">
<img th:src="@{/img/product-2.jpg}">
<h2>Bluetooth Kopfhörer</h2>
<p class='price'> 10.14&nbsp;EUR</p>
<p>
Sind halt Kopfhörer ohne Kabel, mehr gibts da nich zu sagen.
</p>
</a>
<img th:src="@{/shop/articles/{id}/image.jpg(id=${article.id})}"/>
<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 th:text="${article.description}" />
</a>
</section>
<section><a th:href="@{/shop/articles/1234}" class="section">
<img th:src="@{/img/product-3.jpg}">
<h2>???</h2>
<p class='price'> 25.14&nbsp;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&nbsp;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&nbsp;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>
</div>
<div th:if="${articles.size() == 0}">
<h2>Keine Ergebnisse für diesen Suchterm gefunden!</h2>
</div>
</div>
</main>
<footer th:replace="fragments/footer :: footer"></footer>