public async Task <IActionResult> EditProposal(string id, ProposalModel model)
        {
            var map = await MapModel.GetAsync(AppSettings.DefaultRepo);

            var existing = map.Proposals?.FirstOrDefault(i => i.Id == id);

            if (existing != null)
            {
                existing.Title = model.Title;

                using (await LockingService.LockAsync())
                {
                    await map.SaveAsync();

                    await BaseContentsModel.SaveContentsAsync(id, AppSettings.DefaultRepo, model.Contents);

                    await ChangesService.Get(AppSettings.DefaultRepo).PushChangesAsync();
                }
            }

            dynamic parameters = new ExpandoObject();

            parameters.id = id;
            return(RedirectToAction(nameof(ViewProposal), parameters));
        }
        public async Task <IActionResult> EditProposalExample(string scenarioId, string proposalId, AddProposalExampleModel model)
        {
            var scenario = await ScenarioModel.GetAsync(AppSettings.DefaultRepo, scenarioId);

            if (scenario == null)
            {
                return(NotFound());
            }

            var proposalExample = scenario.Proposals?.FirstOrDefault(i => i.ProposalId == proposalId);

            if (proposalExample == null)
            {
                return(NotFound());
            }

            using (await LockingService.LockAsync())
            {
                await BaseContentsModel.SaveContentsAsync(proposalExample.Id, AppSettings.DefaultRepo, model.Contents);

                await ChangesService.Get(AppSettings.DefaultRepo).PushChangesAsync();
            }

            dynamic parameters = new ExpandoObject();

            parameters.scenarioId = scenarioId;
            parameters.proposalId = proposalId;
            return(RedirectToAction(actionName: nameof(ViewProposalExample), routeValues: parameters));
        }
        public async Task <IActionResult> CreateProposal(ProposalModel model)
        {
            var map = await MapModel.GetAsync(AppSettings.DefaultRepo);

            var id = Guid.NewGuid().ToString();

            if (map.Proposals == null)
            {
                map.Proposals = new List <MapProposalModel>();
            }
            map.Proposals.Add(new MapProposalModel()
            {
                Id    = id,
                Title = model.Title
            });

            using (await LockingService.LockAsync())
            {
                await map.SaveAsync();

                await BaseContentsModel.SaveContentsAsync(id, AppSettings.DefaultRepo, model.Contents);

                await ChangesService.Get(AppSettings.DefaultRepo).PushChangesAsync();
            }

            dynamic parameters = new ExpandoObject();

            parameters.id = id;
            return(RedirectToAction(nameof(ViewProposal), parameters));
        }
        public async Task <IActionResult> AddProposalExample(string scenarioId, AddProposalExampleModel model)
        {
            if (string.IsNullOrWhiteSpace(model.ProposalId))
            {
                throw new Exception("You must pick a proposal");
            }

            var map = await MapModel.GetAsync(AppSettings.DefaultRepo);

            var parent = map.FindScenario(scenarioId);

            if (parent != null)
            {
                if (parent.ProposalExamples == null)
                {
                    parent.ProposalExamples = new List <MapProposalExampleModel>();
                }

                var newProposal = new MapProposalExampleModel()
                {
                    ProposalId = model.ProposalId
                };

                parent.ProposalExamples.Add(newProposal);

                using (await LockingService.LockAsync())
                {
                    await map.SaveAsync();

                    await BaseContentsModel.SaveContentsAsync(parent.Id + "-" + model.ProposalId, AppSettings.DefaultRepo, model.Contents);

                    await ChangesService.Get(AppSettings.DefaultRepo).PushChangesAsync();
                }
            }

            dynamic parameters = new ExpandoObject();

            parameters.scenarioId = scenarioId;
            parameters.proposalId = model.ProposalId;
            return(RedirectToAction(actionName: nameof(ViewProposalExample), routeValues: parameters));
        }