From 67bfad22f9c8183f8873f95fa88197042ee581eb Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Fri, 5 Apr 2019 16:30:45 +0400 Subject: [PATCH] #55 added create and update methods --- .../controller/ConferenceController.java | 28 +++++++++++++ .../conference/service/ConferenceService.java | 42 +++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/src/main/java/ru/ulstu/conference/controller/ConferenceController.java b/src/main/java/ru/ulstu/conference/controller/ConferenceController.java index 291b5fc..0da8d8d 100644 --- a/src/main/java/ru/ulstu/conference/controller/ConferenceController.java +++ b/src/main/java/ru/ulstu/conference/controller/ConferenceController.java @@ -3,14 +3,23 @@ package ru.ulstu.conference.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; +import org.springframework.validation.Errors; import org.springframework.web.bind.annotation.GetMapping; +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.model.ConferenceDto; import ru.ulstu.conference.model.ConferenceFilterDto; import ru.ulstu.conference.service.ConferenceService; import ru.ulstu.paper.model.PaperDto; import springfox.documentation.annotations.ApiIgnore; +import javax.validation.Valid; +import java.io.IOException; +import java.util.stream.Collectors; + +import static org.springframework.util.StringUtils.isEmpty; + @Controller() @RequestMapping(value = "/conferences") @ApiIgnore @@ -36,4 +45,23 @@ public class ConferenceController { modelMap.put("conferenceDto", new PaperDto()); } } + + @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()) { + return "/conferences/conference"; + } + conferenceService.save(conferenceDto); + return "redirect:/conferences/conferences"; + } + + private void filterEmptyDeadlines(ConferenceDto conferenceDto) { + conferenceDto.setDeadlines(conferenceDto.getDeadlines().stream() + .filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription())) + .collect(Collectors.toList())); + } } diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceService.java b/src/main/java/ru/ulstu/conference/service/ConferenceService.java index 8fcccb0..e5afd0f 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceService.java @@ -2,13 +2,16 @@ package ru.ulstu.conference.service; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import ru.ulstu.conference.model.Conference; import ru.ulstu.conference.model.ConferenceDto; import ru.ulstu.conference.repository.ConferenceRepository; import ru.ulstu.deadline.service.DeadlineService; +import java.io.IOException; import java.util.List; +import static org.springframework.util.ObjectUtils.isEmpty; import static ru.ulstu.core.util.StreamApiUtils.convert; @Service @@ -38,5 +41,44 @@ public class ConferenceService { return new ConferenceDto(conferenceRepository.findOne(id)); } + public void save(ConferenceDto conferenceDto) throws IOException { + if (isEmpty(conferenceDto.getId())) { + create(conferenceDto); + } else { + update(conferenceDto); + } + } + + @Transactional + 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)); + + return conference.getId(); + } + + private Conference copyFromDto(Conference conference, ConferenceDto conferenceDto) throws IOException { + conference.setTitle(conferenceDto.getTitle()); + conference.setDescription(conferenceDto.getDescription()); + conference.setUrl(conferenceDto.getUrl()); + conference.setPing(0); + conference.setBeginDate(conference.getBeginDate()); + conference.setEndDate(conference.getEndDate()); + + conference.setDeadlines(deadlineService.saveOrCreate(conferenceDto.getDeadlines())); + + return conference; + } + }