예제 #1
0
        public async Task <ActionResult> GetGoalAsync(string idItem)
        {
            InitResource();

            idItem = string.Concat(idItem.Where(c => c >= '0' && c <= '9'));

            if (!int.TryParse(idItem, out var id))
            {
                id = 1;
            }

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

            var showGoal = goals.Where(g => g.Id == id)
                           .First();

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

            var(plannedWorkLoad, actualExecutionTime) = GoalBLogic.CalculatePlannedWorkLoadAndActualExecutionTime(showGoal, goals);

            var goalIndexModel = new GoalIndexModel
            {
                Id                  = showGoal.Id,
                Name                = showGoal.Name,
                Description         = showGoal.Description,
                ArtistList          = showGoal.ArtistList,
                RegistrationDate    = showGoal.RegistrationDate,
                Status              = showGoal.Status,
                PlannedWorkLoad     = plannedWorkLoad,
                ActualExecutionTime = actualExecutionTime,
                ParentId            = showGoal.ParentId,
                EndDate             = showGoal.EndDate
            };

            var subGoalIndexModels = new List <GoalIndexModel>();

            foreach (var subGoal in subGoals)
            {
                subGoalIndexModels.Add(new GoalIndexModel
                {
                    Id                  = subGoal.Id,
                    Name                = subGoal.Name,
                    Description         = subGoal.Description,
                    ArtistList          = subGoal.ArtistList,
                    RegistrationDate    = subGoal.RegistrationDate,
                    Status              = subGoal.Status,
                    PlannedWorkLoad     = subGoal.PlannedWorkLoad,
                    ActualExecutionTime = subGoal.ActualExecutionTime,
                    ParentId            = subGoal.ParentId,
                    EndDate             = subGoal.EndDate
                });
            }

            return(PartialView((goalIndexModel, subGoalIndexModels)));
        }
예제 #2
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));
        }