This repository has been archived on 2020-08-02. You can view files and clone it, but cannot push or open issues or pull requests.
e-commerce/web_backend/src/main/java/org/hso/ecommerce/controller/shop/ShopSearchController.java

54 lines
2.2 KiB
Java
Raw Normal View History

2020-04-29 22:44:16 +02:00
package org.hso.ecommerce.controller.shop;
2020-04-28 22:41:29 +02:00
2020-05-29 11:06:00 +02:00
import org.hso.ecommerce.action.shop.SearchByTermAction;
2020-05-14 15:43:16 +02:00
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;
2020-04-28 22:41:29 +02:00
import org.springframework.stereotype.Controller;
2020-05-14 15:43:16 +02:00
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
2020-05-14 15:43:16 +02:00
2020-05-15 12:23:32 +02:00
import javax.servlet.http.HttpServletRequest;
2020-05-14 15:43:16 +02:00
import javax.servlet.http.HttpServletResponse;
import java.util.List;
2020-04-28 22:41:29 +02:00
@Controller
2020-05-14 15:43:16 +02:00
@RequestMapping("/shop/search")
2020-05-01 10:48:12 +02:00
public class ShopSearchController {
2020-05-14 15:43:16 +02:00
@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,
2020-05-15 12:23:32 +02:00
Model model,
HttpServletRequest request,
HttpServletResponse response
2020-05-14 15:43:16 +02:00
) {
2020-05-15 12:23:32 +02:00
model.addAttribute("categories", categoryRepository.getCategories()); //for sidebar
2020-05-14 15:43:16 +02:00
if (term != null) { //if search by Term
2020-06-05 12:32:34 +02:00
term = term.trim();
2020-05-29 11:06:00 +02:00
List<Article> articles = SearchByTermAction.searchByTerm(term, articleRepository);
2020-05-14 15:43:16 +02:00
model.addAttribute("articles", articles);
} else if (category != null) { //if search by Category
2020-05-17 12:59:03 +02:00
List<Article> articles = articleRepository.getArticlesByCategory(category); //search by Category
2020-05-14 15:43:16 +02:00
model.addAttribute("articles", articles);
2020-05-15 12:23:32 +02:00
} else {
List<Article> articles = SearchByTermAction.searchByTerm("", articleRepository);
model.addAttribute("articles", articles);
2020-05-14 15:43:16 +02:00
}
// Show term in search box
model.addAttribute("searchterm", term != null ? term : "");
return "shop/search";
2020-05-14 15:43:16 +02:00
}
}