add validation

This commit is contained in:
Anton Romanov 2018-12-04 14:13:01 +04:00
parent f9670ad0a5
commit dfe44ab842
5 changed files with 27 additions and 27 deletions

View File

@ -97,10 +97,4 @@ public class AdviceController {
public ResponseExtended<String> handleUserIsUndeadException(Throwable e) { public ResponseExtended<String> handleUserIsUndeadException(Throwable e) {
return handleException(ErrorConstants.USER_UNDEAD_ERROR, e.getMessage()); 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());
}
} }

View File

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

View File

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

View File

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

View File

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