Commit 723d6dcf authored by s7_spruge_k's avatar s7_spruge_k
Browse files

nekas pagaidam nestrada

parent 0e81a085
......@@ -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);
......@@ -187,9 +188,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().getNano();
}
//If calendar With the name exists
if(calendarRepo.findByNameAndUser(newName, currUser) != null)
{
newName+="(1)";
}
System.out.println(newName);
Calendar c1 = calendarRepo.findByNameAndUser(currUser.getActiveCalendar(), currUser);
System.out.println(c1);
if(c1.getName().equals(currUser.getActiveCalendar()))
{
currUser.setActiveCalendar(newName);
......
......@@ -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