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

user can have multiple calendars

parent 4b770530
......@@ -46,8 +46,8 @@ public class CalendarController {
}
User currUser = userRepo.findByUsername(username);
Calendar c1 = calendarRepo.findByUser(currUser);
Calendar c1 = calendarRepo.findAllByUser(currUser).get(0); //TODO RIGHT NOW IT TAKES DEFAULT CALENDAR, IMPLEMENT DROPDOWN.
System.out.println(currUser);
System.out.println(c1);
System.out.println(calendarRepo.findAll());
......@@ -75,7 +75,7 @@ public class CalendarController {
User currUser = userRepo.findByUsername(username);
Calendar c1 = calendarRepo.findByUser(currUser);
Calendar c1 = calendarRepo.findAllByUser(currUser).get(0); //TODO RIGHT NOW IT TAKES DEFAULT CALENDAR, IMPLEMENT DROPDOWN.
//Get Events By Calendar
......@@ -98,6 +98,4 @@ public class CalendarController {
return "calendar";
}
}
package com.example.calendar.controller;
import java.util.ArrayList;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -46,6 +47,20 @@ public class EventController {
public String addNewEventGet(Model models, Event event)
{
List<EventType> allEventTypes = Arrays.asList(EventType.values());
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);
ArrayList<Calendar> allUserCalendars = calendarRepo.findAllByUser(currUser);
models.addAttribute("calendarList", allUserCalendars);
models.addAttribute("eventTypes", allEventTypes);
return "addNewEvent";
}
......@@ -67,7 +82,7 @@ public class EventController {
username = principal.toString();
}
User currUser = userRepo.findByUsername(username);
Calendar c1 = calendarRepo.findByUser(currUser);
Calendar c1 = calendarRepo.findAllByUser(currUser).get(0); //TODO RIGHT NOW IT TAKES DEFAULT CALENDAR, IMPLEMENT DROPDOWN.
event.setCalendar(c1);
eventRepo.save(event);
......
package com.example.calendar.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
......@@ -7,6 +8,7 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import com.example.calendar.models.User;
import com.example.calendar.service.SecurityService;
import com.example.calendar.service.UserService;
......@@ -22,6 +24,7 @@ public class UserController {
@Autowired
private UserValidator userValidator;
@GetMapping("/registration")
public String registration(Model model) {
......@@ -48,14 +51,15 @@ public class UserController {
if (bindingResult.hasErrors()) {
System.out.println("error");
return "registration";
userService.save(userForm);
return "registration";
}
userForm.getCalendar().setName(userForm.getUsername() + " default Calendar");
userForm.getCalendar().setUser(userForm);
userService.save(userForm);
securityService.autoLogin(userForm.getUsername(), userForm.getPasswordConfirm());
return "redirect:/calendar";
}
......
package com.example.calendar.models;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Column;
......@@ -10,8 +8,8 @@ import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
......@@ -23,7 +21,7 @@ public class Calendar {
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID_c")
private int calendar_ID;
@OneToOne
@ManyToOne
@JoinColumn(name="ID_u")
private User user;
@OneToMany(mappedBy = "calendar")
......@@ -50,26 +48,31 @@ public class Calendar {
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Collection<Event> getEvent() {
return event;
}
public void setEvent(Collection<Event> event) {
this.event = event;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCalendar_ID() {
return calendar_ID;
}
@Override
public String toString() {
return "Calendar [calendar_ID=" + calendar_ID + ", user=" + user + ", event=" + event + ", name=" + name + "]";
......
......@@ -58,8 +58,6 @@ public class Event{
//Set & Get
//TODO unit test
public String getName() {
return name;
}
......@@ -84,9 +82,7 @@ public class Event{
//Cannot be previous date
public void setStartDate(LocalDate startDate) {
this.startDate = startDate;
this.startDate = startDate;
}
......@@ -132,35 +128,20 @@ public class Event{
}
public Calendar getCalendar() {
return calendar;
}
public void setCalendar(Calendar calendar) {
this.calendar = calendar;
}
public void setAllDayEvent(boolean isAllDayEvent) {
this.isAllDayEvent = isAllDayEvent;
}
/*Comparator for sorting the list by Student Name*/
public static Comparator<Event> eventDateComp = new Comparator<Event>() {
......@@ -178,15 +159,5 @@ public class Event{
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());
// }
//
}
package com.example.calendar.models;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;
import javax.persistence.CascadeType;
......@@ -9,7 +11,7 @@ import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.springframework.data.annotation.Transient;
......@@ -34,11 +36,15 @@ public class User {
@Column(name = "ID_u")
private int user_ID;
@OneToOne(mappedBy = "user", cascade = {CascadeType.ALL})
private Calendar calendar = new Calendar();
@OneToMany(mappedBy = "user", cascade = {CascadeType.ALL})
private Collection<Calendar> calendar = new ArrayList<Calendar>();
public User() {
Calendar defaultCal = new Calendar();
defaultCal.setUser(this);
calendar.add(defaultCal);
}
public User(String username, String password) {
......@@ -46,7 +52,11 @@ public class User {
setPassword(password);
}
public void addCalendar(Calendar newCal)
{
newCal.setUser(this);
calendar.add(newCal);
}
//Set & get
......@@ -57,62 +67,46 @@ public class User {
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getUser_ID() {
return user_ID;
}
public String getPasswordConfirm() {
return passwordConfirm;
}
public void setPasswordConfirm(String passwordConfirm) {
this.passwordConfirm = passwordConfirm;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
public Calendar getCalendar() {
public Collection<Calendar> getCalendar() {
return calendar;
}
public void setCalendar(Calendar calendar) {
public void setCalendar(Collection<Calendar> calendar) {
this.calendar = calendar;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + ", user_ID=" + user_ID + "]";
return "User [username=" + username + ", password=" + password + ", user_ID=" + user_ID + ", calendar="
+ calendar + "]";
}
}
package com.example.calendar.repo;
import java.util.ArrayList;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
......@@ -8,5 +10,5 @@ import com.example.calendar.models.User;
@Repository
public interface CalendarRepo extends CrudRepository<Calendar, Integer>{
Calendar findByUser(User user);
ArrayList<Calendar> findAllByUser(User user);
}
......@@ -76,7 +76,7 @@ public class EventValidator implements Validator {
username = principal.toString();
}
User currUser = userRepo.findByUsername(username);
Calendar c1 = calendarRepo.findByUser(currUser);
Calendar c1 = calendarRepo.findAllByUser(currUser).get(0); //TODO RIGHT NOW IT TAKES DEFAULT CALENDAR, IMPLEMENT DROPDOWN.
ArrayList<Event> allUserEvents = eventRepo.findAllByCalendar(c1);
......
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