48 lines
1.5 KiB
Java
48 lines
1.5 KiB
Java
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("/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";
|
|
}
|
|
|
|
} |