Commit 06e8b0b6 authored by s7_spruge_k's avatar s7_spruge_k
Browse files

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

View calendar validations

See merge request s7_spruge_k/calendar!45
parents a671acca 2774b0ee
......@@ -43,6 +43,8 @@ public class CalendarController {
@Autowired
private CalendarValidator calendarValidator;
public User findCurrentUser()
{
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
......@@ -66,18 +68,17 @@ public class CalendarController {
{
User currUser = findCurrentUser();
//If user doesnt have calendar - redirected to create
try
{
calendarRepo.findAllByUser(currUser).get(0);
calendarRepo.findByNameAndUser(currUser.getActiveCalendar(), currUser);
}
catch(Exception e)
{
return "redirect:/add-new-calendar";
}
Calendar c1 = calendarRepo.findByName(currUser.getActiveCalendar());
Calendar c1 = calendarRepo.findByNameAndUser(currUser.getActiveCalendar(), currUser);
//Get Events By Calendar
ArrayList<Event> allEvents = eventRepo.findAllByCalendar(c1);
......@@ -202,9 +203,27 @@ public class CalendarController {
@PostMapping(value="/view-calendars", params="action=edit")
public String editCalendar(Model model, String currName, String newName){
Calendar c1 = calendarRepo.findByName(currName);
User currUser = findCurrentUser();
if(newName.equals(""))
{
newName = currUser.getUsername() + LocalTime.now().getHour()+ LocalTime.now().getMinute();
}
//If calendar With the name exists
if(calendarRepo.findByNameAndUser(newName, currUser) != null)
{
newName+="(1)";
}
System.out.println(newName);
Calendar c1 = calendarRepo.findByNameAndUser(currName, currUser);
System.out.println(c1);
if(c1.getName().equals(currUser.getActiveCalendar()))
{
currUser.setActiveCalendar(newName);
......@@ -219,7 +238,8 @@ public class CalendarController {
@PostMapping(value="/view-calendars", params="action=delete")
public String deleteCalendar(Model model, String calName){
Calendar c1 = calendarRepo.findByName(calName);
User currUser = findCurrentUser();
Calendar c1 = calendarRepo.findByNameAndUser(calName, currUser);
ArrayList<Event> allEvents = eventRepo.findAllByCalendar(c1);
for (int i = 0; i < allEvents.size(); i++) {
......@@ -227,13 +247,10 @@ public class CalendarController {
}
calendarRepo.delete(c1);
User currUser = findCurrentUser();
ArrayList<Calendar> allUserCals = calendarRepo.findAllByUser(currUser);
currUser.setActiveCalendar(allUserCals.get(0).getName());
userRepo.save(currUser);
System.out.println(currUser);
System.out.println(allUserCals);
return "redirect:/view-calendars";
}
......
......@@ -128,10 +128,10 @@ public class EventController {
}
Calendar c1 = calendarRepo.findByName(calName);
Calendar c1 = calendarRepo.findByNameAndUser(calName, currUser);
if(c1 == null)
{
c1 = calendarRepo.findByName(currUser.getActiveCalendar());
c1 = calendarRepo.findByNameAndUser(currUser.getActiveCalendar(), currUser);
}
event.setCalendar(c1);
eventRepo.save(event);
......@@ -140,20 +140,4 @@ public class EventController {
}
@GetMapping(value = "/delete-event/{id}")
public String deleteEventGet(Model models, @PathVariable (name = "id") int eventID)
{
try
{
eventRepo.deleteById(eventID);
}
catch(Exception e)
{
return "redirect:/calendar";
}
return "redirect:/calendar";
}
}
......@@ -190,7 +190,7 @@ public class GraphController {
Calendar chosenCalendar = calendarRepo.findByName(calName);
try
{
ArrayList<Event> allEventsInCal = eventRepo.findAllByCalendar(chosenCalendar);
eventRepo.findAllByCalendar(chosenCalendar);
}
catch(Exception e)
{
......
......@@ -11,5 +11,6 @@ import com.example.calendar.models.User;
@Repository
public interface CalendarRepo extends CrudRepository<Calendar, Integer>{
ArrayList<Calendar> findAllByUser(User user);
Calendar findByNameAndUser(String name, User user);
Calendar findByName(String name);
}
......@@ -50,8 +50,12 @@ public class CalendarValidator implements Validator {
}
User u1 = userRepo.findByUsername(username);
ArrayList<Calendar> allUserCals = calendarRepo.findAllByUser(u1);
if(allUserCals.isEmpty())
{
return;
}
for (Calendar calendar : allUserCals) {
if(calendar.getName().equals(c.getName()))
......
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