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

Need to fix add event to calendar in database

parent b13abb66
package com.example.calendar.controller;
import java.time.LocalDate;
import java.time.LocalTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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;
@Controller
public class DBController {
@Autowired
UserRepo userRepo;
@Autowired
EventRepo eventRepo;
@Autowired
CalendarRepo calendarRepo;
@GetMapping(value="/ok")
public String test()
{
User u1 = new User("Janis", "parole123");
userRepo.save(u1);
Event e1 = new Event("Test Event", LocalDate.now().plusDays(1), LocalTime.now().plusHours(1), false);
eventRepo.save(e1);
Calendar c1 = new Calendar();
c1.addNewEvent(e1);
calendarRepo.save(c1);
//
System.out.println(userRepo.findAll());
System.out.println(eventRepo.findAll());
// System.out.println(calendarRepo.findAll());
return "ok";
}
......
......@@ -31,39 +31,20 @@ public class Calendar {
private User user;
@OneToMany(mappedBy = "calendar")
private Collection<Event> event;
//TODO all calendar arrayList should not be in Calendar class.
//TODO Do we need eventList? maybe link events&calendar as oneToMany
private ArrayList<Event> eventList = new ArrayList<>();
public Calendar() {
}
public Calendar(ArrayList<Event> eventList) {
setEventList(eventList);
}
public ArrayList<Event> getEventList() {
return eventList;
}
public void setEventList(ArrayList<Event> eventList) {
this.eventList = eventList;
}
//TODO DISCUSS We have 3 different constructors, which one to use here?
public boolean addNewEvent(String name, String description, LocalDate startDate, LocalTime startTime, EventType newEventType, boolean isAllDayEvent)
//TODO verification
public boolean addNewEvent(Event newEvent)
{
Event tempEvent = new Event(name, description, startDate, startTime, isAllDayEvent);
eventList.add(tempEvent);
event.add(newEvent);
return true;
}
//MADE THROUGH CONTROLLER PROBABLY
......@@ -80,7 +61,7 @@ public class Calendar {
if(eventsToDelete.size() == 1) //So only 1 event with the name matches
{
for (Event event : eventList) {
for (Event event : event) {
if(event.equals(eventsToDelete.get(0))) //We find the event and delete it
{
event = null;
......@@ -116,7 +97,7 @@ public class Calendar {
public ArrayList<Event> getEventsByName(String name)
{
ArrayList<Event> allEventsWithEqualName = new ArrayList<>();
for (Event e : eventList) {
for (Event e : event) {
if(e.getName().equals(name))
{
allEventsWithEqualName.add(e);
......@@ -133,10 +114,10 @@ public class Calendar {
if(eventToDelete != null)
{
for (Event e : eventList) {
for (Event e : event) {
if(e.getEvent_ID() == id)
{
eventList.remove(e);
event.remove(e);
}
}
return true;
......@@ -161,7 +142,7 @@ public class Calendar {
public Event getEventsByID(int id)
{
Event eventWithTheID = null;
for (Event e : eventList) {
for (Event e : event) {
if(e.getEvent_ID() == id)
{
eventWithTheID = e;
......@@ -182,7 +163,7 @@ public class Calendar {
public ArrayList<Event> getEventsByDate(LocalDate date)
{
ArrayList<Event> allEventsAtDate = new ArrayList<>();
for (Event e : eventList) {
for (Event e : event) {
if(e.getStartDate().equals(date))
{
allEventsAtDate.add(e);
......@@ -204,7 +185,7 @@ public class Calendar {
public ArrayList<Event> getEventsByEventType(EventType type)
{
ArrayList<Event> allEventsByType = new ArrayList<>();
for (Event e : eventList) {
for (Event e : event) {
if(e.getEventType().equals(type))
{
allEventsByType.add(e);
......@@ -215,8 +196,9 @@ public class Calendar {
@Override
public String toString() {
return "Calendar [calendar_ID=" + calendar_ID + ", eventList=" + eventList + "]";
return "Calendar [calendar_ID=" + calendar_ID + ", eventList=" + event + "]";
}
......
package com.example.calendar.repo;
import org.springframework.data.repository.CrudRepository;
import com.example.calendar.models.Calendar;
public interface CalendarRepo extends CrudRepository<Calendar, Integer>{
}
package com.example.calendar.repo;
import org.springframework.data.repository.CrudRepository;
import com.example.calendar.models.Event;
public interface EventRepo extends CrudRepository<Event, Integer> {
}
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