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{ ...@@ -115,11 +115,21 @@ public class ProductReviewController extends ControllerBase{
return view(bindingResult.getModel()); return view(bindingResult.getModel());
} }
var dto = viewModel.toDto(); var dto = viewModel.toDto(getCurrentUser().getId());
productReviewService.create(dto); 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); initializeViewModel(viewModel);
viewModel.setSuccess(true);
return view(viewModel); return view(viewModel);
} }
......
...@@ -32,6 +32,8 @@ public class ProductReviewUpsertViewModel extends ViewModelBase { ...@@ -32,6 +32,8 @@ public class ProductReviewUpsertViewModel extends ViewModelBase {
private boolean success; private boolean success;
private boolean exists;
/** /**
* grade of wine * grade of wine
*/ */
...@@ -66,9 +68,10 @@ public class ProductReviewUpsertViewModel extends ViewModelBase { ...@@ -66,9 +68,10 @@ public class ProductReviewUpsertViewModel extends ViewModelBase {
private List<UserDto> users; private List<UserDto> users;
public ProductReviewDto toDto() { public ProductReviewDto toDto(long userId) {
var mapper = new ModelMapper(); 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 @@ ...@@ -18,7 +18,14 @@
</div> </div>
</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"> <form th:action="@{/productreview/add}" th:object="${model}" method="post">
<input type="hidden" th:field="*{UserId}">
<div class="form-group"> <div class="form-group">
<label th:for="*{wineBottleId}" th:value="*{wineBottleId}">Bottle</label> <label th:for="*{wineBottleId}" th:value="*{wineBottleId}">Bottle</label>
<select class="form-control" th:field="*{wineBottleId}"> <select class="form-control" th:field="*{wineBottleId}">
...@@ -26,13 +33,6 @@ ...@@ -26,13 +33,6 @@
</select> </select>
<p class="text-danger" th:each="error : ${#fields.errors('wineBottleId')}" th:text="${error}"></p> <p class="text-danger" th:each="error : ${#fields.errors('wineBottleId')}" th:text="${error}"></p>
</div> </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"> <div class="form-group">
<label th:for="*{grade}" th:value="*{grade}">Grade</label> <label th:for="*{grade}" th:value="*{grade}">Grade</label>
<input class="form-control" type="number" th:field="*{grade}"> <input class="form-control" type="number" th:field="*{grade}">
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
<th scope="col">Price</th> <th scope="col">Price</th>
<th scope="col">On stock</th> <th scope="col">On stock</th>
<th scope="col">Grape variety</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> </tr>
</thead> </thead>
<tbody> <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