Commit 71d71d65 authored by s7_spruge_k's avatar s7_spruge_k
Browse files

Merge branch 'master' into 'spring-security'

Master

See merge request s7_spruge_k/calendar!16
parents a2bd07d0 026932ac
No preview for this file type
No preview for this file type
......@@ -50,6 +50,10 @@
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
......
package com.example.calendar.controller;
import java.time.LocalDate;
import java.time.LocalTime;
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;
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;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
@Controller
public class CalendarController {
@Autowired
UserRepo userRepo;
@Autowired
CalendarRepo calendarRepo;
@Autowired
EventRepo eventRepo;
@GetMapping(value = "/calendar")
public String calendarGet(Model models)
{
//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);
Calendar c1 = calendarRepo.findByUser(currUser);
//Get Events By Calendar
ArrayList<Event> allEvents = eventRepo.findAllByCalendar(c1);
JsonArray eventArray = new JsonArray();
for (int i = 0; i < allEvents.size(); i++) {
JsonObject eventJson = new JsonObject();
eventJson.addProperty("title", allEvents.get(i).getName());
eventJson.addProperty("start", allEvents.get(i).getStartDate() + "T" + allEvents.get(i).getStartTime());
eventArray.add(eventJson);
}
models.addAttribute("currentTime", LocalTime.now().toString());
models.addAttribute("currentDate", LocalDate.now().toString());
models.addAttribute("currentMonth", LocalDate.now().getMonth());
models.addAttribute("currentDay", LocalDate.now().getDayOfWeek());
models.addAttribute("currentUser", currUser.getUsername());
models.addAttribute("allEvents", eventArray);
return "calendar";
}
}
......@@ -4,26 +4,38 @@ 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;
@Controller
public class eventController {
public class EventController {
@Autowired
EventRepo eventRepo;
@GetMapping(value = "/addNewEvent")
@Autowired
UserRepo userRepo;
@Autowired
CalendarRepo calendarRepo;
@GetMapping(value = "/add-new-event")
public String addNewEventGet(Model models, Event event)
{
//TODO Var pievienot, ka default date radaas shodienas datums, lidzigi ar laiku.
......@@ -33,14 +45,30 @@ public class eventController {
return "addNewEvent";
}
@PostMapping(value = "/addNewEvent")
@PostMapping(value = "/add-new-event")
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";
}
}
......@@ -2,6 +2,7 @@ package com.example.calendar.models;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Comparator;
import javax.persistence.Column;
import javax.persistence.Entity;
......@@ -16,7 +17,7 @@ import org.springframework.format.annotation.DateTimeFormat;
@Entity
@Table(name = "EventTable")
public class Event {
public class Event{
@Column(name = "Name")
private String name;
......@@ -110,17 +111,8 @@ public class 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 +142,8 @@ public class Event {
}
public void setStartTime(LocalTime startTime) {
if(startTime.isAfter(LocalTime.now()))
this.startTime = startTime;
else
System.out.println("Error!!!: Time in the past.");
}
@Override
......@@ -168,6 +157,33 @@ public class Event {
super();
}
/*Comparator for sorting the list by Student Name*/
public static Comparator<Event> eventDateComp = new Comparator<Event>() {
public int compare(Event e1, Event e2) {
//ascending order
return e1.getStartDate().compareTo(e2.getStartDate());
}};
/*Comparator for sorting the list by Student Name*/
public static Comparator<Event> eventTimeComp = new Comparator<Event>() {
public int compare(Event e1, Event e2) {
//ascending order
return e1.getStartTime().compareTo(e2.getStartTime());
}};
// @Override
// public int compareTo(Event o) {
//
// if (getStartTime() == null || o.getStartTime() == null) {
// return 0;
// }
// return getStartTime().compareTo(o.getStartTime());
// }
//
}
......@@ -4,8 +4,9 @@ import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.example.calendar.models.Calendar;
import com.example.calendar.models.User;
@Repository
public interface CalendarRepo extends CrudRepository<Calendar, Integer>{
Calendar findByUser(User user);
}
package com.example.calendar.repo;
import java.time.LocalDate;
import java.util.ArrayList;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.example.calendar.models.Calendar;
import com.example.calendar.models.Event;
@Repository
public interface EventRepo extends CrudRepository<Event, Integer> {
ArrayList<Event> findAllByCalendar(Calendar calendar);
ArrayList<Event> findAllByStartDate(LocalDate startDate);
}
......@@ -5,7 +5,7 @@
</head>
<h2>Add New Event</h2>
<body>
<form action="#" th:action="@{addNewEvent}" th:object="${event}" method="post">
<form action="#" th:action="@{/add-new-event}" th:object="${event}" method="post">
<table>
<tr>
<td>Title:</td>
......
<!--<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello Test</title>
</head>
<h2 th:text = "'Hello, ' + ${currentUser}"></h2>
<body>
<table>
<tr>
<td th:text = "'Date: ' + ${currentDate}"></td>
<td th:text = "'Time ' + ${currentTime}"></td>
<td th:text = "'Month ' + ${currentMonth}"></td>
<td th:text = "'Day of the week ' + currentDay"></td>
</tr>
<tr th:each="temp: ${allEvents}">
<td th:text = "'Event ' + ${temp.getName()}"/>
<td th:text = "'Date: ' + ${temp.getStartDate()}"></td>
<td th:text = "'Time: ' + ${temp.getStartTime()}"></td>
</tr>
</table>
</body>
</html>
-->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<link href="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.1.0/fullcalendar.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.7/semantic.min.css" rel="stylesheet">
</head>
<body>
<style>
.fc-content{
font-size: 1.5em;
}
.fc-event{
background-color:#007bff;
}
</style>
<br/>
<div class="ui container">
<div class="ui grid">
<div class="ui sixteen column">
<div id="calendar"></div>
</div>
</div>
</div>
</div>
<input type="hidden" id="events" th:value="${allEvents}"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.7/semantic.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.1.0/fullcalendar.min.js"></script>
<script th:inline="javascript">
$(document).ready(function() {
var all = JSON.parse($("#events").val());
$('#calendar').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,basicWeek,basicDay'
},
navLinks: true, // can click day/week names to navigate views
editable: false,
eventLimit: true, // allow "more" link when too many events
timeFormat: 'H(:mm)',
events: all
});
});
</script>
</body>
</html>
......@@ -9,7 +9,6 @@
<title>Authentification</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link href="/resources/css/common.css" rel="stylesheet">
</head>
<body>
......@@ -23,7 +22,7 @@
<input name="username" type="text" class="form-control" placeholder="Username"
autofocus="true"/>
<br>
<input name="password" type="password" class="form-control" placeholder="Password"/>
<input name="password" type="password" class="form-control" placeholder="Password"/><br>
<div class="alert alert-danger" th:if="${error}">
<p th:text="${error}"></p>
......@@ -31,7 +30,7 @@
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
<br><button class="btn btn-lg btn-primary btn-block" type="submit">Log In</button>
<button class="btn btn-lg btn-primary btn-block" type="submit">Log In</button>
<div class="alert alert-success" th:if="${message}">
<p th:text="${message}"></p>
......@@ -44,7 +43,6 @@
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="/resources/js/bootstrap.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
......
......@@ -9,7 +9,6 @@
<title>Create an account</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link href="@{/resources/css/common.css}" rel="stylesheet">
</head>
<body>
......@@ -25,12 +24,12 @@
</div>
<div class="form-group" th:classappend="${#fields.hasErrors('*')} ? 'has-error' : ''">
<input name="password" type="text" class="form-control" placeholder="Password" autofocus="true"/><br>
<input name="password" type="password" class="form-control" placeholder="Password" autofocus="true"/><br>
<p class="alert alert-danger" th:if="${#fields.hasErrors('password')}" th:errors="*{password}"></p>
</div>
<div class="form-group" th:classappend="${#fields.hasErrors('*')} ? 'has-error' : ''">
<input name="passwordConfirm" type="text" class="form-control" placeholder="Confirm your password" autofocus="true"/><br>
<input name="passwordConfirm" type="password" class="form-control" placeholder="Confirm your password" autofocus="true"/><br>
<p class="alert alert-danger" th:if="${#fields.hasErrors('passwordConfirm')}" th:errors="*{passwordConfirm}"></p>
</div>
......@@ -43,7 +42,6 @@
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="@{/resources/js/bootstrap.min.js}"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
......
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