Commit f0aee202 authored by s7_spruge_k's avatar s7_spruge_k
Browse files

Woring Event validations

parent dc84d1ec
......@@ -18,6 +18,8 @@ import com.example.calendar.models.User;
import com.example.calendar.repo.CalendarRepo;
import com.example.calendar.repo.EventRepo;
import com.example.calendar.repo.UserRepo;
import com.example.calendar.validator.EventValidator;
import com.example.calendar.validator.UserValidator;
import java.util.List;
......@@ -37,7 +39,8 @@ public class EventController {
@Autowired
CalendarRepo calendarRepo;
@Autowired
private EventValidator eventValidator;
@GetMapping(value = "/add-new-event")
public String addNewEventGet(Model models, Event event)
......@@ -50,7 +53,7 @@ public class EventController {
@PostMapping(value = "/add-new-event")
public String addNewEventPost(@Valid Event event, BindingResult bindingResult)
{
eventValidator.validate(event, bindingResult);
if (bindingResult.hasErrors()) {
return "addNewEvent";
}
......
......@@ -12,8 +12,6 @@ import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import org.springframework.format.annotation.DateTimeFormat;
......@@ -21,7 +19,6 @@ import org.springframework.format.annotation.DateTimeFormat;
@Table(name = "EventTable")
public class Event{
@NotBlank
@Column(name = "Name")
private String name;
@Column(name = "Description")
......
package com.example.calendar.validator;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import com.example.calendar.models.Calendar;
import com.example.calendar.models.Event;
import com.example.calendar.models.User;
import com.example.calendar.repo.CalendarRepo;
import com.example.calendar.repo.EventRepo;
import com.example.calendar.repo.UserRepo;
@Component
public class EventValidator implements Validator {
@Autowired
EventRepo eventRepo;
@Autowired
CalendarRepo calendarRepo;
@Autowired
UserRepo userRepo;
@Override
public boolean supports(Class<?> aClass) {
return Event.class.equals(aClass);
......@@ -25,11 +46,47 @@ public class EventValidator implements Validator {
// Date cannot be in the past
// Date & time cannot be the same
// Time cannot be in the past of current day
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "NotEmpty");
if (e.getStartDate() == null)
{
errors.rejectValue("startDate", "DateEmpty");
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");
return;
}
//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);
Calendar c1 = calendarRepo.findByUser(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;
}
}
}
}
\ No newline at end of file
......@@ -46,13 +46,14 @@ a:hover {
<br><br><br> <form action="#" th:action="@{/add-new-event}" th:object="${event}" method="post">
<h2 class="form-signin-heading text-center">Add an event</h2><br>
<div class="form-group" th:classappend="${#fields.hasErrors('*')} ? 'has-error' : ''">
<input name="name" type="text" th:field="*{name}" class="form-control" placeholder="Title" autofocus="true"/><br>
<p th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Name Error</p>
<p class="alert alert-danger" th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></p>
</div>
<textarea name="description" th:field="*{description}" placeholder="Description..." class="form-control" rows="3"></textarea>
<br><div class="row">
<div class="col-sm-5">Date: <input id="datePicker" type="date" th:field="*{startDate}" /> </div>
<p class="alert alert-danger" th:if="${#fields.hasErrors('startDate')}" th:errors="startDate"></p>
<div class="col-sm-5" th:classappend="${#fields.hasErrors('*')} ? 'has-error' : ''">Date: <input id="datePicker" type="date" th:field="*{startDate}" />
<p class="alert alert-danger" th:if="${#fields.hasErrors('startDate')}" th:errors="*{startDate}"></p></div>
<div class="col-sm-4">Time: <input id="timePicker" type="time" th:field="*{startTime}" /></div>
<div class="col-sm-3">
<div class="form-check">
......
......@@ -4,4 +4,6 @@ Duplicate.userForm.username=Someone already has that username.
Size.userForm.password=Try one with at least 8 characters.
Diff.userForm.passwordConfirm=These passwords don't match.
NotBlank=This field cannot be empty!
DateInPast=Date cannot be in the past.
\ No newline at end of file
DateInPast=Date cannot be in the past.
DateEmpty=Date cannot be left empty.
DateTaken=You already have an event at this time!
\ 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