public async Task <IActionResult> Post([FromBody] LocaleDto locale) { if (!ModelState.IsValid || locale == null || string.IsNullOrWhiteSpace(locale.LocaleCode)) { return(BadRequest()); } if (_localeRepo.Get(p => p.LocaleCode.Equals(locale.LocaleCode)).Count() != 0) { return(StatusCode((int)HttpStatusCode.Conflict)); } Locale newLocale = new Locale { LocaleCode = locale.LocaleCode, LocaleName = locale.LocaleName }; _localeRepo.Insert(newLocale); try { await _unitOfWork.SaveAsync(); } catch (Exception ex) { return(BadRequest(ex.InnerException.Message)); } return(CreatedAtRoute(routeName: "GetLocaleRoute", routeValues: new { code = locale.LocaleCode }, value: new LocaleDto(newLocale))); }
public async Task <List <City> > GetCities(LocaleDto locale, CityFilterDto filter = null, PaginationRequestDto pagination = null, OrderDto order = null) { var uri = string.Format(FormatUriForGetCities(filter, pagination, order), Secrets.GeoHelperApiKey, locale.Lang, locale.FallbackLang); Debug.WriteLine(uri); var res = await GetAsync <City>(uri); return(res ?? new List <City>()); }
/// <summary> /// Check locale properties /// </summary> /// <param name="locale">Locale information</param> private void VerifyLocaleDto(LocaleDto locale) { Verify.NotNull(locale, "locale"); Verify.RaiseWhenFailed(); Verify.NotNull(locale.Code, "locale.Code"); Verify.Matches(locale.Code, "^[a-z][a-z](-[a-z][a-z])?$", "locale.Code"); Verify.NotNullOrEmpty(locale.DisplayName, "locale.DisplayName"); Verify.MaxLength(locale.DisplayName, 128, "locale.DisplayName"); Verify.RaiseWhenFailed(); }
public async Task <List <Country> > GetCountries(LocaleDto locale, string name = null) { var uri = string.Format(GetCountriesUri, Secrets.GeoHelperApiKey, locale.Lang, locale.FallbackLang); if (!string.IsNullOrWhiteSpace(name)) { uri += $"&filter[name]={name}"; } var res = await GetAsync <Country>(uri); return(res ?? new List <Country>()); }
/// <summary> /// Modifies an existing locale /// </summary> /// <param name="locale">Locale information</param> public void ModifyLocale(LocaleDto locale) { VerifyLocaleDto(locale); using (var ctx = DataAccessFactory.CreateContext <IConfigurationDataOperations>()) { var localeInDb = ctx.GetByCode(locale.Code); if (localeInDb == null) { throw new LocaleNotFoundException(locale.Code); } localeInDb.DisplayName = locale.DisplayName; try { ctx.UpdateLocale(localeInDb); } catch (UniqueKeyViolationException) { throw new DuplicatedLocaleDisplayNameException(locale.DisplayName); } } }
/// <summary> /// Adds a new locale to the existing ones /// </summary> /// <param name="locale">Locale information</param> public void AddLocale(LocaleDto locale) { VerifyLocaleDto(locale); using (var ctx = DataAccessFactory.CreateContext <IConfigurationDataOperations>()) { try { ctx.InsertLocale(new LocaleRecord { Code = locale.Code, DisplayName = locale.DisplayName }); } catch (PrimaryKeyViolationException) { throw new DuplicatedLocaleCodeException(locale.Code); } catch (UniqueKeyViolationException) { throw new DuplicatedLocaleDisplayNameException(locale.DisplayName); } } }