Commit f7b06b21 authored by issei.miyajima's avatar issei.miyajima

検索機能

parent 9eeaf4ab
......@@ -4,6 +4,7 @@ import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import com.example.domain.Shoes;
......@@ -12,4 +13,12 @@ import com.example.domain.Shoes;
public interface ShoesRepository extends JpaRepository<Shoes, Integer>{
@Query(value = "SELECT * FROM shoes WHERE product_status = ?1 ORDER BY id DESC", nativeQuery = true)
public List <Shoes> findAllByStatus(Integer status);
//商品名あいまい検索
@Query(value = "SELECT * FROM shoes WHERE name LIKE %:keyword% ORDER BY id DESC", nativeQuery = true)
public List <Shoes> findAllForSearch(@Param("keyword") String keyword);
//サイズ検索
@Query(value = "SELECT * FROM shoes WHERE size =?1 ORDER BY id DESC", nativeQuery = true)
public List <Shoes> findAllBySize(Integer size);
}
......@@ -49,4 +49,15 @@ public class ShoesService {
public List<Shoes> findAllLimitedShoes() {
return shoesRepository.findAllByStatus(1);
}
//名前検索用
public List<Shoes> findAllForSearch(String shoesName){
return shoesRepository.findAllForSearch(shoesName);
}
//サイズ検索用
public List<Shoes> findAllBySize(Integer shoesSize){
return shoesRepository.findAllBySize(shoesSize);
}
}
......@@ -13,12 +13,14 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.example.domain.Items;
import com.example.domain.SalesLog;
......@@ -126,6 +128,32 @@ public class EcsiteController {
return "list";
}
//名前検索用
@PostMapping("/search")
public String search(@RequestParam String shoesName, Model model, @AuthenticationPrincipal LoginUser userDetails){
//user情報取得、格納
User user = userDetails.getUser();
model.addAttribute("user", user);
//検索に当てはまる靴取得
List<Shoes> searchShoes = shoesService.findAllForSearch(shoesName);
model.addAttribute("searchShoes", searchShoes);
return "list";
}
//サイズ検索用
public String searchBySize(@RequestParam Integer shoesSize, Model model, @AuthenticationPrincipal LoginUser userDetails){
//user情報取得、格納
User user = userDetails.getUser();
model.addAttribute("user", user);
//検索に当てはまる靴取得
List<Shoes> searchShoes = shoesService.findAllBySize(shoesSize);
model.addAttribute("searchShoes", searchShoes);
return "list";
}
//購入履歴画面
@GetMapping("log/{id}")
public String history (@PathVariable Integer id, Model model, SalesLog salesLog, @AuthenticationPrincipal LoginUser userDetails) {
......
......@@ -114,10 +114,39 @@
</div><!-- end of/. row -->
</div><!-- end of /.container -->
</section><!-- end of /.news letter section -->
<!-- 検索 -->
<form method="post" name="form1" th:action="@{/limited/search}">
<table>
<tr>
<td>Search By Name<input type="text" class="form-control" id="shoes_name" name="shoesName" /></td>
<td><input type="submit" value="search"/></td>
</tr>
</table>
</form>
<!--一覧-->
<p id="cart-p-text"><span id="cart-text">Product List</span></p>
<div class="listBox" id="limitedListBox">
<!-- 検索された商品一覧 -->
<div class="listBox">
<div class="productBox" th:each="searchShoes : ${searchShoes}">
<div class="shoeImageDiv">
<img class="shoeImage"
th:src="@{/upload/{photo}(photo=${searchShoes.photo})}"
alt="productImage" width="300" height="200" />
</div>
<p th:text="${searchShoes.name}" class="shoeName">AirMAX</p>
<p th:text="${searchShoes.size} + ' cm'" class="shoeSize"></p>
<p th:text="'&yen;' +${searchShoes.price}" class="shoePrice">¥10,000</p>
<input type="hidden" th:value="${searchShoes.id}" /> <input
type="hidden" th:value="${searchShoes.stock}" />
<div class="detailsBtn">
<button type="button" id="modal-open" class="details">Datails</button>
</div>
</div>
</div>
<!--限定商品一覧-->
<div class="listBox" id="limitedListBox">
<div class="productBox" th:each="limitedShoes : ${limitedShoes}">
<div class="shoeImageDiv"><img class="shoeImage" th:src="@{/upload/{photo}(photo=${limitedShoes.photo})}" alt="productImage" width="300" height="200"/></div>
<p th:text= "${limitedShoes.name}" class="shoeName shoeLimited">AirMAX</p>
......@@ -129,8 +158,8 @@
<!-- <button class="js-modal-open" id="sign-up">Details</button>-->
</div>
</div>
<div class="listBox">
<!--一般商品一覧-->
<div class="listBox">
<div class="productBox" th:each="generalShoes : ${generalShoes}">
<div class="shoeImageDiv"><img class="shoeImage" th:src="@{/upload/{photo}(photo=${generalShoes.photo})}" alt="productImage" width="300" height="200"/></div>
<p th:text= "${generalShoes.name}" class="shoeName">AirMAX</p>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment