diff --git a/src/main/java/com/cloudcraftgaming/discal/web/endpoints/v1/EventEndpoint.java b/src/main/java/com/cloudcraftgaming/discal/web/endpoints/v1/EventEndpoint.java index 75c87dda..8a713f6d 100644 --- a/src/main/java/com/cloudcraftgaming/discal/web/endpoints/v1/EventEndpoint.java +++ b/src/main/java/com/cloudcraftgaming/discal/web/endpoints/v1/EventEndpoint.java @@ -10,19 +10,24 @@ import com.cloudcraftgaming.discal.api.object.event.Recurrence; import com.cloudcraftgaming.discal.api.object.web.WebGuild; import com.cloudcraftgaming.discal.api.utils.EventUtils; import com.cloudcraftgaming.discal.api.utils.ExceptionHandler; +import com.cloudcraftgaming.discal.api.utils.ImageUtils; import com.cloudcraftgaming.discal.web.handler.DiscordAccountHandler; +import com.cloudcraftgaming.discal.web.utils.ResponseUtils; import com.google.api.client.util.DateTime; import com.google.api.services.calendar.Calendar; import com.google.api.services.calendar.model.Event; +import com.google.api.services.calendar.model.EventDateTime; import com.google.api.services.calendar.model.Events; import org.json.JSONObject; import spark.Request; import spark.Response; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Map; +//TODO: Make endpoints compatible with both dashboard && general API usage. public class EventEndpoint { public static String getEventsForMonth(Request request, Response response) { JSONObject requestBody = new JSONObject(request.body()); @@ -164,14 +169,80 @@ public class EventEndpoint { } public static String updateEvent(Request request, Response response) { - JSONObject requestBody = new JSONObject(request.body()); - String eventId = requestBody.getString("id"); + JSONObject body = new JSONObject(request.body()); + String eventId = body.getString("id"); Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id()); WebGuild g = (WebGuild) m.get("selected"); g.setSettings(DatabaseManager.getManager().getSettings(Long.valueOf(g.getId()))); - //TODO: Okay, time to update the event + //Okay, time to update the event + try { + Calendar service; + if (g.getSettings().useExternalCalendar()) { + service = CalendarAuth.getCalendarService(g.getSettings()); + } else { + service = CalendarAuth.getCalendarService(); + } + CalendarData calendarData = DatabaseManager.getManager().getMainCalendar(Long.valueOf(g.getId())); + com.google.api.services.calendar.model.Calendar cal = service.calendars().get(calendarData.getCalendarId()).execute(); + + Event event = new Event(); + event.setId(eventId); + event.setVisibility("public"); + event.setSummary(body.getString("summary")); + event.setDescription(body.getString("description")); + + EventDateTime start = new EventDateTime(); + start.setDateTime(new DateTime(body.getLong("epochStart"))); + event.setStart(start.setTimeZone(cal.getTimeZone())); + + EventDateTime end = new EventDateTime(); + end.setDateTime(new DateTime(body.getLong("epochEnd"))); + event.setEnd(end.setTimeZone(cal.getTimeZone())); + + if (!body.getString("color").equalsIgnoreCase("NONE")) { + event.setColorId(EventColor.fromNameOrHexOrID(body.getString("color")).getId().toString()); + } + + if (!body.getString("location").equalsIgnoreCase("") || !body.getString("location").equalsIgnoreCase("N/a")) { + event.setLocation(body.getString("location")); + } + + if (body.getBoolean("enableRecurrence")) { + //Handle recur + Recurrence recurrence = new Recurrence(); + recurrence.setFrequency(EventFrequency.fromValue(body.getString("frequency"))); + recurrence.setCount(body.getInt("count")); + recurrence.setInterval(body.getInt("interval")); + + String[] rr = new String[]{recurrence.toRRule()}; + event.setRecurrence(Arrays.asList(rr)); + } + + EventData ed = new EventData(Long.valueOf(g.getId())); + if (!body.getString("image").equalsIgnoreCase("") && ImageUtils.validate(body.getString("image"))) { + ed.setImageLink(body.getString("image")); + ed.setEventId(eventId); + ed.setEventEnd(event.getEnd().getDateTime().getValue()); + } + + if (ed.shouldBeSaved()) { + DatabaseManager.getManager().updateEventData(ed); + } + + service.events().update(calendarData.getCalendarId(), eventId, event).execute(); + + response.status(200); + response.body(ResponseUtils.getJsonResponseMessage("Successfully updated event!")); + + } catch (Exception e) { + ExceptionHandler.sendException(null, "[WEB] Failed to update event!", e, EventEndpoint.class); + e.printStackTrace(); + + response.status(500); + response.body(ResponseUtils.getJsonResponseMessage("Failed to update event!")); + } return response.body(); } diff --git a/src/main/resources/web/public/scripts/dashboard/calendar.js b/src/main/resources/web/public/scripts/dashboard/calendar.js index c3e23815..2fc2317e 100644 --- a/src/main/resources/web/public/scripts/dashboard/calendar.js +++ b/src/main/resources/web/public/scripts/dashboard/calendar.js @@ -232,7 +232,6 @@ function getEventsForSelectedDate() { modalBody.className = "modal-body"; modalCon.appendChild(modalBody); - //TODO: Don't make this POST, intercept and send it in JSON!!!!! var form = document.createElement("form"); modalBody.appendChild(form); @@ -371,7 +370,6 @@ function getEventsForSelectedDate() { form.appendChild(document.createElement("br")); form.appendChild(document.createElement("br")); - //TODO: Make this a proper dropdown //Frequency var frequencyLabel = document.createElement("label"); frequencyLabel.innerHTML = "Recurrence - Frequency"; @@ -449,12 +447,19 @@ function getEventsForSelectedDate() { form.appendChild(document.createElement("br")); form.appendChild(document.createElement("br")); - //ID for API + //ID (readonly) for API + var idLabel = document.createElement("label"); + idLabel.innerHTML = "Event ID (Changing this does nothing)"; + idLabel.appendChild(document.createElement("br")); + form.appendChild(idLabel); var hiddenId = document.createElement("input"); - hiddenId.type = "hidden"; + hiddenId.type = "text"; hiddenId.name = "id"; - hiddenId.innerHTML = event.id; - form.appendChild(hiddenId); + hiddenId.value = event.id; + hiddenId.id = "editId-" + event.id; + idLabel.appendChild(hiddenId); + form.appendChild(document.createElement("br")); + form.appendChild(document.createElement("br")); //Submit button var submit = document.createElement("button"); @@ -462,7 +467,7 @@ function getEventsForSelectedDate() { submit.type = "button"; submit.id = "editsubmit-" + event.id; submit.innerHTML = "Update Event!"; - submit.onclick = function (ev) { + submit.onclick = function (ignore) { updateEvent(this.id); }; form.appendChild(submit); @@ -508,7 +513,16 @@ function updateEvent(editSubmitId) { var eventId = editSubmitId.split("-")[1]; //TODO: Handle date/times - //TODO: Handle whether or not there is recurrence. + var startTimeString = document.getElementById("editStartTime-" + eventId).value.split(":"); + var startDate = document.getElementById("editStartDate-" + eventId).valueAsDate; + + var endTimeString = document.getElementById("editEndTime-" + eventId).value.split(":"); + var endDate = document.getElementById("editEndDate-" + eventId).valueAsDate; + + startDate.setHours(parseInt(startTimeString[0]), parseInt(startTimeString[1]), 0, 0); + + endDate.setHours(parseInt(endTimeString[0]), parseInt(endTimeString[1]), 0, 0); + var colorElement = document.getElementById("editColor-" + eventId); if (document.getElementById("editEnableRecur-" + eventId) !== null) { var freqElement = document.getElementById("editFrequency-" + eventId); @@ -522,7 +536,9 @@ function updateEvent(editSubmitId) { "enableRecurrence": document.getElementById("editEnableRecur-" + eventId).checked, "frequency": freqElement.options[freqElement.selectedIndex].value, "count": document.getElementById("editCount-" + eventId).valueAsNumber, - "interval": document.getElementById("editInterval-" + eventId).valueAsNumber + "interval": document.getElementById("editInterval-" + eventId).valueAsNumber, + "epochStart": startDate.getTime() + 86400000, + "epochEnd": endDate.getTime() + 86400000 }; } else { bodyRaw = { @@ -535,12 +551,12 @@ function updateEvent(editSubmitId) { "enableRecurrence": false, "frequency": "DAILY", "count": -1, - "interval": 1 + "interval": 1, + "epochStart": startDate.getTime(), + "epochEnd": endDate.getTime() }; } - alert(JSON.stringify(bodyRaw)); - var q = $.post("/api/v1/events/update", JSON.stringify(bodyRaw), function (response) { //TODO: close modal showSnackbar("Event successfully updated!"); @@ -560,8 +576,7 @@ function deleteEvent(clickedId) { var eventId = clickedId.replace("delete-", ""); var bodyRaw = {"id": eventId}; - var q = $.post("/api/v1/events/delete", JSON.stringify(bodyRaw), function (response) { - var obj = JSON.parse(response); + var q = $.post("/api/v1/events/delete", JSON.stringify(bodyRaw), function (ignore) { showSnackbar("Successfully deleted event!");