//[Route("[controller]/[action]/{id}")] public async Task <IActionResult> Upsert(string id) { BuildingPlanViewModel model = null; try { var allBuildings = (await _buildingRepository.GetAllBuildings()).ToList(); BuildingPlanModel plan; IEnumerable <BuildingPlanStepViewModel> queue = null; if (string.IsNullOrEmpty(id)) { plan = new BuildingPlanModel { BotUserName = User.Identity.Name }; } else { plan = await _buildingPlanRepository.GetBuildingPlan(id); queue = plan.BuildingSteps.Select(x => new BuildingPlanStepViewModel { Order = x.Order, Level = x.Level, Building = allBuildings.FirstOrDefault(y => y.BuildingId == x.BuildingId) }); } model = _mapper.Map <BuildingPlanViewModel>(plan); model.BuildingSteps = queue?.ToList(); ViewData["buildings"] = allBuildings; } catch (Exception exc) { _logger.LogError(LoggingEvents.GetItemException, exc, exc.Message); ViewBag.ErrorMessage = "Unable to find Building Plan."; } return(View(model)); }
public async Task <IActionResult> Upsert(BuildingPlanViewModel model) { try { var plan = _mapper.Map <BuildingPlanModel>(model); plan.BuildingSteps = model.BuildingSteps.Select(x => new BuildingPlanStepModel { Order = x.Order, Level = x.Level, BuildingId = x.Building.BuildingId }).ToList(); if (plan.BotUserName == BuildingPlanRepository.DefaultPlanUserName) { plan._id = string.Empty; } plan.BotUserName = User.Identity.Name; if (string.IsNullOrEmpty(plan._id)) { await _buildingPlanRepository.Insert(plan); } else { await _buildingPlanRepository.Update(plan); } var allBuildings = (await _buildingRepository.GetAllBuildings()).ToList(); ViewData["buildings"] = allBuildings; } catch (Exception exc) { _logger.LogError(LoggingEvents.InsertItemException, exc, exc.Message); ViewBag.ErrorMessage = "Unable to update Building Plan."; } return(View(model)); }