示例#1
0
        public async System.Threading.Tasks.Task SaveStepDescriptionAsync(StepViewModel updatedStepViewModel)
        {
            int  id   = updatedStepViewModel.Id;
            Step step = await _cacheContext.Steps.SingleAsync(s => s.Id == id);

            step.Description = updatedStepViewModel.Description;
            await _cacheContext.SaveChangesAsync();
        }
示例#2
0
        private static StepViewModel CreateStepViewModel(Step step)
        {
            StepViewModel stepViewModel = new StepViewModel()
            {
                Id          = step.Id,
                Description = step.Description,
                Order       = step.Order,
            };

            return(stepViewModel);
        }
示例#3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="parentTask"></param>
        /// <param name="previousStep">If null, inserts the step as the first one.</param>
        /// <returns></returns>
        public async System.Threading.Tasks.Task <StepViewModel> CreateStepAsync(TaskViewModel parentTask, StepViewModel previousStep = null)
        {
            Step newStep = _cacheContext.Steps.Add(_cacheContext.Steps.Create());

            newStep.Description = "New Step";

            Task persistedTask = _cacheContext.Tasks.Include(t => t.Steps).Single(t => t.Id == parentTask.Id);

            double previousOrder;
            double nextOrder;

            // If null, insert as the first one
            if (previousStep == null)
            {
                previousOrder = 0;
                nextOrder     = persistedTask.Steps.FirstOrDefault()?.Order ?? 1;
            }
            else
            {
                Step persistedPreviousStep = persistedTask.Steps.SingleOrDefault(s => s.Id == previousStep.Id);

                if (persistedPreviousStep == null)
                {
                    throw new ArgumentException("Previous step is not part of the parent task.");
                }

                previousOrder = previousStep.Order;
                nextOrder     = persistedTask.Steps.OrderBy(s => s.Order).FirstOrDefault(s => s.Order > previousOrder)?.Order ?? 1;
            }

            newStep.Order = (previousOrder + nextOrder) / 2;
            persistedTask.Steps.Add(newStep);

            await _cacheContext.SaveChangesAsync();

            StepViewModel stepViewModel = CreateStepViewModel(newStep);

            parentTask.Steps.Add(stepViewModel);

            return(stepViewModel);
        }