예제 #1
0
        public async Task <IActionResult> EditAsync(int goalId)
        {
            InitResource();

            var goal = await repository.FindByIdAsync(goalId);

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

            var goalEditModel = new GoalEditModel()
            {
                Id          = goal.Id,
                Name        = goal.Name,
                Description = goal.Description,
                ArtistList  = goal.ArtistList,
                Status      = goal.Status,
                EndDate     = goal.EndDate
            };

            return(View(goalEditModel));
        }
예제 #2
0
        public async Task <IActionResult> EditGoalAsync([FromBody] GoalEditModel model)
        {
            try
            {
                if (!(ModelState.IsValid && await model.IsWithinParentDatesAsync(this.goalsService)))
                {
                    return(BadRequest(model));
                }

                var goal = Mapper.Map <GoalEditModel, Goal>(model);

                await this.goalsService.EditGoalAsync(goal);

                return(Ok(goal));
            }
            catch (GoalNotFoundException gnfe)
            {
                return(NotFound(gnfe.Message));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex.Message));
            }
        }
예제 #3
0
        public async Task <IActionResult> EditAsync(GoalEditModel goalEditModel)
        {
            InitResource();

            var goals = await repository.FindAll()
                        .Include(g => g.Parent)
                        .ToListAsync();

            var goal = goals.Where(g => g.Id == goalEditModel.Id)
                       .First();

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

            var subGoals = GoalBLogic.GetAllSubGoals(goal, goals);

            var oldStatus = goal.Status;
            var newStatus = goalEditModel.Status;

            var errorMessage = @"Статус ""Suspended"" и ""Completed"" 
                                может быть присвоен только после статуса ""Executed"".";

            if (newStatus == GoalStatus.Suspended && !IsChangedStatusValid(oldStatus, newStatus))
            {
                ModelState.AddModelError("Status", errorMessage);
            }

            if (newStatus == GoalStatus.Completed)
            {
                if (!IsChangedStatusValid(oldStatus, newStatus))
                {
                    ModelState.AddModelError("Status", errorMessage);
                }
                else
                {
                    foreach (var subGoal in subGoals)
                    {
                        oldStatus = subGoal.Status;

                        if (!IsChangedStatusValid(oldStatus, newStatus))
                        {
                            ModelState.AddModelError("", @"В одной из подзадачи статус ""Suspended"" или ""Completed"".");
                        }
                    }
                }
            }

            if (ModelState.IsValid)
            {
                if (newStatus == GoalStatus.Completed)
                {
                    foreach (var subGoal in subGoals)
                    {
                        subGoal.Status = newStatus;

                        repository.Update(subGoal);
                    }
                }

                goal.Name        = goalEditModel.Name;
                goal.Description = goalEditModel.Description;
                goal.ArtistList  = goalEditModel.ArtistList;
                goal.Status      = goalEditModel.Status;
                goal.EndDate     = goalEditModel.EndDate;

                repository.Update(goal);

                await repository.SaveAsync();

                return(RedirectToAction("Index", "Goal"));
            }

            return(View(goalEditModel));
        }