diff --git a/src/main/java/ru/ulstu/core/controller/AdviceController.java b/src/main/java/ru/ulstu/core/controller/AdviceController.java index 2fe853e..18c25dc 100644 --- a/src/main/java/ru/ulstu/core/controller/AdviceController.java +++ b/src/main/java/ru/ulstu/core/controller/AdviceController.java @@ -97,10 +97,4 @@ public class AdviceController { public ResponseExtended handleUserIsUndeadException(Throwable e) { return handleException(ErrorConstants.USER_UNDEAD_ERROR, e.getMessage()); } - - @ExceptionHandler(Exception.class) - public ResponseExtended handleUnknownException(Throwable e) { - e.printStackTrace(); - return handleException(ErrorConstants.UNKNOWN, e.getMessage()); - } } diff --git a/src/main/java/ru/ulstu/paper/controller/PaperController.java b/src/main/java/ru/ulstu/paper/controller/PaperController.java index 360d9d9..740d0d6 100644 --- a/src/main/java/ru/ulstu/paper/controller/PaperController.java +++ b/src/main/java/ru/ulstu/paper/controller/PaperController.java @@ -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 updatePaper(@RequestBody @Valid PaperDto paperDto) throws IOException { - return new Response<>(paperService.update(paperDto)); - } - @DeleteMapping("/{paper-id}") public Response delete(@PathVariable("paper-id") Integer paperId) throws IOException { paperService.delete(paperId); diff --git a/src/main/java/ru/ulstu/paper/model/PaperDto.java b/src/main/java/ru/ulstu/paper/model/PaperDto.java index 2ab9284..7ea239d 100644 --- a/src/main/java/ru/ulstu/paper/model/PaperDto.java +++ b/src/main/java/ru/ulstu/paper/model/PaperDto.java @@ -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; diff --git a/src/main/java/ru/ulstu/paper/service/PaperService.java b/src/main/java/ru/ulstu/paper/service/PaperService.java index a9a199e..99b5d4f 100644 --- a/src/main/java/ru/ulstu/paper/service/PaperService.java +++ b/src/main/java/ru/ulstu/paper/service/PaperService.java @@ -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); + } + } } diff --git a/src/main/resources/templates/papers/paper.html b/src/main/resources/templates/papers/paper.html index 1e65963..bf4886d 100644 --- a/src/main/resources/templates/papers/paper.html +++ b/src/main/resources/templates/papers/paper.html @@ -26,17 +26,16 @@
-
+
- + +

Incorrect title

@@ -55,9 +54,8 @@
- + +

Incorrect title