Commit 0e291ec3 authored by s7_spruge_k's avatar s7_spruge_k
Browse files

Merge branch 'calendar-controller' into 'master'

Calendar controller

See merge request s7_spruge_k/calendar!13
parents 99e7ad0d 86740e12
......@@ -6,6 +6,8 @@ import java.util.ArrayList;
import java.util.Collections;
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.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
......@@ -13,6 +15,7 @@ import org.springframework.web.bind.annotation.PostMapping;
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;
......@@ -58,30 +61,51 @@ public class CalendarController {
models.addAttribute("currentDate", LocalDate.now());
models.addAttribute("currentMonth", LocalDate.now().getMonth());
models.addAttribute("currentDay", LocalDate.now().getDayOfWeek());
//TODO how to pass User?
//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> todayEvents = eventRepo.findAllByCalendar(c1);
ArrayList<Event> todayEventsConfirmed = new ArrayList<>();
if(todayEvents.isEmpty())
System.out.println("No events current date");
else
{
//4. Get Events By currentDay.
for (Event e : todayEvents) {
if(e.getStartDate() == null || e.getStartTime() == null)
{
continue;
}
if(e.getStartDate().equals(LocalDate.now()))
todayEventsConfirmed.add(e);
}
}
System.out.println(todayEventsConfirmed);
//Vajag sakartot hronologiski pec laikiem, lai vieglak izmantotu skataa.
//event klasee ir compareTo, kas salidzina pec laikiem
Collections.sort(todayEvents);
// //1. Get currently logged in user
//
//
// //2. Get Calendar By User
// Calendar c1 = calendarRepo.findByUser(user);
//
//
// //3. Get Events By Calendar
// ArrayList<Event> todayEvents = eventRepo.findAllByCalendar(c1);
//
// //4. Get Events By currentDay.
// for (Event e : todayEvents) {
// if(!e.getStartDate().equals(LocalDate.now()))
// todayEvents.remove(e); //Neesmu parliecinats vai shitaa strada, ja nu kas var veidot jaunu arrayList
// } //Kura uzglaba vertibas
//
// //Vajag sakartot hronologiski pec laikiem, lai vieglak izmantotu skataa.
// //event klasee ir compareTo, kas salidzina pec laikiem
// Collections.sort(todayEvents);
//
// models.addAttribute("todayEvents", todayEvents);
models.addAttribute("currentUser", currUser.getUsername());
models.addAttribute("todayEvents", todayEventsConfirmed);
return "dayView";
}
......
......@@ -4,14 +4,20 @@ import java.util.Arrays;
import java.util.EnumSet;
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.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.example.calendar.models.Calendar;
import com.example.calendar.models.Event;
import com.example.calendar.models.EventType;
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 java.util.List;
......@@ -23,6 +29,12 @@ public class EventController {
@Autowired
EventRepo eventRepo;
@Autowired
UserRepo userRepo;
@Autowired
CalendarRepo calendarRepo;
@GetMapping(value = "/add-new-event")
public String addNewEventGet(Model models, Event event)
{
......@@ -37,10 +49,26 @@ public class EventController {
public String addNewEventPost(Event event)
{
//TODO japievieno events konkretajam calendaram kuraa ir user
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username = "";
if (principal instanceof UserDetails) {
username = ((UserDetails)principal).getUsername();
} else {
username = principal.toString();
}
//TODO CAnnot add empty date / time
User currUser = userRepo.findByUsername(username);
Calendar c1 = calendarRepo.findByUser(currUser);
System.out.println(event);
// c1.addNewEvent(event);
//calendarRepo.save(c1);
eventRepo.save(event);
return "redirect:/ok";
return "redirect:/day-view";
}
}
......@@ -110,17 +110,8 @@ public class Event implements Comparable<Event>{
//Cannot be previous date
public void setStartDate(LocalDate startDate) {
LocalDate currentDate = LocalDate.now();
if(startDate.isAfter(currentDate))
{
this.startDate = startDate; //startDate is after currentDate
}
else
{
//TODO What to do when incorrect date entered?
System.out.println("ERROR!!!!: StartDate is before currentDate");
}
this.startDate = startDate;
}
......@@ -150,11 +141,8 @@ public class Event implements Comparable<Event>{
}
public void setStartTime(LocalTime startTime) {
if(startTime.isAfter(LocalTime.now()))
this.startTime = startTime;
else
System.out.println("Error!!!: Time in the past.");
}
@Override
......
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Today Date</title>
</head>
<h2 th:text = "'Hello, ' + ${currentUser}"></h2>
<body>
<table>
<tr>
<td th:text = "'Date: ' + ${currentDate}"></td>
<td th:text = "'Today is: ' + ${currentDay} + 'of ' + ${currentMonth}"></td>
<td th:text = "'The time is: ' + ${currentTime}"></td>
</tr>
<tr th:each="temp: ${todayEvents}">
<td th:text = "'Event ' + ${temp.getName()}"/>
<td th:text = "'Date: ' + ${temp.getStartDate()}"></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