public async Task <ActionResult> SprintSettings(SprintSettingsFormViewModel model)
        {
            //Find project in database
            var project = await _db.Projects.FindAsync(model.Id);

            if (project == null)
            {
                return(HttpNotFound("Project not found."));
            }

            //TODO: Ensure user is allowed access to this project

            //Ensure model is valid
            if (!ModelState.IsValid)
            {
                var sprintSettingsViewModel = new SprintSettingsViewModel()
                {
                    Id            = project.Id,
                    FormViewModel = new SprintSettingsFormViewModel()
                    {
                        Id             = project.Id,
                        SprintDuration = project.SprintDuration
                    },
                    TableViewModel = new SprintSettingsTableViewModel()
                    {
                        Id      = project.Id,
                        Sprints = GetTableItemsList(project)
                    }
                };
                return(PartialView("_SprintSettings", sprintSettingsViewModel));
            }

            //Update entity
            project.SprintDuration = model.SprintDuration;

            //Update future sprints with new schedule
            var futureSprints = project.Sprints.Where(sprint => sprint.StartDate > DateTime.Now);
            var lastSprint    = project.GetThisSprint();

            foreach (var futureSprint in futureSprints)
            {
                futureSprint.StartDate = lastSprint.EndDate;
                futureSprint.EndDate   = futureSprint.StartDate.AddDays(7 * model.SprintDuration);
                lastSprint             = futureSprint;
            }

            try
            {
                //Save all changes made
                await _db.SaveChangesAsync();

                return(Json(new { success = true }));
            }
            catch (Exception exception)
            {
                ViewBag.Error = exception.Message;
            }
            return(await SprintSettings(model.Id));
        }
        // GET: Sprint/SprintSettings/5
        public async Task <ActionResult> SprintSettings(int?id)
        {
            //Ensure valid input
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            //Find project in database
            var project = await _db.Projects.FindAsync(id);

            if (project == null)
            {
                return(HttpNotFound("Project not found."));
            }

            //TODO: Ensure user is allowed access to this project

            //Create and return model
            var model = new SprintSettingsViewModel()
            {
                Id            = project.Id,
                FormViewModel = new SprintSettingsFormViewModel()
                {
                    Id             = project.Id,
                    SprintDuration = project.SprintDuration
                },
                TableViewModel = new SprintSettingsTableViewModel()
                {
                    Id      = project.Id,
                    Sprints = GetTableItemsList(project)
                }
            };

            return(PartialView("_SprintSettings", model));
        }
        public async Task <ActionResult> EditSprint(SprintEditViewModel model)
        {
            //Ensure model is valid
            if (!ModelState.IsValid)
            {
                return(PartialView("_SprintEdit", model));
            }

            //Find database sprint
            var sprint = await _db.Sprints.FindAsync(model.Id);

            if (sprint == null)
            {
                return(HttpNotFound("Sprint not found."));
            }

            //TODO: Ensure user is allowed access to this project

            //Validate sprint dates
            if (model.EndDate < DateTime.Now)
            {
                model.Error = "You cannot set the end date to the past.";
                return(PartialView("_SprintEdit", model));
            }

            //Attempt to store the edit
            try
            {
                //Update the database
                sprint.StartDate = model.StartDate;
                sprint.EndDate   = model.EndDate;
                await _db.SaveChangesAsync();

                //Update future sprints with new schedule
                var futureSprints = sprint.Project.Sprints.Where(s => s.Iteration > sprint.Iteration);
                var lastSprint    = sprint;
                foreach (var futureSprint in futureSprints)
                {
                    futureSprint.StartDate = lastSprint.EndDate;
                    futureSprint.EndDate   = futureSprint.StartDate.AddDays(7 * sprint.Project.SprintDuration);
                    lastSprint             = futureSprint;
                }
                await _db.SaveChangesAsync();
            }
            catch (Exception exception)
            {
                //Return error
                model.Error = exception.Message;
                return(PartialView("_SprintEdit", model));
            }

            //Create view model and return
            var sprintSettingViewModel = new SprintSettingsViewModel()
            {
                Id            = sprint.Project.Id,
                Info          = "Sprint Updated",
                FormViewModel = new SprintSettingsFormViewModel()
                {
                    Id             = sprint.Project.Id,
                    SprintDuration = sprint.Project.SprintDuration
                },
                TableViewModel = new SprintSettingsTableViewModel()
                {
                    Id      = sprint.Project.Id,
                    Sprints = GetTableItemsList(sprint.Project)
                }
            };

            return(PartialView("_SprintSettings", sprintSettingViewModel));
        }