모임 추가를 처리하는 핸들러 생성
package me.weekbelt.studyolle.event;
@Controller
@RequestMapping("/study/{path}")
@RequiredArgsConstructor
public class EventController {
// ...........
private final EventService eventService;
private final ModelMapper modelMapper;
// ...........
@PostMapping("/new-event")
public String newEventSubmit(@CurrentAccount Account account, @PathVariable String path,
@Valid EventForm eventForm, Errors errors, Model model) {
Study study = studyService.getStudyToUpdateStatus(account, path);
if (errors.hasErrors()) {
model.addAttribute("account", account);
model.addAttribute("study", study);
return "event/form";
}
Event event = eventService.createEvent(modelMapper.map(eventForm, Event.class), study, account);
return "redirect:/study/" + study.getEncodedPath() + "/events/" + event.getId();
}
}
모임 추가를 처리하는 createEvent메소드 작성
package me.weekbelt.studyolle.event;
@Service
@Transactional
@RequiredArgsConstructor
public class EventService {
private final EventRepository eventRepository;
public Event createEvent(Event event, Study study, Account account) {
event.setCreatedBy(account);
event.setCreatedDateTime(LocalDateTime.now());
event.setStudy(study);
return eventRepository.save(event);
}
}
EventRepository 작성
package me.weekbelt.studyolle.event;
@Transactional(readOnly = true)
public interface EventRepository extends JpaRepository<Event, Long> {
}
모임 폼에 입력한 값중에 날짜 관련 처리를 검증하는 EventValidator 작성
package me.weekbelt.studyolle.event.validator;
@Component
public class EventValidator implements Validator {
@Override
public boolean supports(Class<?> aClass) {
return EventForm.class.isAssignableFrom(aClass);
}
@Override
public void validate(Object target, Errors errors) {
EventForm eventForm = (EventForm) target;
if (isNotValidEndEnrollmentDateTime(eventForm)) {
errors.rejectValue("endEnrollmentDateTime", "wrong.datetime", "모임 접수 종료 일시를 정확히 입력하세요.");
}
if (isNotValidEndDateTime(eventForm)) {
errors.rejectValue("endDateTime", "wrong.datetime", "모임 접수 종료 일시를 정확히 입력하세요.");
}
if (isNotValidStartDateTime(eventForm)) {
errors.rejectValue("startDateTime", "wrong.datetime", "모임 시작 일시를 정확히 입력하세요.");
}
}
private boolean isNotValidStartDateTime(EventForm eventForm) {
return eventForm.getStartDateTime().isBefore(eventForm.getEndEnrollmentDateTime());
}
private boolean isNotValidEndEnrollmentDateTime(EventForm eventForm) {
return eventForm.getEndEnrollmentDateTime().isBefore(LocalDateTime.now());
}
private boolean isNotValidEndDateTime(EventForm eventForm) {
LocalDateTime endDateTime = eventForm.getEndDateTime();
return endDateTime.isBefore(eventForm.getStartDateTime()) || endDateTime.isBefore(eventForm.getEndEnrollmentDateTime());
}
}
EventController에 EventValidator등록
package me.weekbelt.studyolle.event;
@Controller
@RequestMapping("/study/{path}")
@RequiredArgsConstructor
public class EventController {
// .....
private final EventValidator eventValidator;
@InitBinder("eventForm")
public void initBinder(WebDataBinder webDataBinder){
webDataBinder.addValidators(eventValidator);
}
// .....
}
'스프링과 JPA 기반 웹 어플리케이션 개발 > 5부 모임' 카테고리의 다른 글
64. 모임 목록 조회 (0) | 2020.05.04 |
---|---|
63. 모임 조회 (0) | 2020.05.04 |
61. 모임 만들기 (0) | 2020.04.30 |
60. 모임 도메인 (0) | 2020.04.30 |
59. 모임 관리 기능 미리보기 (0) | 2020.04.28 |