Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
s7_spruge_k
calendar
Commits
0e81a085
Commit
0e81a085
authored
Jun 19, 2019
by
s7_spruge_k
Browse files
fixed add-new-event validations again
parent
688491f4
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
26 additions
and
38 deletions
+26
-38
src/main/java/com/example/calendar/controller/EventController.java
...java/com/example/calendar/controller/EventController.java
+4
-14
src/main/java/com/example/calendar/validator/EventValidator.java
...n/java/com/example/calendar/validator/EventValidator.java
+20
-23
src/main/resources/validation.properties
src/main/resources/validation.properties
+2
-1
No files found.
src/main/java/com/example/calendar/controller/EventController.java
View file @
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
);
...
...
src/main/java/com/example/calendar/validator/EventValidator.java
View file @
0e81a085
...
...
@@ -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
src/main/resources/validation.properties
View file @
0e81a085
...
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment