diff --git a/src/main/java/ru/ulstu/conference/controller/ConferenceController.java b/src/main/java/ru/ulstu/conference/controller/ConferenceController.java index 006a1de..7a229ca 100644 --- a/src/main/java/ru/ulstu/conference/controller/ConferenceController.java +++ b/src/main/java/ru/ulstu/conference/controller/ConferenceController.java @@ -52,11 +52,9 @@ public class ConferenceController { @PostMapping(value = "/conference", params = "save") public String save(@Valid ConferenceDto conferenceDto, Errors errors) throws IOException { filterEmptyDeadlines(conferenceDto); - if (conferenceDto.getDeadlines().isEmpty()) { - errors.rejectValue("deadlines", "errorCode", "Не может быть пустым"); - } - if (errors.hasErrors()) + if (errors.hasErrors()) { return CONFERENCE_PAGE; + } conferenceService.save(conferenceDto); return String.format(REDIRECT_TO, CONFERENCES_PAGE); @@ -65,12 +63,23 @@ public class ConferenceController { @PostMapping(value = "/conference", params = "addDeadline") public String addDeadline(@Valid ConferenceDto conferenceDto, Errors errors) { filterEmptyDeadlines(conferenceDto); - if (errors.hasErrors()) + if (errors.hasErrors()) { return CONFERENCE_PAGE; + } conferenceDto.getDeadlines().add(new Deadline()); return CONFERENCE_PAGE; } + @PostMapping(value = "/conference", params = "removeDeadline") + public String removeDeadline(@Valid ConferenceDto conferenceDto, Errors errors, + @RequestParam(value = "removeDeadline") Integer deadlineIndex) throws IOException { + if (errors.hasErrors()) { + return CONFERENCE_PAGE; + } + conferenceService.removeDeadline(conferenceDto, deadlineIndex); + return CONFERENCE_PAGE; + } + private void filterEmptyDeadlines(ConferenceDto conferenceDto) { conferenceDto.setDeadlines(conferenceDto.getDeadlines().stream() .filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription())) diff --git a/src/main/java/ru/ulstu/conference/model/Conference.java b/src/main/java/ru/ulstu/conference/model/Conference.java index 1c824b3..faed15c 100644 --- a/src/main/java/ru/ulstu/conference/model/Conference.java +++ b/src/main/java/ru/ulstu/conference/model/Conference.java @@ -56,7 +56,6 @@ public class Conference extends BaseEntity { @OrderBy("date") private List deadlines = new ArrayList<>(); - @ManyToMany(fetch = FetchType.EAGER) @JoinTable(name = "paper_conference", joinColumns = {@JoinColumn(name = "conference_id")}, diff --git a/src/main/java/ru/ulstu/conference/model/ConferenceDto.java b/src/main/java/ru/ulstu/conference/model/ConferenceDto.java index 277e783..e5efcde 100644 --- a/src/main/java/ru/ulstu/conference/model/ConferenceDto.java +++ b/src/main/java/ru/ulstu/conference/model/ConferenceDto.java @@ -36,8 +36,8 @@ public class ConferenceDto { @Temporal(TemporalType.TIMESTAMP) @DateTimeFormat(pattern = "yyyy-MM-dd") private Date endDate = new Date(); - @NotEmpty private List deadlines = new ArrayList<>(); + private List removedDeadlineIds = new ArrayList<>(); private Set userIds = new HashSet<>(); private Set paperIds = new HashSet<>(); private Set papers = new HashSet<>(); @@ -45,7 +45,6 @@ public class ConferenceDto { private Integer filterUserId; public ConferenceDto() { - deadlines.add(new Deadline()); } @JsonCreator @@ -194,4 +193,13 @@ public class ConferenceDto { public void setFilterUserId(Integer filterUserId) { this.filterUserId = filterUserId; } + + public List getRemovedDeadlineIds() { + return removedDeadlineIds; + } + + public void setRemovedDeadlineIds(List removedDeadlineIds) { + this.removedDeadlineIds = removedDeadlineIds; + } + } diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceService.java b/src/main/java/ru/ulstu/conference/service/ConferenceService.java index c28c637..9c40295 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceService.java @@ -53,20 +53,24 @@ public class ConferenceService { public Integer create(ConferenceDto conferenceDto) throws IOException { Conference newConference = copyFromDto(new Conference(), conferenceDto); newConference = conferenceRepository.save(newConference); - - return newConference.getId(); } @Transactional public Integer update(ConferenceDto conferenceDto) throws IOException { Conference conference = conferenceRepository.findOne(conferenceDto.getId()); - conferenceRepository.save(copyFromDto(conference, conferenceDto)); - + conferenceDto.getRemovedDeadlineIds().forEach(deadlineService::remove); return conference.getId(); } + public void removeDeadline(ConferenceDto conferenceDto, Integer deadlineIndex) throws IOException { + if (conferenceDto.getDeadlines().get(deadlineIndex).getId() != null) { + conferenceDto.getRemovedDeadlineIds().add(conferenceDto.getDeadlines().get(deadlineIndex).getId()); + } + conferenceDto.getDeadlines().remove((int) deadlineIndex); + } + private Conference copyFromDto(Conference conference, ConferenceDto conferenceDto) throws IOException { conference.setTitle(conferenceDto.getTitle()); conference.setDescription(conferenceDto.getDescription()); @@ -74,9 +78,7 @@ public class ConferenceService { conference.setPing(0); conference.setBeginDate(conferenceDto.getBeginDate()); conference.setEndDate(conferenceDto.getEndDate()); - conference.setDeadlines(deadlineService.saveOrCreate(conferenceDto.getDeadlines())); - return conference; } diff --git a/src/main/java/ru/ulstu/deadline/service/DeadlineService.java b/src/main/java/ru/ulstu/deadline/service/DeadlineService.java index 2180025..0ef8a4f 100644 --- a/src/main/java/ru/ulstu/deadline/service/DeadlineService.java +++ b/src/main/java/ru/ulstu/deadline/service/DeadlineService.java @@ -41,4 +41,9 @@ public class DeadlineService { newDeadline = deadlineRepository.save(newDeadline); return newDeadline; } + + @Transactional + public void remove(Integer deadlineId) { + deadlineRepository.delete(deadlineId); + } } diff --git a/src/main/resources/public/css/conference.css b/src/main/resources/public/css/conference.css index ec88796..004fe8c 100644 --- a/src/main/resources/public/css/conference.css +++ b/src/main/resources/public/css/conference.css @@ -19,6 +19,7 @@ body { .deadline { margin: 0; height: 40px; + min-height: 40px; } .deadline-text { @@ -63,6 +64,9 @@ body { .icon-delete { background-color: #f44; + background-image: url(/img/conference/delete.png); + background-repeat: round; + color: transparent !important; } .icon-delete:hover { diff --git a/src/main/resources/templates/conferences/conference.html b/src/main/resources/templates/conferences/conference.html index cbc0019..c93fc39 100644 --- a/src/main/resources/templates/conferences/conference.html +++ b/src/main/resources/templates/conferences/conference.html @@ -50,6 +50,7 @@
+
@@ -58,11 +59,10 @@ th:field="*{deadlines[__${rowStat.index}__].description}"/> - Удалить + +