Commit 6eaaf039 authored by Ondřej Pavlica's avatar Ondřej Pavlica
Browse files

Fix checkout exception, add product names to cart listing

parent f486f1ea
Pipeline #140881 passed with stages
in 2 minutes and 22 seconds
......@@ -6,5 +6,6 @@ import cz.muni.fi.pa165.winery.dto.order.OrderDto;
public interface CartService {
CartDto loadFromCookie();
void saveToCookie(CartDto cart);
void clear();
OrderDto convertToOrder(CartDto cart, long userId);
}
......@@ -51,7 +51,7 @@ public class DaoBaseImpl<ENTITY extends EntityBase> implements DaoBase<ENTITY> {
public ENTITY get(long id, boolean useCachedResults) {
try {
var entity = entityManager.find(entityClass, id);
if (!useCachedResults) {
if (!useCachedResults && entity != null) {
entityManager.refresh(entity);
}
return entity;
......
......@@ -53,6 +53,11 @@ public class CartServiceImpl implements CartService {
cookieService.setCookie(getCartCookieName(), json);
}
@Override
public void clear() {
cookieService.setCookie(getCartCookieName(), "{}");
}
@Override
public OrderDto convertToOrder(CartDto cart, long userId) {
var order = new OrderDto();
......
......@@ -106,19 +106,30 @@ public class CartController extends ControllerBase {
item.setBottleId(entry.getKey());
item.setQuantity(new BigDecimal(entry.getValue()));
item.setOrderId(order.getId());
item.setOrderId(order.getId());
order.getItems().add(item);
orderItemService.create(item);
cart.remove(entry.getKey());
var bottle = wineBottleService.get(item.getBottleId());
if (bottle == null) {
continue;
}
bottle.setStock(bottle.getStock() - item.getQuantity().intValue());
orderItemService.create(item);
wineBottleService.update(bottle);
}
cartService.saveToCookie(cart);
cartService.clear();
return "Success";
}
@Override
protected <T extends ViewModelBase> void initializeViewModel(T viewModel) {
super.initializeViewModel(viewModel);
if (viewModel instanceof CartListingViewModel) {
var cartViewModel = (CartListingViewModel) viewModel;
var productNames = wineBottleService.getAll().stream().collect(Collectors.toMap(x -> x.getId(), x -> x.getName()));
cartViewModel.setProductNames(productNames);
}
}
}
......@@ -112,7 +112,9 @@ public class ControllerBase {
private Pair<String, String> getControllerAndAction() {
var callerFrame = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE)
.walk(s ->
s.filter(c -> ControllerBase.class.isAssignableFrom(c.getDeclaringClass()) && c.getDeclaringClass() != ControllerBase.class)
s.filter(c -> ControllerBase.class.isAssignableFrom(c.getDeclaringClass()) &&
c.getDeclaringClass() != ControllerBase.class &&
!c.getMethodName().equals("initializeViewModel"))
.findFirst());
if (!callerFrame.isPresent()) {
......
......@@ -5,9 +5,11 @@ import lombok.Getter;
import lombok.Setter;
import java.util.List;
import java.util.Map;
@Getter
@Setter
public class CartListingViewModel extends ViewModelBase {
private List<CartItemViewModel> cart;
private Map<Long, String> productNames;
}
......@@ -12,12 +12,12 @@
<h4>Cart contents:</h4>
<table class="table">
<thead>
<th>Item ID</th>
<th>Product Name</th>
<th>Count</th>
</thead>
<tbody>
<tr th:each="item : ${model.cart}">
<td th:text="${item.productId}">ID</td>
<td th:text="${model.productNames[__${item.productId}__]}">ID</td>
<td th:text="${item.count}">Count</td>
</tr>
</tbody>
......
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