Commit 4cf88f82 authored by Samuel Dudík's avatar Samuel Dudík
Browse files

Fix some cart issues

parent 6eaaf039
Pipeline #140956 passed with stages
in 2 minutes and 35 seconds
......@@ -63,6 +63,9 @@ public class CartController extends ControllerBase {
@PostMapping("/add")
@ResponseBody
public String add(long id, int count) {
if (count < 1)
return "Fail";
var cart = cartService.loadFromCookie();
cart.add(id, count);
cartService.saveToCookie(cart);
......@@ -90,6 +93,19 @@ public class CartController extends ControllerBase {
@PostMapping("/checkout")
@ResponseBody
public String add() {
var cart = cartService.loadFromCookie();
var cartItems = cart.getCartItems();
for (Map.Entry<Long, Integer> entry : cartItems.entrySet()) {
var bottle = wineBottleService.get(entry.getKey());
if (bottle == null) {
continue;
}
if (bottle.getStock() < entry.getValue())
return "Fail";
}
var order = new OrderDto();
order.setShipped(false);
......@@ -98,9 +114,6 @@ public class CartController extends ControllerBase {
orderService.create(order);
var cart = cartService.loadFromCookie();
var cartItems = cart.getCartItems();
for (Map.Entry<Long, Integer> entry : cartItems.entrySet()) {
var item = new OrderItemDto();
item.setBottleId(entry.getKey());
......
......@@ -14,11 +14,13 @@
<thead>
<th>Product Name</th>
<th>Count</th>
<th>Delete</th>
</thead>
<tbody>
<tr th:each="item : ${model.cart}">
<td th:text="${model.productNames[__${item.productId}__]}">ID</td>
<td th:text="${item.count}">Count</td>
<td th:text="${model.productNames[__${item.productId}__]}">Product Name</td>
<td><input th:onchange="'setCartAmount(' + ${item.productId} + ', document.getElementById(\'quantity\').value)'" type="number" id="quantity" name="quantity" min="1" th:value="${item.count}"/></td>
<td><a href="#" th:onclick="'removeFromCart(' + ${item.productId} + ').then(x => refreshPage())'">Delete</a></td>
</tr>
</tbody>
</table>
......
......@@ -43,6 +43,7 @@
<h3>Items</h3>
<div th:each="entry, stat : *{items}">
<span th:text="*{wineNames[__${entry.bottleId}__]}"/>
// TODO max = 5
<input type="number" id="quantity" name="quantity" min="1" max="5" th:field="*{quantities[__${entry.id}__]}">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
......
......@@ -42,7 +42,7 @@
</table>
<div class="mb-2">
<input id="itemCount" type="number" value="1"/>
<input id="itemCount" type="number" min="1" value="1"/>
<a class="btn btn-primary btn-sm" href="#" th:onclick="'addToCart(' + ${model.id} + ', document.getElementById(\'itemCount\').value).then(x => document.getElementById(\'success-alert\').style.display = \'block\')'">Add to cart</a>
</div>
......
Supports Markdown
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