From 8f2f6adc3b788923c39c4fd7d39a307e5d4bf592 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 23 Apr 2019 23:15:22 +0400 Subject: [PATCH] #64 show global message --- .../repository/ConferenceRepository.java | 3 ++ .../conference/service/ConferenceService.java | 4 ++ .../core/controller/AdviceController.java | 5 +++ .../paper/controller/PaperController.java | 41 +++++++++--------- .../paper/controller/PaperRestController.java | 6 +-- ...{PaperFilterDto.java => PaperListDto.java} | 15 +++++-- .../ru/ulstu/paper/service/PaperService.java | 4 +- src/main/resources/public/js/papers.js | 8 +++- src/main/resources/templates/default.html | 42 +++++++++++++------ .../papers/fragments/paperLineFragment.html | 3 +- 10 files changed, 85 insertions(+), 46 deletions(-) rename src/main/java/ru/ulstu/paper/model/{PaperFilterDto.java => PaperListDto.java} (67%) diff --git a/src/main/java/ru/ulstu/conference/repository/ConferenceRepository.java b/src/main/java/ru/ulstu/conference/repository/ConferenceRepository.java index cb8a488..f217db9 100644 --- a/src/main/java/ru/ulstu/conference/repository/ConferenceRepository.java +++ b/src/main/java/ru/ulstu/conference/repository/ConferenceRepository.java @@ -16,4 +16,7 @@ public interface ConferenceRepository extends JpaRepository @Query("SELECT c FROM Conference c WHERE c.beginDate > :date") List findAllActive(@Param("date") Date date); + + @Query("SELECT case when count(c) > 0 then true else false end FROM Conference c JOIN c.papers p WHERE p.id = :paperId") + boolean isPaperAttached(@Param("paperId") Integer paperId); } diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceService.java b/src/main/java/ru/ulstu/conference/service/ConferenceService.java index b96c9cd..1346894 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceService.java @@ -184,4 +184,8 @@ public class ConferenceService { public List findAllActive() { return conferenceRepository.findAllActive(new Date()); } + + public boolean isAttachedToConference(Integer paperId) { + return conferenceRepository.isPaperAttached(paperId); + } } diff --git a/src/main/java/ru/ulstu/core/controller/AdviceController.java b/src/main/java/ru/ulstu/core/controller/AdviceController.java index 8238797..27ba6c9 100644 --- a/src/main/java/ru/ulstu/core/controller/AdviceController.java +++ b/src/main/java/ru/ulstu/core/controller/AdviceController.java @@ -39,6 +39,11 @@ public class AdviceController { return userService.getCurrentUser().getUserAbbreviate(); } + @ModelAttribute("flashMessage") + public String getFlashMessage() { + return null; + } + private Response handleException(ErrorConstants error) { log.warn(error.toString()); return new Response<>(error); diff --git a/src/main/java/ru/ulstu/paper/controller/PaperController.java b/src/main/java/ru/ulstu/paper/controller/PaperController.java index 4b13275..41d5586 100644 --- a/src/main/java/ru/ulstu/paper/controller/PaperController.java +++ b/src/main/java/ru/ulstu/paper/controller/PaperController.java @@ -1,6 +1,5 @@ package ru.ulstu.paper.controller; -import org.springframework.dao.DataIntegrityViolationException; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -9,14 +8,14 @@ import org.springframework.ui.ModelMap; import org.springframework.validation.Errors; 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.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; +import ru.ulstu.conference.service.ConferenceService; import ru.ulstu.deadline.model.Deadline; import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.model.PaperDto; -import ru.ulstu.paper.model.PaperFilterDto; +import ru.ulstu.paper.model.PaperListDto; import ru.ulstu.paper.service.LatexService; import ru.ulstu.paper.service.PaperService; import ru.ulstu.user.model.User; @@ -39,23 +38,34 @@ import static org.springframework.util.StringUtils.isEmpty; @ApiIgnore public class PaperController { private final PaperService paperService; + private final ConferenceService conferenceService; private final LatexService latexService; - public PaperController(PaperService paperService, LatexService latexService) { + public PaperController(PaperService paperService, + ConferenceService conferenceService, + LatexService latexService) { this.paperService = paperService; + this.conferenceService = conferenceService; this.latexService = latexService; } @GetMapping("/papers") public void getPapers(ModelMap modelMap) { - modelMap.put("filteredPapers", new PaperFilterDto(paperService.findAllDto(), null, null)); + modelMap.put("filteredPapers", new PaperListDto(paperService.findAllDto(), null, null)); } @PostMapping("/papers") - public void filterPapers(@Valid PaperFilterDto paperFilterDto, ModelMap modelMap) { - modelMap.put("filteredPapers", new PaperFilterDto(paperService.filter(paperFilterDto), - paperFilterDto.getFilterAuthorId(), - paperFilterDto.getYear())); + public void listPapers(@Valid PaperListDto paperListDto, ModelMap modelMap) { + if (paperListDto.getPaperDeleteId() != null) { + if (conferenceService.isAttachedToConference(paperListDto.getPaperDeleteId())) { + modelMap.put("flashMessage", "Статью нельзя удалить, она прикреплена к конференции"); + } else { + paperService.delete(paperListDto.getPaperDeleteId()); + } + } + modelMap.put("filteredPapers", new PaperListDto(paperService.filter(paperListDto), + paperListDto.getFilterAuthorId(), + paperListDto.getYear())); } @GetMapping("/dashboard") @@ -95,19 +105,6 @@ public class PaperController { return "/papers/paper"; } - @GetMapping("/delete/{paper-id}") - public String delete(@Valid PaperFilterDto paperFilterDto, - @PathVariable("paper-id") Integer paperId, - Errors errors) throws IOException { - try { - paperService.delete(paperId); - } catch (DataIntegrityViolationException e) { - errors.reject("relationExist", "Статья прикреплена к конференции"); - return "/papers/papers"; - } - return "redirect:/papers/papers"; - } - @ModelAttribute("allStatuses") public List getPaperStatuses() { return paperService.getPaperStatuses(); diff --git a/src/main/java/ru/ulstu/paper/controller/PaperRestController.java b/src/main/java/ru/ulstu/paper/controller/PaperRestController.java index 2bd8384..d98ba98 100644 --- a/src/main/java/ru/ulstu/paper/controller/PaperRestController.java +++ b/src/main/java/ru/ulstu/paper/controller/PaperRestController.java @@ -11,7 +11,7 @@ import org.springframework.web.bind.annotation.RestController; import ru.ulstu.configuration.Constants; import ru.ulstu.core.model.response.Response; import ru.ulstu.paper.model.PaperDto; -import ru.ulstu.paper.model.PaperFilterDto; +import ru.ulstu.paper.model.PaperListDto; import ru.ulstu.paper.service.PaperService; import javax.validation.Valid; @@ -58,8 +58,8 @@ public class PaperRestController { } @PostMapping("/filter") - public Response> filter(@RequestBody @Valid PaperFilterDto paperFilterDto) throws IOException { - return new Response<>(paperService.filter(paperFilterDto)); + public Response> filter(@RequestBody @Valid PaperListDto paperListDto) throws IOException { + return new Response<>(paperService.filter(paperListDto)); } @GetMapping("formatted-list") diff --git a/src/main/java/ru/ulstu/paper/model/PaperFilterDto.java b/src/main/java/ru/ulstu/paper/model/PaperListDto.java similarity index 67% rename from src/main/java/ru/ulstu/paper/model/PaperFilterDto.java rename to src/main/java/ru/ulstu/paper/model/PaperListDto.java index f7aef29..871047a 100644 --- a/src/main/java/ru/ulstu/paper/model/PaperFilterDto.java +++ b/src/main/java/ru/ulstu/paper/model/PaperListDto.java @@ -2,15 +2,16 @@ package ru.ulstu.paper.model; import java.util.List; -public class PaperFilterDto { +public class PaperListDto { private List papers; private Integer filterAuthorId; + private Integer paperDeleteId; private Integer year; - public PaperFilterDto() { + public PaperListDto() { } - public PaperFilterDto(List paperDtos, Integer filterAuthorId, Integer year) { + public PaperListDto(List paperDtos, Integer filterAuthorId, Integer year) { this.papers = paperDtos; this.filterAuthorId = filterAuthorId; this.year = year; @@ -39,4 +40,12 @@ public class PaperFilterDto { public void setYear(Integer year) { this.year = year; } + + public Integer getPaperDeleteId() { + return paperDeleteId; + } + + public void setPaperDeleteId(Integer paperDeleteId) { + this.paperDeleteId = paperDeleteId; + } } diff --git a/src/main/java/ru/ulstu/paper/service/PaperService.java b/src/main/java/ru/ulstu/paper/service/PaperService.java index 6d0456c..32561a9 100644 --- a/src/main/java/ru/ulstu/paper/service/PaperService.java +++ b/src/main/java/ru/ulstu/paper/service/PaperService.java @@ -9,7 +9,7 @@ import ru.ulstu.file.model.FileDataDto; import ru.ulstu.file.service.FileService; import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.model.PaperDto; -import ru.ulstu.paper.model.PaperFilterDto; +import ru.ulstu.paper.model.PaperListDto; import ru.ulstu.paper.repository.PaperRepository; import ru.ulstu.timeline.service.EventService; import ru.ulstu.user.model.User; @@ -181,7 +181,7 @@ public class PaperService { return paper; } - public List filter(PaperFilterDto filterDto) { + public List filter(PaperListDto filterDto) { return convert(sortPapers(paperRepository.filter( filterDto.getFilterAuthorId() == null ? null : userService.findById(filterDto.getFilterAuthorId()), filterDto.getYear())), PaperDto::new); diff --git a/src/main/resources/public/js/papers.js b/src/main/resources/public/js/papers.js index 78d3120..7a937ba 100644 --- a/src/main/resources/public/js/papers.js +++ b/src/main/resources/public/js/papers.js @@ -13,7 +13,8 @@ $(document).ready(function () { }); $('a[data-confirm]').click(function(ev) { - var href = $(this).attr('href'); + var id = $(this).parent().parent().find('.id-class').val(); + if (!$('#dataConfirmModal').length) { $('#modalDelete').append(''); } $('#dataConfirmModal').find('#myModalLabel').text($(this).attr('data-confirm')); - $('#dataConfirmOK').attr('href', href); + $('#dataConfirmOK').click(function () { + $("#paperDeleteId").val(id); + $('form').submit(); + }); $('#dataConfirmModal').modal({show:true}); return false; }); diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index 00e2c0f..89bc9ec 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -55,7 +55,8 @@ Сайт кафедры