public async Task <(bool HasMoreToBuild, IEnumerable <BuildAction> Actions)> GetBuildActions(Village village, IEnumerable <BuildingModel> allBuildings = null)
        {
            var result               = new List <BuildAction>();
            var buildings            = (allBuildings ?? await _buildingRepository.GetAllBuildings()).ToList();
            var dorf1BuildingToBuild = GetResourceFieldToBuildNext(village, buildings);

            if (dorf1BuildingToBuild != null)
            {
                result.Add(new BuildAction
                {
                    BuildingId   = dorf1BuildingToBuild.BuildingId,
                    Action       = GameActionType.BUILD,
                    BuildingSlot = dorf1BuildingToBuild.Id,
                    Village      = village
                });
            }

            BuildingPlanModel plan = null;

            if (!string.IsNullOrEmpty(village.BuildingPlanId))
            {
                plan = await _buildingPlanRepository.GetBuildingPlan(village.BuildingPlanId);
            }
            if (plan == null)
            {
                plan = (await _buildingPlanRepository.GetBuildingPlans(BuildingPlanRepository.DefaultPlanUserName)).FirstOrDefault();
            }

            var hasMoreBuildingsToBuild = TryGetNextBuildingInVillageToBuildFromBuildingPlan(village, buildings, plan, out var dorf2Building);

            if (dorf2Building != null)
            {
                var slot2 = village.BuildingSlots
                            .Where(x => x.BuildingId == dorf2Building.BuildingId)
                            .OrderBy(x => x.Level)
                            .FirstOrDefault();

                result.Add(new BuildAction
                {
                    BuildingId   = dorf2Building.BuildingId,
                    Action       = GameActionType.BUILD,
                    BuildingSlot = string.IsNullOrEmpty(slot2?.Id) ? dorf2Building.PrefferedBuildingSlot : slot2.Id,
                    Village      = village
                });
            }

            return(hasMoreBuildingsToBuild || dorf1BuildingToBuild != null, result);
        }
        //[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));
        }