Commit 01837289 authored by s7_spruge_k's avatar s7_spruge_k
Browse files

Finished basic month controller

parent 0da819b4
......@@ -38,23 +38,79 @@ public class CalendarController {
models.addAttribute("currentTime", LocalTime.now());
models.addAttribute("currentDate", LocalDate.now());
models.addAttribute("currentMonth", LocalDate.now().getMonth());
models.addAttribute("currentDay", LocalDate.now().getDayOfWeek());
//1. Get currently logged in 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);
//2. Get Calendar By User
Calendar c1 = calendarRepo.findByUser(currUser);
//3. Get Events By Calendar
ArrayList<Event> allEvents = eventRepo.findAllByCalendar(c1);
int totalDaysInCurrentMonth = LocalDate.now().lengthOfMonth();
int currentDayByIndex = LocalDate.now().getDayOfMonth();
int numberOfPreviousDaysNeedToGet = currentDayByIndex - 1;
int numberOfDaysAheadNeedToGet = totalDaysInCurrentMonth - currentDayByIndex;
//Sakumu un beigu datums pashreizeejam menesim.
LocalDate monthStartingDate = LocalDate.now().minusDays(numberOfPreviousDaysNeedToGet);
LocalDate monthEndingDate = LocalDate.now().plusDays(numberOfDaysAheadNeedToGet);
ArrayList<Event> monthEvents = new ArrayList<>();
//Get events in current week.
if(allEvents.isEmpty())
System.out.println("No events");
else
{
for (Event e : allEvents) {
LocalDate temp = e.getStartDate();
if(temp == null)
continue;
if(temp.isBefore(monthEndingDate.plusDays(1)) && temp.isAfter(monthStartingDate.minusDays(1)))
monthEvents.add(e);
}
}
Collections.sort(monthEvents, Event.eventDateComp);
//TODO If the dates are the same, sort By Time!!!!!!!!!!!!!!!!!
models.addAttribute("monthStartingDate", monthStartingDate);
models.addAttribute("monthEndingDate", monthEndingDate);
models.addAttribute("currentUser", currUser.getUsername());
models.addAttribute("monthEvents", monthEvents);
//TODO how to pass User?
return "monthView";
}
@GetMapping(value = "/week-view")
public String weekViewGet(Model models)
{
models.addAttribute("currentTime", LocalTime.now());
models.addAttribute("currentDate", LocalDate.now());
models.addAttribute("currentMonth", LocalDate.now().getMonth());
models.addAttribute("currentDay", LocalDate.now().getDayOfWeek());
//1. Get currently logged in user
//TODO Currently logged in user does not work.
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username = "";
......
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>This Month</title>
</head>
<h2 th:text = "'Hello, ' + ${currentUser}"></h2>
<body>
<table>
<tr>
<td th:text = "'Date: ' + ${currentDate}"></td>
<td th:text = "'Month start: ' + ${monthStartingDate}"></td>
<td th:text = "'Month end: ' + ${monthEndingDate}"></td>
</tr>
<tr th:each="temp: ${monthEvents}">
<td th:text = "'Event ' + ${temp.getName()}"/>
<td th:text = "'Date: ' + ${temp.getStartDate()}"></td>
<td th:text = "'Time: ' + ${temp.getStartTime()}"></td>
</tr>
</table>
</body>
</html>
\ 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