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

Fix order items not updating

parent 65d183bf
Pipeline #140070 failed with stages
in 1 minute and 57 seconds
......@@ -45,7 +45,9 @@ public class DaoBaseImpl<ENTITY extends EntityBase> implements DaoBase<ENTITY> {
@Override
public ENTITY get(long id) {
try {
return entityManager.find(entityClass, id);
var entity = entityManager.find(entityClass, id);
entityManager.refresh(entity);
return entity;
}
catch (Exception ex) {
throw new WineryDataAccessException(ex.getMessage(), ex);
......
......@@ -45,4 +45,14 @@ public class OrderItemEntityMapper extends EntityMapperImpl<OrderItemDto, OrderI
entity.setOrder(order);
return entity;
}
@Override
public OrderItemDto mapToDto(OrderItem orderItem) {
if (orderItem == null) {
return null;
}
var dto = super.mapToDto(orderItem);
dto.setOrderId(orderItem.getOrder().getId());
return dto;
}
}
......@@ -111,6 +111,12 @@
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
<dependency>
<groupId>one.util</groupId>
<artifactId>streamex</artifactId>
<version>0.6.5</version>
</dependency>
</dependencies>
<build>
......
package cz.muni.fi.pa165.winery.webapp.controllers;
import cz.muni.fi.pa165.winery.dto.order.OrderDto;
import cz.muni.fi.pa165.winery.services.order.OrderItemService;
import cz.muni.fi.pa165.winery.services.order.OrderService;
import cz.muni.fi.pa165.winery.services.user.UserService;
import cz.muni.fi.pa165.winery.webapp.models.orders.OrdersListingViewModel;
......@@ -21,10 +22,12 @@ import java.util.stream.Collectors;
public class OrderController extends ControllerBase {
private final OrderService orderService;
private final UserService userService;
private final OrderItemService orderItemService;
public OrderController(OrderService orderService, UserService userService) {
public OrderController(OrderService orderService, UserService userService, OrderItemService orderItemService) {
this.orderService = orderService;
this.userService = userService;
this.orderItemService = orderItemService;
}
@GetMapping("")
......@@ -85,6 +88,13 @@ public class OrderController extends ControllerBase {
var dto = viewModel.toDto();
orderService.update(dto);
var order = orderService.get(viewModel.getId(), true);
for (var item : order.getItems()) {
item.setQuantity(viewModel.getQuantities().get(item.getId()));
orderItemService.update(item);
}
initializeViewModel(viewModel);
viewModel.setSuccess(true);
......
......@@ -9,6 +9,7 @@ import cz.muni.fi.pa165.winery.webapp.models.ViewModelBase;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import one.util.streamex.StreamEx;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
......@@ -20,6 +21,7 @@ import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
......@@ -34,17 +36,13 @@ public class OrdersUpsertViewModel extends ViewModelBase {
mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
mapper.map(dto, this);
wineNames = dto.getItems()
.stream()
.map(x -> x.getWineBottle().getName())
.distinct()
.collect(Collectors.toList());
wineNames = StreamEx.of(dto.getItems())
.distinct(x -> x.getWineBottle().getId())
.toMap(x -> x.getWineBottle().getId(), x -> x.getWineBottle().getName());
quantities = dto.getItems()
.stream()
.map(x -> x.getQuantity())
.distinct()
.collect(Collectors.toList());
.collect(Collectors.toMap(x -> x.getId(), x -> x.getQuantity()));
}
private long id;
......@@ -74,9 +72,9 @@ public class OrdersUpsertViewModel extends ViewModelBase {
*/
private Set<OrderItemDto> items;
private List<String> wineNames;
private Map<Long, String> wineNames;
private List<BigDecimal> quantities;
private Map<Long, BigDecimal> quantities;
private String userName;
......
......@@ -41,9 +41,9 @@
<input id="shipped" type="checkbox" th:field="*{shipped}">
</div>
<h3>Items</h3>
<div th:each="name, stat : *{wineNames}">
<span th:text="${name}"/>
<input type="number" id="quantity" name="quantity" min="1" max="5" th:field="*{quantities[__${stat.index}__]}">
<div th:each="entry, stat : *{items}">
<span th:text="*{wineNames[__${entry.bottleId}__]}"/>
<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>
</form>
......
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