public async Task <IActionResult> Create(Airport airport) { if (ModelState.IsValid) { try { await _airportRepository.CreateAsync(airport); return(RedirectToAction(nameof(Index))); } catch (DbUpdateException dbUpdateException) { if (dbUpdateException.InnerException.Message.Contains("duplicate")) { ModelState.AddModelError(string.Empty, "There's an airport with the same IATA already!"); } else { ModelState.AddModelError(string.Empty, dbUpdateException.InnerException.Message); } } catch (Exception exception) { ModelState.AddModelError(string.Empty, exception.Message); } } return(View(airport)); }
public async Task <IActionResult> Create(CreateViewModel model) { if (ModelState.IsValid) { var airport = await _airportRepository.GetByIataAsync(model.IATA); if (airport != null) { ModelState.AddModelError(nameof(model.IATA), "There's already an Airport registered with the given IATA."); return(View(model)); } var entity = _converterHelper.ToAirportEntity(model); await _airportRepository.CreateAsync(entity); ViewBag.Message = "Airport successfuly created and awaiting aproval."; } return(View(model)); }