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

Fixes

parent 4187d534
......@@ -31,7 +31,7 @@ public class WineBottle extends EntityBase{
*/
@Column(nullable = false)
@NotNull
@Pattern(regexp = "^[\\w0-9]*$", message = "Only alphanumerical characters")
@Pattern(regexp = "^[\\w0-9 ]*$", message = "Only alphanumerical characters")
private String name;
/**
......@@ -46,7 +46,7 @@ public class WineBottle extends EntityBase{
* attribute of type BigDecimal representing price
*/
@Column(nullable = false)
@Positive
@PositiveOrZero
private BigDecimal price;
/**
......@@ -54,7 +54,7 @@ public class WineBottle extends EntityBase{
*/
@Column(nullable = false)
@NotNull
@Positive
@PositiveOrZero
private BigDecimal volume;
/**
......
......@@ -21,6 +21,7 @@ import org.springframework.web.servlet.ModelAndView;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.stream.Collectors;
......@@ -99,16 +100,25 @@ public class CartController extends ControllerBase {
@ResponseBody
public String add() {
var cart = cartService.loadFromCookie();
var cartItems = cart.getCartItems();
var cartItems = new HashMap<>(cart.getCartItems());
for (Map.Entry<Long, Integer> entry : cartItems.entrySet().stream().collect(Collectors.toList())) {
if (entry.getValue() < 0) {
throw new ResponseStatusException(GONE, "Cannot order a negative number of items.");
}
if (entry.getValue() == 0) {
cartItems.remove(entry.getKey());
continue;
}
for (Map.Entry<Long, Integer> entry : cartItems.entrySet()) {
var bottle = wineBottleService.get(entry.getKey());
if (bottle == null) {
continue;
}
if (bottle.getStock() < entry.getValue())
if (bottle.getStock() < entry.getValue()) {
throw new ResponseStatusException(GONE, "Not enough products on stock.");
}
}
var order = new OrderDto();
......
......@@ -7,6 +7,7 @@ import lombok.NoArgsConstructor;
import lombok.Setter;
import org.modelmapper.ModelMapper;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
/**
......@@ -29,7 +30,7 @@ public class GrapeUpsertViewModel extends ViewModelBase {
/**
* name of grape variety
*/
@NotNull(message = "Cannot be empty")
@NotBlank(message = "Cannot be empty")
private String name;
public GrapeDto toDto() {
......
......@@ -13,10 +13,7 @@ import one.util.streamex.StreamEx;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.PositiveOrZero;
import javax.validation.constraints.*;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashSet;
......@@ -52,19 +49,19 @@ public class OrdersUpsertViewModel extends ViewModelBase {
/**
* The order creation timestamp
*/
@NotNull
@NotNull(message = "Cannot be empty")
private LocalDateTime dateTime;
/**
* Whether the order is already shipped
*/
@NotNull
@NotNull(message = "Cannot be empty")
private boolean shipped;
/**
* The order creator's user ID
*/
@NotNull
@Positive(message = "Cannot be empty")
private long userId;
/**
......
......@@ -30,20 +30,20 @@ public class UserInsertViewModel extends ViewModelBase {
/**
* User's first name (forename).
*/
@NotNull(message = "Cannot be empty")
@NotBlank(message = "Cannot be empty")
private String firstName;
/**
* User's last name (surname)
*/
@NotNull(message = "Cannot be empty")
@NotBlank(message = "Cannot be empty")
private String lastName;
/**
* User's email address
*/
@NotNull(message = "Cannot be empty")
@Email
@NotBlank(message = "Cannot be empty")
@Email(message = "Not a valid email format")
private String email;
/**
......@@ -51,7 +51,7 @@ public class UserInsertViewModel extends ViewModelBase {
*
* Not stored directly
*/
@NotBlank
@NotBlank(message = "Cannot be empty")
private String password;
public UserDto toDto(PasswordEncoder encoder) {
......
......@@ -11,6 +11,7 @@ import org.modelmapper.convention.MatchingStrategies;
import org.springframework.security.crypto.password.PasswordEncoder;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
/**
......@@ -43,20 +44,20 @@ public class UserUpdateViewModel extends ViewModelBase {
/**
* User's first name (forename).
*/
@NotNull(message = "Cannot be empty")
@NotBlank(message = "Cannot be empty")
private String firstName;
/**
* User's last name (surname)
*/
@NotNull(message = "Cannot be empty")
@NotBlank(message = "Cannot be empty")
private String lastName;
/**
* User's email address
*/
@NotNull(message = "Cannot be empty")
@Email
@NotBlank(message = "Cannot be empty")
@Email(message = "Not a valid email format")
private String email;
/**
......
......@@ -11,6 +11,7 @@ import org.modelmapper.ModelMapper;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.PositiveOrZero;
import java.math.BigDecimal;
import java.util.List;
......@@ -37,6 +38,7 @@ public class WineBottleUpsertViewModel extends ViewModelBase {
* name of wine bottle
*/
@NotNull(message = "Cannot be empty")
@Pattern(regexp = "^[\\w0-9 ]*$", message = "Only alphanumerical characters")
private String name;
/**
......
......@@ -9,6 +9,7 @@ import lombok.Setter;
import org.modelmapper.ModelMapper;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;
......@@ -32,7 +33,7 @@ public class WineTypeUpsertViewModel extends ViewModelBase {
/**
* name of wine type
*/
@NotNull(message = "Cannot be empty")
@NotBlank(message = "Cannot be empty")
private String name;
/**
......
......@@ -26,7 +26,7 @@
<tbody>
<tr th:each="item : ${model.cart}">
<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><input th:onchange="'setCartAmount(' + ${item.productId} + ', document.getElementById(\'quantity' + ${item.productId} + '\').value)'" type="number" th:id="'quantity' + ${item.productId}" name="quantity" th:value="${item.count}"/></td>
<td><a href="#" th:onclick="'removeFromCart(' + ${item.productId} + ').then(x => refreshPage())'">Delete</a></td>
</tr>
</tbody>
......
......@@ -37,7 +37,7 @@
Username
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" th:href="@{/myorder}">Orders</a>
<a class="dropdown-item" th:href="@{/myorder}">My Orders</a>
<a class="dropdown-item" th:href="@{/cart}">Cart</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" th:href="@{/logout}">Logout</a>
......
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