public ActionResult UpdateGridSortOrder(Dictionary <int, int> items) { var message = "Updated grid sortorder"; try { var dbHelper = new DatabaseHelper(); if (items.Count > 0) { foreach (var item in items) { // Key = id, Value = SortOrder dbHelper.UpdateGridSortOrder(item.Key, item.Value); } } // Update runtimecache var editors = dbHelper.GetEditors(); RuntimeCacheHelper.SetCacheItem("LeBlenderEditors", editors, 1); } catch (Exception ex) { message = "Error while trying to update grid sortorder"; LogHelper.Error <HelperController>("Error while trying to update grid sortorder in the database", ex); } return(Json(new { Message = message })); }
/// <summary> /// Gets a JSON list of the available continents /// </summary> /// <returns>JSON response of available criteria</returns> public JsonResult GetContinents() { var cacheKey = $"PersonalisationGroups_GeoLocation_Continents"; var countries = RuntimeCacheHelper.GetCacheItem(cacheKey, () => { var assembly = GetResourceAssembly(); var resourceName = GetResourceName("continents"); using (var stream = assembly.GetManifestResourceStream(resourceName)) { if (stream == null) { return(null); } using (var reader = new StreamReader(stream)) { var continentRecords = reader.ReadToEnd() .Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries) .Select(x => new { code = x.Split(',')[0], name = CleanName(x.Split(',')[1]) }); continentRecords = continentRecords.OrderBy(x => x.name); return(continentRecords); } } }); return(Json(countries, JsonRequestBehavior.AllowGet)); }
/// <summary> /// Gets a JSON list of the available regions for a given country /// </summary> /// <param name="countryCode">Country code</param> /// <returns>JSON response of available criteria</returns> public JsonResult GetRegions(string countryCode) { var cacheKey = $"PersonalisationGroups_GeoLocation_Regions_{countryCode}"; var regions = RuntimeCacheHelper.GetCacheItem(cacheKey, () => { using (var stream = GetStreamForRegions()) { if (stream == null) { return(null); } using (var reader = new StreamReader(stream)) { var streamContents = reader.ReadToEnd(); var regionRecords = streamContents .SplitByNewLine(StringSplitOptions.RemoveEmptyEntries) .Where(x => x.Split(',')[0] == countryCode.ToUpperInvariant()) .Select(x => new { code = x.Split(',')[1], name = CleanName(x.Split(',')[2]) }) .OrderBy(x => x.name); return(regionRecords); } } }); return(Json(regions, JsonRequestBehavior.AllowGet)); }
public ActionResult UpdateEditor(string editor) { var message = "Grid editor has been saved"; try { var gridEditor = JsonConvert.DeserializeObject <LeBlenderGridEditorModel>(editor); var dbHelper = new DatabaseHelper(); var LeBlenderGridEditorId = dbHelper.InsertOrUpdateGridEditor(gridEditor); if (gridEditor.Config.Count > 0) { dbHelper.InsertOrUpdateConfig(LeBlenderGridEditorId, gridEditor.Config); } // Update runtimecache var editors = dbHelper.GetEditors(); RuntimeCacheHelper.SetCacheItem("LeBlenderEditors", editors, 1); } catch (Exception ex) { LogHelper.Error <HelperController>($"Error while trying to update editor", ex); message = $"{ex.Message} - {ex.InnerException?.Message}"; } return(Json(new { Message = message })); }
public Region GetRegionFromIp(string ip) { var cacheKey = $"PersonalisationGroups_GeoLocation_Region_{ip}"; var cachedItem = RuntimeCacheHelper.GetCacheItem(cacheKey, () => { try { using (var reader = new DatabaseReader(_pathToCityDb)) { try { var response = reader.City(ip); var region = new Region { City = response.City.Name, Subdivisions = response.Subdivisions .Select(x => x.Name) .Union(response.Subdivisions .SelectMany(x => x.Names .Where(y => !string.IsNullOrEmpty(y.Value)) .Select(y => y.Value))) .ToArray(), Country = new Country { Code = response.Country.IsoCode, Name = response.Country.Name, } }; return(region); } catch (AddressNotFoundException) { return(null); } catch (GeoIP2Exception ex) { if (IsInvalidIpException(ex)) { return(null); } throw; } } } catch (FileNotFoundException) { throw new FileNotFoundException( $"MaxMind Geolocation database required for locating visitor region from IP address not found, expected at: {_pathToCountryDb}. The path is derived from either the default ({AppConstants.DefaultGeoLocationCountryDatabasePath}) or can be configured using a relative path in an appSetting with key: \"{AppConstants.ConfigKeys.CustomGeoLocationCountryDatabasePath}\"", _pathToCountryDb); } }); return(cachedItem); }
public ActionResult GetEditors() { // Check if they exist in cache var editors = RuntimeCacheHelper.GetCachedItem <IOrderedEnumerable <LeBlenderGridEditorModel> >("LeBlenderEditors"); if (editors == null) { var dbHelper = new DatabaseHelper(); editors = dbHelper.GetEditors(); RuntimeCacheHelper.SetCacheItem("LeBlenderEditors", editors, 1); } return(Content(JsonConvert.SerializeObject(editors))); }
public ActionResult TransferAllEditors(string editors) { if (IsValidDomain(Request.Url)) { Response.Headers.Remove("Access-Control-Allow-Origin"); Response.AddHeader("Access-Control-Allow-Origin", Request.UrlReferrer.GetLeftPart(UriPartial.Authority)); Response.Headers.Remove("Access-Control-Allow-Credentials"); Response.AddHeader("Access-Control-Allow-Credentials", "true"); Response.Headers.Remove("Access-Control-Allow-Methods"); Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); } else { Response.StatusCode = 403; return(Content("Forbidden")); } var message = "Grideditors has been transferred successfully"; try { var gridEditors = JsonConvert.DeserializeObject <List <LeBlenderGridEditorModel> >(editors); if (gridEditors != null && gridEditors.Any()) { var dbHelper = new DatabaseHelper(); foreach (var gridEditor in gridEditors) { var gridEditorId = dbHelper.InsertOrUpdateGridEditor(gridEditor); if (gridEditor.Config.Count > 0) { dbHelper.InsertOrUpdateConfig(gridEditorId, gridEditor.Config); } } var savedEditors = dbHelper.GetEditors(); RuntimeCacheHelper.SetCacheItem("LeBlenderEditors", savedEditors, 1); } } catch (Exception ex) { message = $"Error while transferring editor for database storage. Message: {ex.Message}"; LogHelper.Error <HelperController>(message, ex); } return(Content(message)); }
public ActionResult DeleteEditor(int id) { var message = $"Successfully deleted editor from the database"; try { var dbHelper = new DatabaseHelper(); dbHelper.DelteGridEditor(id); var editors = dbHelper.GetEditors(); RuntimeCacheHelper.SetCacheItem("LeBlenderEditors", editors, 1); } catch (Exception ex) { message = $"Error while trying to delete GridEditor with id: {id} from the database"; LogHelper.Error <HelperController>($"Error while trying to delete editor with id: {id} from the database", ex); } return(Content(message)); }
public Country GetCountryFromIp(string ip) { var cacheKey = $"PersonalisationGroups_GeoLocation_Country_{ip}"; var cachedItem = RuntimeCacheHelper.GetCacheItem(cacheKey, () => { try { using (var reader = new DatabaseReader(_pathToCountryDb)) { try { var response = reader.Country(ip); return(new Country { Code = response.Country.IsoCode, Name = response.Country.Name, }); } catch (AddressNotFoundException) { return(null); } catch (GeoIP2Exception ex) { if (IsInvalidIpException(ex)) { return(null); } throw; } } } catch (FileNotFoundException) { throw new FileNotFoundException( $"MaxMind Geolocation database required for locating visitor country from IP address not found, expected at: {_pathToCountryDb}. The path is derived from either the default ({AppConstants.DefaultGeoLocationCountryDatabasePath}) or can be configured using a relative path in an appSetting with key: \"{AppConstants.ConfigKeys.CustomGeoLocationCountryDatabasePath}\"", _pathToCountryDb); } }); return(cachedItem); }
/// <summary> /// Gets a JSON list of the available countries /// </summary> /// <returns>JSON response of available criteria</returns> public JsonResult GetCountries(bool withRegionsOnly = false) { var cacheKey = $"PersonalisationGroups_GeoLocation_Countries_{withRegionsOnly}"; var countries = RuntimeCacheHelper.GetCacheItem(cacheKey, () => { var assembly = GetResourceAssembly(); var resourceName = GetResourceName("countries"); using (var stream = assembly.GetManifestResourceStream(resourceName)) { if (stream == null) { return(null); } using (var reader = new StreamReader(stream)) { var countryRecords = reader.ReadToEnd() .SplitByNewLine(StringSplitOptions.RemoveEmptyEntries) .Select(x => new { code = x.Split(',')[0], name = CleanName(x.Split(',')[1]) }); if (withRegionsOnly) { var countryCodesWithRegions = GetCountryCodesWithRegions(assembly); countryRecords = countryRecords .Where(x => countryCodesWithRegions.Contains(x.code)); } countryRecords = countryRecords.OrderBy(x => x.name); return(countryRecords); } } }); return(Json(countries, JsonRequestBehavior.AllowGet)); }
public ActionResult DeleteAllEditors(string editors) { try { var gridEditors = JsonConvert.DeserializeObject <List <LeBlenderGridEditorModel> >(editors); if (gridEditors != null && gridEditors.Any()) { var dbHelper = new DatabaseHelper(); foreach (var gridEditor in gridEditors) { dbHelper.DelteGridEditor(gridEditor.Id); } var updatedEditors = dbHelper.GetEditors(); RuntimeCacheHelper.SetCacheItem("LeBlenderEditors", updatedEditors, 1); } return(Content("Deleted all editors from database")); } catch (Exception ex) { LogHelper.Error <HelperController>($"Error while trying to perform action: Delete All Editors", ex); return(Content($"Error while trying to delete all editors. Error: {ex.Message}")); } }