Added additional validation. Small refactoring.

This commit is contained in:
Alexey Zinchenko 2019-05-10 00:02:52 +03:00
parent fab69122bb
commit db9b92fa1a
6 changed files with 72 additions and 27 deletions

View File

@ -3,8 +3,6 @@ package com.github.prominence.carrepair.controller;
import com.github.prominence.carrepair.enums.OrderStatus; import com.github.prominence.carrepair.enums.OrderStatus;
import com.github.prominence.carrepair.model.Order; import com.github.prominence.carrepair.model.Order;
import com.github.prominence.carrepair.repository.spec.OrderSpecifications; import com.github.prominence.carrepair.repository.spec.OrderSpecifications;
import com.github.prominence.carrepair.service.ClientService;
import com.github.prominence.carrepair.service.MechanicService;
import com.github.prominence.carrepair.service.OrderService; import com.github.prominence.carrepair.service.OrderService;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
@ -14,7 +12,6 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.ui.ModelMap; import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.validation.SmartValidator;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.Optional; import java.util.Optional;
@ -24,15 +21,9 @@ import java.util.Optional;
public class OrderController { public class OrderController {
private OrderService orderService; private OrderService orderService;
private ClientService clientService;
private MechanicService mechanicService;
private SmartValidator smartValidator;
public OrderController(OrderService orderService, ClientService clientService, MechanicService mechanicService, SmartValidator smartValidator) { public OrderController(OrderService orderService) {
this.orderService = orderService; this.orderService = orderService;
this.clientService = clientService;
this.mechanicService = mechanicService;
this.smartValidator = smartValidator;
} }
@GetMapping @GetMapping
@ -50,8 +41,6 @@ public class OrderController {
@GetMapping(value = "/create") @GetMapping(value = "/create")
public String create(Model model) { public String create(Model model) {
model.addAttribute("order", new Order()); model.addAttribute("order", new Order());
model.addAttribute("clientIdsList", clientService.getAllClientIds());
model.addAttribute("mechanicIdsList", mechanicService.getAllMechanicIds());
model.addAttribute("orderStatuses", OrderStatus.values()); model.addAttribute("orderStatuses", OrderStatus.values());
return "order/edit"; return "order/edit";
@ -72,13 +61,7 @@ public class OrderController {
@PostMapping(value = "/update/{id}") @PostMapping(value = "/update/{id}")
public String update(Order order, BindingResult bindingResult, @PathVariable long id, Long clientId, Long mechanicId, Model model) { public String update(Order order, BindingResult bindingResult, @PathVariable long id, Long clientId, Long mechanicId, Model model) {
if (clientId != null) { orderService.fetchNestedObjectsAndValidate(order, clientId, mechanicId, bindingResult);
clientService.findById(clientId).ifPresent(order::setClient);
}
if (mechanicId != null) {
mechanicService.findById(mechanicId).ifPresent(order::setMechanic);
}
smartValidator.validate(order, bindingResult);
if (bindingResult.hasErrors()) { if (bindingResult.hasErrors()) {
order.setId(id); // why should we do this? order.setId(id); // why should we do this?
model.addAttribute("orderStatuses", OrderStatus.values()); model.addAttribute("orderStatuses", OrderStatus.values());
@ -91,13 +74,7 @@ public class OrderController {
@PostMapping(value = "/create") @PostMapping(value = "/create")
public String save(Order order, BindingResult bindingResult, Long clientId, Long mechanicId, Model model) { public String save(Order order, BindingResult bindingResult, Long clientId, Long mechanicId, Model model) {
if (clientId != null) { orderService.fetchNestedObjectsAndValidate(order, clientId, mechanicId, bindingResult);
clientService.findById(clientId).ifPresent(order::setClient);
}
if (mechanicId != null) {
mechanicService.findById(mechanicId).ifPresent(order::setMechanic);
}
smartValidator.validate(order, bindingResult);
if (bindingResult.hasErrors()) { if (bindingResult.hasErrors()) {
model.addAttribute("orderStatuses", OrderStatus.values()); model.addAttribute("orderStatuses", OrderStatus.values());
return "order/edit"; return "order/edit";

View File

@ -2,10 +2,13 @@ package com.github.prominence.carrepair.service;
import com.github.prominence.carrepair.model.Order; import com.github.prominence.carrepair.model.Order;
import com.github.prominence.carrepair.repository.OrderRepository; import com.github.prominence.carrepair.repository.OrderRepository;
import com.github.prominence.carrepair.validation.OrderValidator;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import org.springframework.validation.SmartValidator;
import java.util.Optional; import java.util.Optional;
@ -14,8 +17,17 @@ public class OrderService {
private OrderRepository orderRepository; private OrderRepository orderRepository;
public OrderService(OrderRepository orderRepository) { private ClientService clientService;
private MechanicService mechanicService;
private SmartValidator smartValidator;
private OrderValidator orderValidator;
public OrderService(OrderRepository orderRepository, ClientService clientService, MechanicService mechanicService, SmartValidator smartValidator, OrderValidator orderValidator) {
this.orderRepository = orderRepository; this.orderRepository = orderRepository;
this.clientService = clientService;
this.mechanicService = mechanicService;
this.smartValidator = smartValidator;
this.orderValidator = orderValidator;
} }
public Page<Order> findAll(Pageable pageable) { public Page<Order> findAll(Pageable pageable) {
@ -46,4 +58,15 @@ public class OrderService {
public long getOrderCount() { public long getOrderCount() {
return orderRepository.count(); return orderRepository.count();
} }
public void fetchNestedObjectsAndValidate(Order order, Long clientId, Long mechanicId, BindingResult bindingResult) {
if (clientId != null) {
clientService.findById(clientId).ifPresent(order::setClient);
}
if (mechanicId != null) {
mechanicService.findById(mechanicId).ifPresent(order::setMechanic);
}
smartValidator.validate(order, bindingResult);
orderValidator.validate(order, bindingResult);
}
} }

View File

@ -0,0 +1,33 @@
package com.github.prominence.carrepair.validation;
import com.github.prominence.carrepair.enums.OrderStatus;
import com.github.prominence.carrepair.model.Order;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import java.time.LocalDateTime;
@Component
public class OrderValidator implements Validator {
@Override
public boolean supports(Class<?> aClass) {
return Order.class.isAssignableFrom(aClass);
}
@Override
public void validate(Object o, Errors errors) {
Order order = (Order) o;
LocalDateTime finishedOn = order.getFinishedOn();
if (finishedOn != null) {
LocalDateTime startedOn = order.getCreatedOn();
if (startedOn != null && order.getFinishedOn().isBefore(order.getCreatedOn())) {
errors.rejectValue("finishedOn", "error.finishedOn.finishedBeforeStarted");
}
if (order.getOrderStatus() != OrderStatus.ACCEPTED) {
errors.rejectValue("finishedOn", "error.finishedOn.incompatibleStatus");
}
}
}
}

View File

@ -39,3 +39,7 @@ common.search.button = Search
common.nothingFound.label = Nothing was found. common.nothingFound.label = Nothing was found.
common.deleteConfirmation.label = Are you sure to delete? common.deleteConfirmation.label = Are you sure to delete?
# validation
error.finishedOn.finishedBeforeStarted = value must be after starting date
error.finishedOn.incompatibleStatus = order cannot be finished until it isn't accepted by client

View File

@ -39,3 +39,7 @@ common.search.button = Search
common.nothingFound.label = Nothing was found. common.nothingFound.label = Nothing was found.
common.deleteConfirmation.label = Are you sure to delete? common.deleteConfirmation.label = Are you sure to delete?
# validation
error.finishedOn.finishedBeforeStarted = value must be after starting date
error.finishedOn.incompatibleStatus = order cannot be finished until it isn't accepted by client

View File

@ -39,3 +39,7 @@ common.search.button = Поиск
common.nothingFound.label = Ничего не найдено. common.nothingFound.label = Ничего не найдено.
common.deleteConfirmation.label = Вы действительно хотите удалить? common.deleteConfirmation.label = Вы действительно хотите удалить?
# validation
error.finishedOn.finishedBeforeStarted = дата окончания должна быть после даты начала
error.finishedOn.incompatibleStatus = заказ не может быть закончен если клиент еще не принял его