Commit 73a77542 authored by s7_spruge_k's avatar s7_spruge_k
Browse files

Merge branch 'view-calendar-validations' into 'master'

fixed add-new-event validations again

See merge request s7_spruge_k/calendar!44
parents a667c04d 0e81a085
......@@ -100,19 +100,11 @@ public class EventController {
@PostMapping(value = "/add-new-event")
public String addNewEventPost(@Valid Event event, BindingResult bindingResult, String calName, Model models)
{
List<EventType> allEventTypes = Arrays.asList(EventType.values());
User currUser = findCurrentUser();
//If user doesnt have calendar - redirected to create
try
{
calendarRepo.findAllByUser(currUser).get(0);
}
catch(Exception e)
{
return "redirect:/add-new-calendar";
}
//Gets all the calendars and eventTypes again.
List<EventType> allEventTypes = Arrays.asList(EventType.values());
User currUser = findCurrentUser();
ArrayList<Calendar> allUserCalendars = calendarRepo.findAllByUser(currUser);
JsonArray calendarArray = new JsonArray();
......@@ -127,9 +119,7 @@ public class EventController {
models.addAttribute("eventTypes", allEventTypes);
eventValidator.validate(event, bindingResult);
......
......@@ -51,10 +51,18 @@ public class EventValidator implements Validator {
return;
}
if(e.getStartTime() == null)
{
errors.rejectValue("startTime", "DateInPast");
return;
}
if (e.getStartDate().isBefore(LocalDate.now())) {
errors.rejectValue("startDate", "DateInPast");
return;
}
if(e.getStartDate().equals(LocalDate.now()) && e.getStartTime().isBefore(LocalTime.now()))
{
errors.rejectValue("startDate", "DateInPast");
......@@ -62,29 +70,18 @@ public class EventValidator implements Validator {
}
// //Get current user
// Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//
// String username = "";
// if (principal instanceof UserDetails) {
// username = ((UserDetails)principal).getUsername();
// } else {
// username = principal.toString();
// }
//
//
// User currUser = userRepo.findByUsername(username);
// ArrayList<Calendar> c1 = calendarRepo.findAllByUser(currUser);
//
// ArrayList<Event> allUserEvents = eventRepo.findAllByCalendar(c1);
//
// for (Event event : allUserEvents) {
// if(e.getStartDate().equals(event.getStartDate()) && e.getStartTime().equals(event.getStartTime()))
// {
// errors.rejectValue("startDate", "DateTaken");
// break;
// }
// }
//Check if there's an event at that time.
Calendar c1 = e.getCalendar();
ArrayList<Event> allUserEvents = eventRepo.findAllByCalendar(c1);
for (Event event : allUserEvents) {
if(e.getStartDate().equals(event.getStartDate()) && e.getStartTime().equals(event.getStartTime()))
{
errors.rejectValue("startDate", "DateTaken");
break;
}
}
}
}
\ No newline at end of file
......@@ -7,4 +7,5 @@ NotBlank=This field cannot be empty!
DateInPast=Date cannot be in the past.
DateEmpty=Date cannot be left empty.
DateTaken=You already have an event at this time!
CalendarExists=You already have a calendar with this name!
\ No newline at end of file
CalendarExists=You already have a calendar with this name!
TimeEmpty=Time cannot be left empty.
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment