#55 added create and update methods
This commit is contained in:
parent
d6b7fe790a
commit
67bfad22f9
@ -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()));
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user