public IActionResult Create() { int astronomerId = int.Parse(this.userManager.GetUserId(User)); DiscoveryFormViewModel model = new DiscoveryFormViewModel(); this.PopulateCreateViewModel(astronomerId, model); return(View(model)); }
public IActionResult Create(DiscoveryFormViewModel model) { int astronomerId = int.Parse(this.userManager.GetUserId(User)); if (!ModelState.IsValid) { this.PopulateCreateViewModel(astronomerId, model); return(View(model)); } if (this.discoveryService.Exists(model.Discovery.StarSystem)) { this.PopulateCreateViewModel(astronomerId, model); TempData.AddErrorMessage(string.Format(WebConstants.EntryExists, StarSystem)); return(View(model)); } if (this.starService.Exists(model.Star.Name)) { this.PopulateCreateViewModel(astronomerId, model); TempData.AddErrorMessage(string.Format(WebConstants.EntryExists, Star)); return(View(model)); } int discoveryId = this.discoveryService.Create( model.Discovery.StarSystem, model.Discovery.Distance, model.TelescopeId, astronomerId, model.AstronomerIds); if (discoveryId <= 0) { return(BadRequest()); } bool success = this.starService.Create(discoveryId, model.Star.Name, model.Star.Temperature); if (!success) { this.discoveryService.Delete(discoveryId); return(BadRequest()); } TempData.AddSuccessMessage(string.Format(WebConstants.SuccessfullEntityOperation, Discovery, WebConstants.Added)); return(RedirectToAction(nameof(Details), new { id = discoveryId })); }
private void PopulateCreateViewModel(int astronomerId, DiscoveryFormViewModel model) { model.Astronomers = this.GetAstronomers(astronomerId); model.Telescopes = this.GetTelescopes(); }