public async Task MakeEvents(IReadOnlyList <ScheduleDiff> schedule, WeekSchedule updatedWeekSchedule)
        {
            Calendar calendar = await _calendarService.GetCalendarById(_googleCalendarId);

            if (calendar == null)
            {
                throw new CalendarNotFoundException(_googleCalendarId);
            }

            var timezone = calendar.TimeZone;

            EventsResource.ListRequest request = _calendarService.CreateListRequestForWeek(
                _googleCalendarId,
                updatedWeekSchedule.Year,
                updatedWeekSchedule.WeekNumber);

            Events result = await _calendarService.GetEvents(request);

            var allRows = new List <Event>();

            while (result.Items != null)
            {
                // Add the rows to the final list
                allRows.AddRange(result.Items);

                // We will know we are on the last page when the next page token is null.
                // If this is the case, break.
                if (result.NextPageToken == null)
                {
                    break;
                }

                // Prepare the next page of results
                request.PageToken = result.NextPageToken;

                // Execute and process the next page request
                result = await _calendarService.GetEvents(request);
            }

            foreach (Event ev in allRows)
            {
                _ = await _calendarService.DeleteEvent(_googleCalendarId, ev);
            }

            // add items to calendar.
            IOrderedEnumerable <ScheduleDiff> addedSchedules = schedule
                                                               .Where(x => x.Status is ScheduleStatus.Added or ScheduleStatus.Unchanged)
                                                               .OrderBy(x => x.Start)
                                                               .ThenBy(x => x.Status);

            foreach (ScheduleDiff item in addedSchedules)
            {
                var extendedProperty = new Event.ExtendedPropertiesData
                {
                    Shared = new Dictionary <string, string>
                    {
                        { "Week", item.SingleShift.WeekSchedule.Year + "-" + item.SingleShift.WeekSchedule.WeekNumber },
                    },
                };

                var newEvent = new Event
                {
                    Start              = CreateEventDateTime(item.SingleShift.StartDateTime, timezone),
                    End                = CreateEventDateTime(item.SingleShift.EndDateTime, timezone),
                    Description        = string.Empty,
                    Location           = item.SingleShift.Location,
                    Summary            = item.SingleShift.Location,
                    ExtendedProperties = extendedProperty,
                };

                _ = await _calendarService.InsertEvent(_googleCalendarId, newEvent);
            }
        }
Example #2
0
        public void MakeEvents(IList <ScheduleDiff> schedule)
        {
            var weeks = schedule.Select(x => x.Schedule.Week).Distinct();

            foreach (var w in weeks)
            {
                /*
                 * List<string> xxy = new List<string>();
                 * //xxy.Add("MadeByApplication=Coen");
                 * xxy.Add("Week=" + w.Year + "-" + w.WeekNr);
                 * var x = new Google.Apis.Util.Repeatable<string>(xxy);
                 *
                 *
                 *
                 * var request = calendarService.CreateListRequest(calendarId);
                 * request.MaxResults = 100;
                 * request.ShowDeleted = false;
                 *
                 * //request.SharedExtendedProperty = x;
                 * request.SharedExtendedProperty = "Week=" + w.Year + "-"+  w.WeekNr;
                 */

                var request = calendarService.CreateListRequestForWeek(googleCalendarId, w);

                var result  = calendarService.GetEvents(request);
                var allRows = new List <Event>();
                while (result.Items != null)
                {
                    //Add the rows to the final list
                    allRows.AddRange(result.Items);

                    // We will know we are on the last page when the next page token is
                    // null.
                    // If this is the case, break.
                    if (result.NextPageToken == null)
                    {
                        break;
                    }

                    // Prepare the next page of results
                    request.PageToken = result.NextPageToken;

                    // Execute and process the next page request
                    result = calendarService.GetEvents(request);
                }

                foreach (Event ev in allRows)
                {
                    calendarService.DeleteEvent(googleCalendarId, ev);
                }
            }

            // add items to calendar.
            foreach (var item in schedule.Where(x => x.Status == ScheduleStatus.Added).OrderBy(x => x.Start).ThenBy(x => x.Status))
            {
                var extendedProperty = new Event.ExtendedPropertiesData();

                extendedProperty.Shared = new Dictionary <string, string>();
                // x.Shared.Add("MadeByApplication", "Coen");
                extendedProperty.Shared.Add("Week", item.Schedule.Week.Year + "-" + item.Schedule.Week.WeekNr);

                //  queryEvent.SharedExtendedProperty = "EventID=3684";
                var newEvent = new Event
                {
                    Start = new EventDateTime {
                        DateTime = item.Schedule.StartDateTime
                    },
                    End = new EventDateTime {
                        DateTime = item.Schedule.EndDateTime
                    },
                    Description = "Ja, je moet werken! xx coen",
                    Location    = item.Schedule.Location,
                    Summary     = item.Schedule.Location,


                    ExtendedProperties = extendedProperty
                };
                var e = calendarService.InsertEvent(googleCalendarId, newEvent);
            }

            // [email protected] -> Gmail Account (default Kalender)
            // var request = service.Events.Create("*****@*****.**", newEvent);
        }