add validation

pull/149/head
Anton Romanov 6 years ago
parent f9670ad0a5
commit dfe44ab842

@ -97,10 +97,4 @@ public class AdviceController {
public ResponseExtended<String> handleUserIsUndeadException(Throwable e) {
return handleException(ErrorConstants.USER_UNDEAD_ERROR, e.getMessage());
}
@ExceptionHandler(Exception.class)
public ResponseExtended<String> handleUnknownException(Throwable e) {
e.printStackTrace();
return handleException(ErrorConstants.UNKNOWN, e.getMessage());
}
}

@ -1,19 +1,17 @@
package ru.ulstu.paper.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import ru.ulstu.core.model.response.Response;
import ru.ulstu.paper.model.PaperDto;
import ru.ulstu.paper.model.PaperFilterDto;
@ -40,25 +38,23 @@ public class PaperController {
}
@GetMapping("/paper")
public void getPapers(ModelMap modelMap, @RequestParam(value="id") Integer id) {
public void getPapers(ModelMap modelMap, @RequestParam(value = "id") Integer id) {
if (id != null && id > 0) {
modelMap.put("paper", paperService.findOneDto(id));
modelMap.put("paperDto", paperService.findOneDto(id));
} else {
modelMap.put("paper", new PaperDto());
modelMap.put("paperDto", new PaperDto());
}
}
@PostMapping("/paper")
public String createPaper(@ModelAttribute PaperDto paperDto) throws IOException {
paperService.create(paperDto);
public String save(@Valid PaperDto paperDto, Errors errors) throws IOException {
if (errors.hasErrors()) {
return "/papers/paper";
}
paperService.save(paperDto);
return "redirect:/papers/papers";
}
@PutMapping
public Response<Integer> updatePaper(@RequestBody @Valid PaperDto paperDto) throws IOException {
return new Response<>(paperService.update(paperDto));
}
@DeleteMapping("/{paper-id}")
public Response<Boolean> delete(@PathVariable("paper-id") Integer paperId) throws IOException {
paperService.delete(paperId);

@ -4,7 +4,9 @@ import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.format.annotation.DateTimeFormat;
import ru.ulstu.user.model.UserDto;
import javax.validation.constraints.Future;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Date;
import java.util.Set;
import java.util.stream.Collectors;
@ -14,6 +16,7 @@ import static ru.ulstu.core.util.StreamApiUtils.convert;
public class PaperDto {
private Integer id;
@NotEmpty
@Size(min = 3, max = 100)
private String title;
private Paper.PaperStatus status;
private Date createDate;

@ -17,6 +17,7 @@ import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import static org.springframework.util.ObjectUtils.isEmpty;
import static ru.ulstu.core.util.StreamApiUtils.convert;
import static ru.ulstu.paper.model.Paper.PaperStatus.ATTENTION;
import static ru.ulstu.paper.model.Paper.PaperStatus.DRAFT;
@ -150,4 +151,12 @@ public class PaperService {
paperNotificationService.sendFailedNotification(paper, oldStatus);
});
}
public void save(PaperDto paperDto) throws IOException {
if (isEmpty(paperDto.getId())) {
create(paperDto);
} else {
update(paperDto);
}
}
}

@ -26,17 +26,16 @@
<div class="row">
<div class="col-lg-12">
<form method="post" action="#" th:action="@{/papers/paper}" th:object="${paper}">
<form method="post" th:action="@{'/papers/paper?id='+*{id}}" th:object="${paperDto}">
<div class="row">
<div class="col-md-6 col-sm-12">
<div class="form-group">
<label for="title">Название:</label>
<input type="hidden" name="id" th:field="*{id}"/>
<input class="form-control" id="title" type="text"
placeholder="Название статьи"
required=""
data-validation-required-message="Введите название статьи"
th:field="*{title}"/>
<p th:if="${#fields.hasErrors('title')}" th:errors="*{title}" class="alert alert-danger">Incorrect title</p>
<p class="help-block text-danger"></p>
</div>
@ -55,9 +54,8 @@
<div class="form-group">
<label>Дедлайн:</label>
<input type="date" class="form-control" name="deadline"
th:field="*{deadlineDate}"
/>
<input type="date" class="form-control" name="deadline" th:field="*{deadlineDate}"/>
<p th:if="${#fields.hasErrors('deadlineDate')}" th:errors="*{deadlineDate}" class="alert alert-danger">Incorrect title</p>
</div>
<div class="form-check">

Loading…
Cancel
Save