I'm making pet app(shop) and i'm confused about one thing - i don't understand how to add product to shopping cart. I have 2 tables - products and cart, they have same fields. Here is my code with Thymeleaf:
<tbody>
<tr th:each="products : ${products}">
<td th:text="${products.name}"></td>
<td th:text="${products.price}"></td>
<td th:text="${products.quantity}"></td>
<td th:text="${products.serial}"></td>
<td> <p><a href="/product-add" class="btn btn-primary">Add to cart</a></p>
</td>
</tr>
</tbody>
Here is my controller, what is supposed to save one row from products to cart:
@RequestMapping("product-add")
public String addToCart (Model model) {
model.addAttribute("selection", cartRepository.selection());
return "redirect:/products";
}
and query(i know that it copies all products to cart, but its only for example):
@Query(value = "insert into cart(select * from products )", nativeQuery = true)
List<Cart> selection();
My question is - how can i copy just one row(one product) in this case? How i can separate product id for adding to cart? should it be done in controller or maybe in sql query?I'm just missing something simple((
question from:
https://stackoverflow.com/questions/65853026/add-one-row-from-table-to-table 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…