Commit 688491f4 authored by s7_spruge_k's avatar s7_spruge_k
Browse files

fixed add-new-event valdiations

parent a63b4187
......@@ -2,7 +2,6 @@ package com.example.calendar.controller;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
......@@ -59,20 +58,7 @@ public class CalendarController {
return currUser;
}
@GetMapping("/test")
public String testGet() {
User currUser = findCurrentUser();
ArrayList<Calendar> c1 = calendarRepo.findAllByUser(currUser); //TODO RIGHT NOW IT TAKES DEFAULT CALENDAR, IMPLEMENT DROPDOWN.
System.out.println(currUser);
System.out.println(c1);
return "ok";
}
@GetMapping({"/", "/calendar"})
......@@ -137,6 +123,7 @@ public class CalendarController {
e1.setStartTime(event.getStartTime());
e1.setEventType(event.getEventType());
eventRepo.save(e1);
return "redirect:/calendar";
}
......
......@@ -98,116 +98,63 @@ public class EventController {
}
@PostMapping(value = "/add-new-event")
public String addNewEventPost(@Valid Event event, BindingResult bindingResult, String calName)
public String addNewEventPost(@Valid Event event, BindingResult bindingResult, String calName, Model models)
{
List<EventType> allEventTypes = Arrays.asList(EventType.values());
User currUser = findCurrentUser();
System.out.println(calName);
eventValidator.validate(event, bindingResult);
if (bindingResult.hasErrors()) {
return "redirect:/add-new-event";
}
Calendar c1 = calendarRepo.findByName(calName);
System.out.println(event);
event.setCalendar(c1);
eventRepo.save(event);
return "redirect:/calendar";
}
@GetMapping(value = "/view-event/{id}")
public String viewEventGet(Model models, @PathVariable (name = "id") int eventID)
{
// User currUser = findCurrentUser();
// Calendar currCal = calendarRepo.findAllByUser(currUser).get(currUser.getActiveCalendar());
//If user doesnt have calendar - redirected to create
try
{
Event currEvent = eventRepo.findById(eventID).get();
calendarRepo.findAllByUser(currUser).get(0);
}
catch(Exception e)
{
return "redirect:/calendar";
return "redirect:/add-new-calendar";
}
Event currEvent = eventRepo.findById(eventID).get();
models.addAttribute("currentEvent", currEvent);
return "viewEvent";
}
@GetMapping(value = "view-event-edit/{id}")
public String viewEventEditGet(Event event, Model models, @PathVariable (name = "id") int eventID)
{
ArrayList<Calendar> allUserCalendars = calendarRepo.findAllByUser(currUser);
JsonArray calendarArray = new JsonArray();
try
{
Event currEvent = eventRepo.findById(eventID).get();
}
catch(Exception e)
{
return "redirect:/calendar";
for (int i = 0; i < allUserCalendars.size(); i++) {
JsonObject calendarJson = new JsonObject();
calendarJson.addProperty("title", allUserCalendars.get(i).getName());
calendarArray.add(calendarJson);
}
Event currEvent = eventRepo.findById(eventID).get();
models.addAttribute("calendarList", calendarArray);
models.addAttribute("eventTypes", allEventTypes);
models.addAttribute("currentEvent", currEvent);
return "viewEventEdit";
}
@PostMapping(value = "view-event-edit/{id}")
public String viewEventEditPost(Event event, Model models, @PathVariable (name = "id") int eventID)
{
//Ideja - Tiek izveidots jauns event events, un izdzests vecais
Event oldEvent = eventRepo.findById(eventID).get();
//TODO new event field validations
//TODO change calendar
//If new event name empty - keep previous
if(event.getName() == "")
{
event.setName(oldEvent.getName());
}
if(event.getDescription() == "")
{
event.setDescription(oldEvent.getDescription());
}
if(event.getStartDate() == null)
{
event.setStartDate(oldEvent.getStartDate());
}
if(event.getStartTime() == null)
{
event.setStartTime(oldEvent.getStartTime());
}
if(event.getEventType().equals(null))
{
event.setEventType(oldEvent.getEventType());
}
User currUser = findCurrentUser();
Calendar c1 = calendarRepo.findByName(currUser.getActiveCalendar());
event.setCalendar(c1);
eventRepo.save(event);
eventRepo.deleteById(eventID);
eventValidator.validate(event, bindingResult);
return "redirect:/view-event/" + eventID;
if (bindingResult.hasErrors()) {
return "addNewEvent";
}
Calendar c1 = calendarRepo.findByName(calName);
if(c1 == null)
{
c1 = calendarRepo.findByName(currUser.getActiveCalendar());
}
event.setCalendar(c1);
eventRepo.save(event);
return "redirect:/calendar";
}
@GetMapping(value = "/delete-event/{id}")
public String deleteEventGet(Model models, @PathVariable (name = "id") int eventID)
{
try
{
//Vai izpildaas, ja ir try blokā??????
eventRepo.deleteById(eventID);
}
......@@ -215,8 +162,7 @@ public class EventController {
{
return "redirect:/calendar";
}
//eventRepo.deleteById(eventID);
return "redirect:/calendar";
}
......
......@@ -3,8 +3,8 @@
<head>
<meta charset="utf-8">
<title>Add an Event</title>
<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">
<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">
<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">
</head>
......
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