Merge branch '62-delete-deadline' into 'dev'

Resolve "Удаление дедлайна"

Closes #62

See merge request romanov73/ng-tracker!50
This commit is contained in:
Anton Romanov 2019-04-14 19:33:45 +00:00
commit 9e657a7d2d
7 changed files with 46 additions and 19 deletions

View File

@ -52,11 +52,9 @@ public class ConferenceController {
@PostMapping(value = "/conference", params = "save") @PostMapping(value = "/conference", params = "save")
public String save(@Valid ConferenceDto conferenceDto, Errors errors) throws IOException { public String save(@Valid ConferenceDto conferenceDto, Errors errors) throws IOException {
filterEmptyDeadlines(conferenceDto); filterEmptyDeadlines(conferenceDto);
if (conferenceDto.getDeadlines().isEmpty()) { if (errors.hasErrors()) {
errors.rejectValue("deadlines", "errorCode", "Не может быть пустым");
}
if (errors.hasErrors())
return CONFERENCE_PAGE; return CONFERENCE_PAGE;
}
conferenceService.save(conferenceDto); conferenceService.save(conferenceDto);
return String.format(REDIRECT_TO, CONFERENCES_PAGE); return String.format(REDIRECT_TO, CONFERENCES_PAGE);
@ -65,12 +63,23 @@ public class ConferenceController {
@PostMapping(value = "/conference", params = "addDeadline") @PostMapping(value = "/conference", params = "addDeadline")
public String addDeadline(@Valid ConferenceDto conferenceDto, Errors errors) { public String addDeadline(@Valid ConferenceDto conferenceDto, Errors errors) {
filterEmptyDeadlines(conferenceDto); filterEmptyDeadlines(conferenceDto);
if (errors.hasErrors()) if (errors.hasErrors()) {
return CONFERENCE_PAGE; return CONFERENCE_PAGE;
}
conferenceDto.getDeadlines().add(new Deadline()); conferenceDto.getDeadlines().add(new Deadline());
return CONFERENCE_PAGE; 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) { private void filterEmptyDeadlines(ConferenceDto conferenceDto) {
conferenceDto.setDeadlines(conferenceDto.getDeadlines().stream() conferenceDto.setDeadlines(conferenceDto.getDeadlines().stream()
.filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription())) .filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription()))

View File

@ -56,7 +56,6 @@ public class Conference extends BaseEntity {
@OrderBy("date") @OrderBy("date")
private List<Deadline> deadlines = new ArrayList<>(); private List<Deadline> deadlines = new ArrayList<>();
@ManyToMany(fetch = FetchType.EAGER) @ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "paper_conference", @JoinTable(name = "paper_conference",
joinColumns = {@JoinColumn(name = "conference_id")}, joinColumns = {@JoinColumn(name = "conference_id")},

View File

@ -36,8 +36,8 @@ public class ConferenceDto {
@Temporal(TemporalType.TIMESTAMP) @Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(pattern = "yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd")
private Date endDate = new Date(); private Date endDate = new Date();
@NotEmpty
private List<Deadline> deadlines = new ArrayList<>(); private List<Deadline> deadlines = new ArrayList<>();
private List<Integer> removedDeadlineIds = new ArrayList<>();
private Set<Integer> userIds = new HashSet<>(); private Set<Integer> userIds = new HashSet<>();
private Set<Integer> paperIds = new HashSet<>(); private Set<Integer> paperIds = new HashSet<>();
private Set<PaperDto> papers = new HashSet<>(); private Set<PaperDto> papers = new HashSet<>();
@ -45,7 +45,6 @@ public class ConferenceDto {
private Integer filterUserId; private Integer filterUserId;
public ConferenceDto() { public ConferenceDto() {
deadlines.add(new Deadline());
} }
@JsonCreator @JsonCreator
@ -194,4 +193,13 @@ public class ConferenceDto {
public void setFilterUserId(Integer filterUserId) { public void setFilterUserId(Integer filterUserId) {
this.filterUserId = filterUserId; this.filterUserId = filterUserId;
} }
public List<Integer> getRemovedDeadlineIds() {
return removedDeadlineIds;
}
public void setRemovedDeadlineIds(List<Integer> removedDeadlineIds) {
this.removedDeadlineIds = removedDeadlineIds;
}
} }

View File

@ -53,20 +53,24 @@ public class ConferenceService {
public Integer create(ConferenceDto conferenceDto) throws IOException { public Integer create(ConferenceDto conferenceDto) throws IOException {
Conference newConference = copyFromDto(new Conference(), conferenceDto); Conference newConference = copyFromDto(new Conference(), conferenceDto);
newConference = conferenceRepository.save(newConference); newConference = conferenceRepository.save(newConference);
return newConference.getId(); return newConference.getId();
} }
@Transactional @Transactional
public Integer update(ConferenceDto conferenceDto) throws IOException { public Integer update(ConferenceDto conferenceDto) throws IOException {
Conference conference = conferenceRepository.findOne(conferenceDto.getId()); Conference conference = conferenceRepository.findOne(conferenceDto.getId());
conferenceRepository.save(copyFromDto(conference, conferenceDto)); conferenceRepository.save(copyFromDto(conference, conferenceDto));
conferenceDto.getRemovedDeadlineIds().forEach(deadlineService::remove);
return conference.getId(); 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 { private Conference copyFromDto(Conference conference, ConferenceDto conferenceDto) throws IOException {
conference.setTitle(conferenceDto.getTitle()); conference.setTitle(conferenceDto.getTitle());
conference.setDescription(conferenceDto.getDescription()); conference.setDescription(conferenceDto.getDescription());
@ -74,9 +78,7 @@ public class ConferenceService {
conference.setPing(0); conference.setPing(0);
conference.setBeginDate(conferenceDto.getBeginDate()); conference.setBeginDate(conferenceDto.getBeginDate());
conference.setEndDate(conferenceDto.getEndDate()); conference.setEndDate(conferenceDto.getEndDate());
conference.setDeadlines(deadlineService.saveOrCreate(conferenceDto.getDeadlines())); conference.setDeadlines(deadlineService.saveOrCreate(conferenceDto.getDeadlines()));
return conference; return conference;
} }

View File

@ -41,4 +41,9 @@ public class DeadlineService {
newDeadline = deadlineRepository.save(newDeadline); newDeadline = deadlineRepository.save(newDeadline);
return newDeadline; return newDeadline;
} }
@Transactional
public void remove(Integer deadlineId) {
deadlineRepository.delete(deadlineId);
}
} }

View File

@ -19,6 +19,7 @@ body {
.deadline { .deadline {
margin: 0; margin: 0;
height: 40px; height: 40px;
min-height: 40px;
} }
.deadline-text { .deadline-text {
@ -63,6 +64,9 @@ body {
.icon-delete { .icon-delete {
background-color: #f44; background-color: #f44;
background-image: url(/img/conference/delete.png);
background-repeat: round;
color: transparent !important;
} }
.icon-delete:hover { .icon-delete:hover {

View File

@ -50,6 +50,7 @@
<div class="form-group"> <div class="form-group">
<label for="deadlines">Дедлайны:</label> <label for="deadlines">Дедлайны:</label>
<div class="deadline-list form-control list-group" id="deadlines"> <div class="deadline-list form-control list-group" id="deadlines">
<input type="hidden" th:field="*{removedDeadlineIds}"/>
<div class="deadline d-flex p-0 list-group-item list-group-horizontal" <div class="deadline d-flex p-0 list-group-item list-group-horizontal"
th:each="deadline, rowStat : *{deadlines}"> th:each="deadline, rowStat : *{deadlines}">
<input type="hidden" th:field="*{deadlines[__${rowStat.index}__].id}"/> <input type="hidden" th:field="*{deadlines[__${rowStat.index}__].id}"/>
@ -58,11 +59,10 @@
th:field="*{deadlines[__${rowStat.index}__].description}"/> th:field="*{deadlines[__${rowStat.index}__].description}"/>
<input class="list-group-item" type="date" name="deadline" <input class="list-group-item" type="date" name="deadline"
th:field="*{deadlines[__${rowStat.index}__].date}"/> th:field="*{deadlines[__${rowStat.index}__].date}"/>
<img class="icon icon-delete grey-border" src="/img/conference/delete.png" <input type="submit" class="icon icon-delete grey-border"
alt="Удалить" alt="Удалить" name="removeDeadline" th:value="${rowStat.index}"
th:onclick="|$('#deadlines${rowStat.index}\\.description').val(''); />
$('#deadlines${rowStat.index}\\.date').val(''); <!--th:onclick="|$(this).parent().remove();|"-->
$('#addDeadline').click();|"/>
</div> </div>
</div> </div>
</div> </div>