#111 save method refactor

This commit is contained in:
Nightblade73 2019-05-13 19:08:21 +04:00
parent d846192046
commit 6044d6f8e3
3 changed files with 20 additions and 14 deletions

View File

@ -74,18 +74,9 @@ public class ConferenceController {
@PostMapping(value = "/conference", params = "save")
public String save(@Valid ConferenceDto conferenceDto, Errors errors) throws IOException {
conferenceService.filterEmptyDeadlines(conferenceDto);
conferenceService.checkEmptyFieldsOfDeadline(conferenceDto, errors);
conferenceDto.setName(conferenceDto.getTitle());
conferenceService.checkUniqueName(conferenceDto,
errors,
conferenceDto.getId(),
"title",
"Конференция с таким именем уже существует");
if (errors.hasErrors()) {
if (!conferenceService.save(conferenceDto, errors)) {
return CONFERENCE_PAGE;
}
conferenceService.save(conferenceDto);
return String.format(REDIRECT_TO, CONFERENCES_PAGE);
}

View File

@ -91,12 +91,26 @@ public class ConferenceService extends BaseService {
return new ConferenceDto(conferenceRepository.findOne(id));
}
public void save(ConferenceDto conferenceDto) throws IOException {
public boolean save(ConferenceDto conferenceDto, Errors errors) throws IOException {
conferenceDto.setName(conferenceDto.getTitle());
filterEmptyDeadlines(conferenceDto);
checkEmptyFieldsOfDeadline(conferenceDto, errors);
checkUniqueName(conferenceDto,
errors,
conferenceDto.getId(),
"title",
"Конференция с таким именем уже существует");
if (errors.hasErrors()) {
return false;
}
if (isEmpty(conferenceDto.getId())) {
create(conferenceDto);
} else {
update(conferenceDto);
}
return true;
}
@Transactional

View File

@ -30,11 +30,12 @@
<label for="title">Название:</label>
<input class="form-control" th:field="*{title}" id="title" type="text"
placeholder="Название конференции"/>
<p th:if="${#fields.hasErrors('title')}" th:errors="*{title}"
class="alert alert-danger">Incorrect title</p>
<p class="help-block text-danger"></p>
</div>
<p th:if="${#fields.hasErrors('title')}" th:errors="*{title}"
class="alert alert-danger">Incorrect title</p>
<p class="help-block text-danger"></p>
<div class="form-group">
<label for="url">URL:</label>
<input class="form-control" th:field="*{url}" id="url" type="text"