예제 #1
0
        public bool AddSetting(AppSettingViewModel setting)
        {
            setting.AppSettingId = 0;
            var appSettingModel = mapper.Map <AppSettingViewModel, AppSettings>(setting);

            return(settingsRepository.AddSetting(appSettingModel));
        }
예제 #2
0
        public async Task <IActionResult> GetColorSettings()
        {
            var clientUsername = User.FindFirst(ClaimTypes.Name).Value;
            var userSettings   = await _settingsRepo.GetSettings(clientUsername);

            SettingsForQueryDTO colorSettings;

            try
            {
                colorSettings = userSettings.Single(x => x.Name == "General-Colors");
            }
            catch (InvalidOperationException)
            {
                // Type of DEFAULT means the user uses one of the default values (0, 1, 2 ...)
                // Type of CUSTOM means the value is a custom rgb value ('12,34,54'  'x,y,z'  ...)
                Setting defaultColorSettings = new Setting(
                    name: "General-Colors",
                    fields: new List <SettingField> {
                    new SettingField(
                        name: "UIBase",
                        value: "0",
                        type: "DEFAULT"
                        ),
                    new SettingField(
                        name: "TextBase",
                        value: "0",
                        type: "DEFAULT"
                        ),
                    new SettingField(
                        name: "BackgroundBase",
                        value: "0",
                        type: "DEFAULT"
                        )
                }
                    );

                _settingsRepo.AddSetting(clientUsername, defaultColorSettings);
                colorSettings = _mapper.Map <SettingsForQueryDTO>(defaultColorSettings);
            }

            return(Ok(colorSettings));
        }
예제 #3
0
        public async Task <IActionResult> GetForecastForUser(CityForLocationDTO position)
        {
            var clientUsername = User.FindFirst(ClaimTypes.Name).Value;

            // Check cache for forecast for the USER, that's not older then appsettings.WeatherCacheTime
            var forecastCacheBasePath = "./cache/forecasts/";
            var forecastCachePath     = forecastCacheBasePath + clientUsername + ".txt";

            if (Directory.Exists(forecastCacheBasePath))
            {
                if (System.IO.File.Exists(forecastCachePath))
                {
                    var cacheContent    = System.IO.File.ReadAllText(forecastCachePath);
                    var cachedForecast  = JsonConvert.DeserializeObject <ForecastAllForReturnDTO>(cacheContent);
                    var cacheMaxAgeMins = int.Parse(_config.GetSection("AppSettings:WeatherCacheTime").Value);

                    if (cachedForecast.QueryTime.AddMinutes(cacheMaxAgeMins) > DateTime.Now)
                    {
                        // Cache found AND it's recent
                        return(Ok(cachedForecast));
                    }
                }
            }
            else
            {
                Directory.CreateDirectory(forecastCacheBasePath);
            }
            // Cache not found or old

            // Get which city the user wants the forecast on
            var userSettings = await _settingsRepo.GetSettings(clientUsername);

            string city;

            try
            {
                city = userSettings.Single(x => x.Name == "Overview-Weather").Fields.Single(x => x.Name == "City").Value;
            }
            catch (InvalidOperationException)
            {
                city = "CURRENT_LOCATION";

                _settingsRepo.AddSetting(clientUsername, new Setting(
                                             name: "Overview-Weather",
                                             fields: new List <SettingField> {
                    new SettingField(
                        name: "City",
                        value: "CURRENT_LOCATION"
                        )
                }
                                             ));

                //TODO: _ if the location info is valid, have that as the default instead
            }

            // Get lat-lon data
            string name;
            double latitude;
            double longitude;

            if (city == "CURRENT_LOCATION")
            {
                latitude  = position.Latitude;
                longitude = position.Longitude;

                if (latitude == 0 && longitude == 0)
                {
                    /*
                     * _settingsRepo.ChangeSettingField(clientUsername, "Overview-Weather", "City", new SettingField(
                     *  name: "City",
                     *  value: "Debrecen"
                     * ));
                     * return BadRequest("Invalid location data! Check if you allowed location information for the website! Changing your city setting to 'Debrecen'.");
                     */
                    name = "Debrecen";
                }
                else
                {
                    string country;
                    name = GetCityFromCords(latitude, longitude, out country);
                    //TODO: _ If not already there, add city to DB
                }
            }
            else
            {
                name = city;
                var cityInfo = await GetCityInfo(name);

                latitude  = cityInfo.Latitude;
                longitude = cityInfo.Longitude;
            }

            var forecast = await GetForecast(name, latitude, longitude);

            var forecastString = JsonConvert.SerializeObject(forecast);

            System.IO.File.WriteAllText(forecastCachePath, forecastString);

            return(Ok(forecast));
        }