public GoogleCalendarScheduler(IDateTimeProvider dateTimeProvider, IFlexKidsConfig flexKidsConfig /*IGoogleCalendarService googleCalendarService*/) { // if (googleCalendarService == null) // throw new ArgumentNullException("googleCalendarService"); // calendarService = googleCalendarService; googleCalendarId = flexKidsConfig.GoogleCalendarId; googleCalendarAccount = flexKidsConfig.GoogleCalendarAccount; //TODO Check if file exists and make it more robust. var certificate = new X509Certificate2(@flexKidsConfig.GoogleCalendarKeyFile, "notasecret", X509KeyStorageFlags.Exportable); var credential = new ServiceAccountCredential( new ServiceAccountCredential.Initializer(googleCalendarAccount) { Scopes = new[] { CalendarService.Scope.Calendar } }.FromCertificate(certificate)); // Create the service. var service = new CalendarService(new BaseClientService.Initializer { HttpClientFactory = new Google.Apis.Http.HttpClientFactory(), HttpClientInitializer = credential, ApplicationName = "FlexKids Rooster by CoenM", }); if (service == null) { throw new Exception("Cannot create service."); } calendarService = new GoogleCalendarService(service); var calendar = calendarService.GetCalendarById(googleCalendarId); if (calendar == null) { throw new Exception("Cannot find calendar"); } }
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); } }