Пример #1
0
 public GoogleCalendarService(
     HttpClient httpClient,
     AdventSettings adventSettings,
     TestSettings testSettings,
     GoogleCalendarSettings calendarSettings)
 {
     this._httpClient       = httpClient;
     this._adventSettings   = adventSettings;
     this._testSettings     = testSettings;
     this._calendarSettings = calendarSettings;
 }
        private async Task <GoogleCalendarSettings> CreateSettings(string calendarName)
        {
            GoogleCalendarSettings temporarySettings = null;

            if (File.Exists(SettingsPath))
            {
                using (var r = new StreamReader(SettingsPath))
                {
                    var json = r.ReadToEnd();
                    temporarySettings = JsonConvert.DeserializeObject <GoogleCalendarSettings>(json);
                    if (temporarySettings.CalendarName != calendarName)
                    {
                        Log.Warn(String.Format("Calendar name mismatch stored:[{0}], provided:[{1}]",
                                               temporarySettings.CalendarName, calendarName));
                        Log.Warn("Deleting old calendar and making a new one");
                        var isCalendarDeleted = await RemoveCalendar(temporarySettings.CalendarId);

                        if (isCalendarDeleted)
                        {
                            Log.Info("Calendar successfully deleted");
                        }
                        else
                        {
                            throw new Exception(
                                      String.Format("Failed to delete calendar id[{0}] and name [{1}]",
                                                    temporarySettings.CalendarId, temporarySettings.CalendarId));
                        }
                    }
                }
            }
            else
            {
                try
                {
                    var calendarId = (await CreateCalendar(calendarName)).Id;
                    temporarySettings = new GoogleCalendarSettings
                    {
                        CalendarName = calendarName,
                        CalendarId   = calendarId
                    };
                }
                catch (Exception ex)
                {
                    Log.Error("Exception", ex);
                }
                var jsonSettings = JsonConvert.SerializeObject(temporarySettings);
                using (var sw = new StreamWriter(SettingsPath))
                {
                    await sw.WriteAsync(jsonSettings);
                }
            }
            Log.Info(temporarySettings.ToJson());
            return(temporarySettings);
        }
Пример #3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var koplanetSettings = new KoplanetSettings();

            Configuration.Bind("KoplanetSettings", koplanetSettings);
            services.AddSingleton(koplanetSettings);
            services.AddSingleton <KoplanetClient>();

            var googleCalendarSettings = new GoogleCalendarSettings();

            Configuration.Bind("GoogleCalendarSettings", googleCalendarSettings);
            services.AddSingleton(googleCalendarSettings);
            services.AddSingleton <GoogleCalendarClient>();

            var googleReCaptchaSettings = new GoogleReCaptchaSettings();

            Configuration.Bind("GoogleReCaptchaSettings", googleReCaptchaSettings);
            services.AddSingleton(googleReCaptchaSettings);
            services.AddSingleton <GoogleReCaptchaClient>();

            var mailSettings = new MailSettings();

            Configuration.Bind("MailSettings", mailSettings);
            services.AddSingleton(mailSettings);
            services.AddSingleton <MailClient>();

            services.AddSingleton <LaNinaInterviewManager>();

            services.Configure <AdminSettings>(Configuration.GetSection("AdminSettings"));

            services.AddEntityFrameworkSqlite()
            .AddDbContext <LaNinaApplicantDbContext>(
                options => options.UseSqlite($"Data Source={_hostingEnvironment.ContentRootPath}/Databases/applicants.db"));

            services.AddEntityFrameworkSqlite()
            .AddDbContext <LaNinaFlagDbContext>(
                options => options.UseSqlite($"Data Source={_hostingEnvironment.ContentRootPath}/Databases/flags.db"));

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }
        public async Task <bool> Initialize(string clientId, string clientSecret, string calendarName)
        {
            Log.Info(String.Format("Initializing google calendar [{0}]", calendarName));
            var authenticated = await Authenticate(clientId, clientSecret);

            if (authenticated)
            {
                _settings = await CreateSettings(calendarName);

                var theirCalendarId = (await GetCalendar(_settings.CalendarId)).Id;
                if (_settings.CalendarId == theirCalendarId)
                {
                    Log.Debug(String.Format("Our calendar id matches the one on google: id[{0}]", _settings.CalendarId));
                }
                else
                {
                    throw new Exception(String.Format("Stored calendar id [{0}] doesn't match the one on google [{1}]",
                                                      _settings.CalendarId, theirCalendarId));
                }
            }
            return(authenticated);
        }
 public async Task<bool> Initialize(string clientId, string clientSecret, string calendarName)
 {
     Log.Info(String.Format("Initializing google calendar [{0}]", calendarName));
     var authenticated = await Authenticate(clientId, clientSecret);
     if (authenticated)
     {
         _settings = await CreateSettings(calendarName); 
         var theirCalendarId = (await GetCalendar(_settings.CalendarId)).Id;
         if (_settings.CalendarId == theirCalendarId)
         {
             Log.Debug(String.Format("Our calendar id matches the one on google: id[{0}]", _settings.CalendarId));
         }
         else
         {
             throw new Exception(String.Format("Stored calendar id [{0}] doesn't match the one on google [{1}]",
                 _settings.CalendarId, theirCalendarId));
         }
     }
     return authenticated;
 }
 private async Task<GoogleCalendarSettings> CreateSettings(string calendarName)
 {
     GoogleCalendarSettings temporarySettings = null;
     if (File.Exists(SettingsPath))
     {
         using (var r = new StreamReader(SettingsPath))
         {
             var json = r.ReadToEnd();
             temporarySettings = JsonConvert.DeserializeObject<GoogleCalendarSettings>(json);
             if (temporarySettings.CalendarName != calendarName)
             {
                 Log.Warn(String.Format("Calendar name mismatch stored:[{0}], provided:[{1}]",
                     temporarySettings.CalendarName, calendarName));
                 Log.Warn("Deleting old calendar and making a new one");
                 var isCalendarDeleted = await RemoveCalendar(temporarySettings.CalendarId);
                 if (isCalendarDeleted)
                 {
                     Log.Info("Calendar successfully deleted");
                 }
                 else
                 {
                     throw new Exception(
                         String.Format("Failed to delete calendar id[{0}] and name [{1}]",
                             temporarySettings.CalendarId, temporarySettings.CalendarId));
                 }
             }
         }
     }
     else
     {
         try
         {
             var calendarId = (await CreateCalendar(calendarName)).Id;
             temporarySettings = new GoogleCalendarSettings
             {
                 CalendarName = calendarName,
                 CalendarId = calendarId
             };
         }
         catch (Exception ex)
         {
             Log.Error("Exception", ex);
         }
         var jsonSettings = JsonConvert.SerializeObject(temporarySettings);
         using (var sw = new StreamWriter(SettingsPath))
         {
             await sw.WriteAsync(jsonSettings);
         }
     }
     Log.Info(temporarySettings.ToJson());
     return temporarySettings;
 }