public HttpResponseMessage Update(int Id, TimeBlockUpdateRequest updateRequest) { if (updateRequest == null) { ModelState.AddModelError("", "No data supplied!"); } if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } if (Id == updateRequest.Id) { calendarService.UpdateTimeBlock(updateRequest); int UserId = User.Identity.GetId().Value; Task.Run(() => { UserConnectedCalendars userCalendar = exCalHelperService.CheckConnectedCalendars(UserId); if (userCalendar.hasGoogle) { googleCalendarService.UpdateEvent(UserId, Id, updateRequest); } if (userCalendar.hasMicrosoft) { msCalService.UpdateEvent(UserId, Id, updateRequest); } }); return(Request.CreateResponse(HttpStatusCode.OK, new SuccessResponse())); } else { ModelState.AddModelError("", "Id in URL does not match Id in request body"); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } }
// Save received event (already validated by edit view model) to db // If SaveEventMsg.SaveChangeOnline true, then also save event to / delete event from google calendar private async void OnModifyEventReceived(ModifyEventMsg m) { var eventToModify = m.Event; var service = new GoogleCalendarService(); try { // Modify local db and possiblely google calendar based on action switch (m.Action) { case EventModifyAction.Add: string googleCalendarEventId = null; var targetAccountId = eventToModify.AID; // Apply changes online first so any failure will prevent changes from being saved locally if (LoggedIn && m.SaveChangeOnline) { googleCalendarEventId = await service.AddEvent(eventToModify.ToEvent()); } // Event was successfully persisted on Google Calendar, update account and id if (!googleCalendarEventId.IsNullOrEmpty()) { var accounts = _dbs.GetAvailableAccounts(); var lastSyncAccount = accounts.Where(a => a.AccId > 1) .OrderByDescending(a => a.LastSync) .FirstOrDefault(); if (lastSyncAccount == null) { throw new DataException("Couldn't find target Google Calendar account"); } targetAccountId = lastSyncAccount.AccId; eventToModify.GID = googleCalendarEventId; } // Save changes locally await _dbs.AddEvent(eventToModify, targetAccountId); // Switch view to edit just created event Messenger.Base.Send(eventToModify); break; case EventModifyAction.Update: if (LoggedIn && m.SaveChangeOnline) { await service.UpdateEvent(eventToModify.ToEvent()); } await _dbs.UpdateEvent(eventToModify, eventToModify.EID); break; case EventModifyAction.Delete: if (LoggedIn && m.SaveChangeOnline) { await service.DeleteEvent(eventToModify.GID); } _dbs.DeleteEvent(eventToModify.EID); break; default: SendModifyEventStatus(success: false, msg: "Unknown action received"); return; } } catch (DataException ex) { SendModifyEventStatus(success: false, msg: ex.Message); return; } catch (Google.GoogleApiException) { SendModifyEventStatus(success: false, msg: "Unable to apply changes online. If error persists, consider modifying event locally"); return; } SendModifyEventStatus(success: true, deleted: m.Action == EventModifyAction.Delete); RefreshEvents(viewableEventsNum); // Update tree view if opened Messenger.Base.Send(new UpdateTreeViewMsg { ModifiedEvent = m.Event, Action = m.Action }); }