public ActionResult Index(int id = 0) { CountryBindingModel model = new CountryBindingModel(); CountryViewModel viewModel = new CountryViewModel(); model.SetSharedData(User); if (id == 0) { return(View(model)); } else { var response = AsyncHelpers.RunSync <JObject>(() => ApiCall.CallApi("/api/Admin/GetEntityById", User, null, true, false, null, "Id=" + id + "&EntityType=" + Utility.KorsaEntityTypes.Country)); if (response is Error) { return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "Internal Server Error")); } else { viewModel = response.GetValue("result").ToObject <CountryViewModel>(); } model.IsActive = viewModel.IsActive; model.Name = viewModel.English.Name; model.SetSharedData(User); return(View(model)); } }
public async Task <IHttpActionResult> UpdateCountry(string isoCode, [FromBody] CountryBindingModel country) { if (country == null) { return(BadRequest($"Request content is empty")); } using (IUnitOfWork rep = Store.CreateUnitOfWork()) { var item = await rep.CountryRepository.GetAsync(isoCode); if (item == null) { return(NotFound()); } item.IsoCode = country.IsoCode.ToUpper(); item.Name = country.Name; item.CallingCode = country.CallingCode; item.DateFormat = country.DateFormat; await rep.CompleteAsync(); var result = item.AsCountryDTO(Url); var response = Request.CreateResponse(HttpStatusCode.Created, result); response.Headers.Location = new Uri(result.GetUrl); return(ResponseMessage(response)); } }
//[ResponseType(typeof(Country))] public IHttpActionResult PostCountry(CountryBindingModel countryBM) { if (ModelState.IsValid) { try { var country = Mapper.Map <CountryBindingModel, Country>(countryBM); _countryService.Save(country); country = _countryService.GetById(country.Id); countryBM = Mapper.Map <Country, CountryBindingModel>(country); return(Ok(countryBM)); } catch (Exception ex) { var result = ex.Message; } } else { return(BadRequest(ModelState)); } return(Ok(StatusCode(HttpStatusCode.BadRequest))); }
public ActionResult Create(CountryBindingModel model) { if (!this.ModelState.IsValid) { return(View(model)); } var country = new Country() { LongName = model.LongName, ShortName = model.ShortName, CallingCode = model.CallingCode, FlagUrl = model.FlagUrl }; try { this.service.CreateCountry(country); } catch (System.Exception) { this.ViewData["Error"] = "This country already exists!"; return(this.View(model)); } return(RedirectToAction("Index", "Home")); }
public ActionResult Create(CountryBindingModel bind) { if (this.ModelState.IsValid) { var country = Mapper.Map <CountryBindingModel, Country>(bind); this.db.Countries.Add(country); this.db.SaveChanges(); return(this.RedirectToAction("Index")); } return(this.View(bind)); }
public ActionResult Edit([Bind(Include = "Id, Name")] CountryBindingModel model) { if (this.ModelState.IsValid) { var country = this.db.Countries.Find(model.Id); country = Mapper.Map <CountryBindingModel, Country>(model); this.db.Countries.AddOrUpdate(a => a.Id, country); this.db.SaveChanges(); return(this.RedirectToAction("Index")); } return(this.View(model)); }
public IHttpActionResult DeleteCountry(Guid id) { try { var countryBM = new CountryBindingModel() { Id = id }; var country = Mapper.Map <CountryBindingModel, Country>(countryBM); _countryService.Delete(country.Id); return(Ok()); } catch (Exception ex) { var result = ex.Message; } return(Ok(StatusCode(HttpStatusCode.BadRequest))); }
public async Task <ActionResult> AddCountry(CountryBindingModel model) { if (ModelState.IsValid) { JObject response; response = await ApiCall.CallApi("/api/Admin/AddUpdateCountry", User, model); if (response.ToString().Contains("UnAuthorized")) { return(new HttpStatusCodeResult(HttpStatusCode.Unauthorized, "UnAuthorized Error")); } else if (response is Error) { return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError, (response as Error).ErrorMessage)); } return(RedirectToAction("ManageCountries", "CountryCities")); } else { return(View()); } }
public async Task <IHttpActionResult> PostCountry([FromBody] CountryBindingModel country) { if (country == null) { return(BadRequest($"Request content is empty")); } using (IUnitOfWork rep = Store.CreateUnitOfWork()) { var item = await rep.CountryRepository.GetAsync(country.IsoCode); if (item != null) { return(BadRequest("Country already exists")); } var newCountry = new Country() { IsoCode = country.IsoCode.ToUpper(), Name = country.Name, DateFormat = country.DateFormat, CallingCode = country.CallingCode, Currencies = new List <Currency>(), Organizations = new List <Organization>() }; if (country.Currencies != null) { foreach (var isoCode in country.Currencies) { var cur = await rep.CurrencyRepository.GetAsync(isoCode); if (cur == null) { return(BadRequest($"Currency {isoCode} not found")); } newCountry.Currencies.Add(cur); } } if (country.Organizations != null) { foreach (var isoCode in country.Organizations) { var org = await rep.OrganizationRepository.GetAsync(isoCode); if (org == null) { return(BadRequest($"Organization {isoCode} not found")); } newCountry.Organizations.Add(org); } } newCountry = rep.CountryRepository.Add(newCountry); await rep.CompleteAsync(); var result = newCountry.AsCountryDTO(Url); var response = Request.CreateResponse(HttpStatusCode.Created, result); response.Headers.Location = new Uri(result.GetUrl); return(ResponseMessage(response)); } }