Commit 6b6a4b2b authored by Michaela Korenková's avatar Michaela Korenková
Browse files

Merge branch 'feature/mvc/korenkova' into 'main'

ProductReview

See merge request !43
parents f486f1ea 8343196a
Pipeline #141056 passed with stages
in 2 minutes and 45 seconds
......@@ -115,11 +115,21 @@ public class ProductReviewController extends ControllerBase{
return view(bindingResult.getModel());
}
var dto = viewModel.toDto();
productReviewService.create(dto);
var dto = viewModel.toDto(getCurrentUser().getId());
var reviews = productReviewService.getAllByBottle(dto.getWineBottleId());
List<Long> users = reviews
.stream()
.map(x -> x.getUserId())
.distinct()
.collect(Collectors.toList());
if (!users.contains(dto.getUserId())){
productReviewService.create(dto);
viewModel.setSuccess(true);
} else {
viewModel.setExists(true);
}
initializeViewModel(viewModel);
viewModel.setSuccess(true);
return view(viewModel);
}
......
......@@ -32,6 +32,8 @@ public class ProductReviewUpsertViewModel extends ViewModelBase {
private boolean success;
private boolean exists;
/**
* grade of wine
*/
......@@ -66,9 +68,10 @@ public class ProductReviewUpsertViewModel extends ViewModelBase {
private List<UserDto> users;
public ProductReviewDto toDto() {
public ProductReviewDto toDto(long userId) {
var mapper = new ModelMapper();
return mapper.map(this, ProductReviewDto.class);
var dto = mapper.map(this, ProductReviewDto.class);
dto.setUserId(userId);
return dto;
}
}
......@@ -18,7 +18,14 @@
</div>
</div>
<div th:if="${model.exists}">
<div class="alert alert-danger" role="alert">
You already created review for this product!
</div>
</div>
<form th:action="@{/productreview/add}" th:object="${model}" method="post">
<input type="hidden" th:field="*{UserId}">
<div class="form-group">
<label th:for="*{wineBottleId}" th:value="*{wineBottleId}">Bottle</label>
<select class="form-control" th:field="*{wineBottleId}">
......@@ -26,13 +33,6 @@
</select>
<p class="text-danger" th:each="error : ${#fields.errors('wineBottleId')}" th:text="${error}"></p>
</div>
<div class="form-group">
<label th:for="*{userId}" th:value="*{userId}">User</label>
<select class="form-control" th:field="*{userId}">
<option th:each="user : *{users}" th:value="${user.id}" th:text="${user.firstName}"></option>
</select>
<p class="text-danger" th:each="error : ${#fields.errors('userId')}" th:text="${error}"></p>
</div>
<div class="form-group">
<label th:for="*{grade}" th:value="*{grade}">Grade</label>
<input class="form-control" type="number" th:field="*{grade}">
......
......@@ -22,7 +22,7 @@
<th scope="col">Price</th>
<th scope="col">On stock</th>
<th scope="col">Grape variety</th>
<th scope="col">Actions</th>
<th th:if='${model.currentUser != null && model.currentUser.hasRole("ADMIN")}' scope="col">Actions</th>
</tr>
</thead>
<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