Commit 814bf788 authored by s7_spruge_k's avatar s7_spruge_k
Browse files

small code fixes

parent e8c2b40f
......@@ -44,7 +44,7 @@ public class CalendarController {
private CalendarValidator calendarValidator;
//Helper method for finding the current User -> Used very often
public User findCurrentUser()
{
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
......@@ -68,6 +68,7 @@ public class CalendarController {
{
User currUser = findCurrentUser();
//If user doesnt have calendar - redirected to create
try
{
......@@ -120,7 +121,9 @@ public class CalendarController {
Event e1 = eventRepo.findByCalendarAndStartDateAndStartTime(c1, localDate, localTime);
//Simple validations
//--Validations--
String tempName = event.getName();
if(tempName.equals(""))
{
......@@ -227,6 +230,7 @@ public class CalendarController {
}
ArrayList<Calendar> allUserCalendars = calendarRepo.findAllByUser(currUser);
JsonArray calendarArray = new JsonArray();
......@@ -274,6 +278,8 @@ public class CalendarController {
return "redirect:/view-calendars";
}
@PostMapping(value="/view-calendars", params="action=delete")
public String deleteCalendar(Model model, String calName){
......@@ -294,6 +300,8 @@ public class CalendarController {
return "redirect:/view-calendars";
}
@PostMapping(value="/view-calendars", params="action=view")
public String viewCalendar(Model model, String calName){
User currUser = findCurrentUser();
......
......@@ -101,7 +101,8 @@ public class EventController {
{
//Gets all the calendars and eventTypes again.
//Gets all the calendars and eventTypes again and sends them to view.
//--
List<EventType> allEventTypes = Arrays.asList(EventType.values());
User currUser = findCurrentUser();
......@@ -117,9 +118,12 @@ public class EventController {
models.addAttribute("calendarList", calendarArray);
models.addAttribute("eventTypes", allEventTypes);
//--
eventValidator.validate(event, bindingResult);
if (bindingResult.hasErrors()) {
......
......@@ -33,6 +33,7 @@ public class GraphController {
@Autowired
EventRepo eventRepo;
public User findCurrentUser()
{
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
......@@ -147,12 +148,15 @@ public class GraphController {
JsonArray calendarArray = new JsonArray();
for (int i = 0; i < allUserCalendars.size(); i++) {
JsonObject calendarJson = new JsonObject();
calendarJson.addProperty("title", allUserCalendars.get(i).getName());
calendarArray.add(calendarJson);
}
ArrayList<String> graphTypes = new ArrayList<>();
graphTypes.add("bar");
graphTypes.add("doughnut");
......@@ -160,10 +164,13 @@ public class GraphController {
graphTypes.add("line");
ArrayList<String> analysisTypes = new ArrayList<>();
analysisTypes.add("Analysis by day");
analysisTypes.add("Analysis by month");
model.addAttribute("analysisTypes", analysisTypes);
model.addAttribute("graphTypes", graphTypes);
model.addAttribute("calendarList", calendarArray);
......@@ -176,7 +183,6 @@ public class GraphController {
statHelper.setChosenCalendar(calName);
User currUser = findCurrentUser();
try
{
......@@ -187,14 +193,18 @@ public class GraphController {
return "redirect:/add-new-calendar";
}
Calendar chosenCalendar = calendarRepo.findByName(calName);
Calendar chosenCalendar = calendarRepo.findByNameAndUser(calName, currUser);
//Checks if calendar has events
try
{
eventRepo.findAllByCalendar(chosenCalendar);
}
catch(Exception e)
{
System.out.println("Nav event kalendara");
return "redirect:/choose-cal";
}
......@@ -206,6 +216,8 @@ public class GraphController {
ArrayList<Integer> eventCountInMonths = new ArrayList<>();
LocalDate now = LocalDate.now();
for (int i = 1; i <= 12; i++) {
int temp = 0;
......@@ -272,13 +284,15 @@ public class GraphController {
eventJson.addProperty("eventCount", eventCountThisMonth.get(i));
eventArray.add(eventJson);
}
String title = "Event count this month by days: " + statHelper.getChosenCalendar();
models.addAttribute("name", title);
models.addAttribute("allEvents", eventArray);
}
models.addAttribute("graphType", statHelper.getGraphMethod());
return "stats";
}
......
......@@ -37,6 +37,7 @@ public class UserController {
username = principal.toString();
}
//Doesn't allow authorised users to register again.
if(username != "anonymousUser")
return "redirect:/calendar";
......@@ -49,9 +50,7 @@ public class UserController {
public String registration(@ModelAttribute("userForm") User userForm, BindingResult bindingResult) {
userValidator.validate(userForm, bindingResult);
if (bindingResult.hasErrors()) {
System.out.println("error");
if (bindingResult.hasErrors()) {
return "registration";
}
......@@ -73,6 +72,7 @@ public class UserController {
username = principal.toString();
}
//Doesn't let authorised users log in again
if(username != "anonymousUser")
return "redirect:/calendar";
......
......@@ -21,9 +21,11 @@ public class Calendar {
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID_c")
private int calendar_ID;
@ManyToOne
@JoinColumn(name="ID_u")
private User user;
@OneToMany(mappedBy = "calendar")
private Collection<Event> event;
......
......@@ -2,7 +2,6 @@ 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;
......@@ -21,6 +20,7 @@ public class Event{
@Column(name = "Name")
private String name;
@Column(name = "Description")
private String description;
......@@ -31,12 +31,15 @@ public class Event{
@DateTimeFormat(pattern = "HH:mm")
@Column(name = "StartTime")
private LocalTime startTime;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID_e")
private int event_ID;
@Column(name = "EventType")
private EventType eventType;
@ManyToOne
@JoinColumn(name = "ID_c")
private Calendar calendar;
......@@ -126,22 +129,5 @@ public class Event{
this.calendar = calendar;
}
/*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());
}};
}
......@@ -22,6 +22,7 @@ public class User {
@Column(name = "Username")
private String username;
@Column(name = "Password")
private String password;
......
......@@ -27,6 +27,7 @@ public class UserValidator implements Validator {
if (user.getUsername().length() < 6 || user.getUsername().length() > 32) {
errors.rejectValue("username", "Size.userForm.username");
}
if (userService.findByUsername(user.getUsername()) != null) {
errors.rejectValue("username", "Duplicate.userForm.username");
}
......
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