Ejemplo n.º 1
0
        public async Task <object> UpdateTask([FromRoute] string taskId, [FromBody] AspNetTasks updatedTask)
        {
            using (var ctx = new AckeeCtx())
            {
                var task = ctx.Tasks
                           .Include(t => t.MilestoneTasks)
                           .FirstOrDefault(t => t.TaskID == updatedTask.TaskID);

                if (task == null)
                {
                    return(BadRequest());
                }

                task.TaskName        = updatedTask.TaskName;
                task.TaskDescription = updatedTask.TaskDescription;
                task.StartDate       = updatedTask.StartDate;
                task.EndDate         = updatedTask.EndDate;
                task.Completed       = updatedTask.Completed;

                await ctx.SaveChangesAsync();

                // Set completed milestones to complete
                task = ctx.Tasks.FirstOrDefault(t => t.TaskID == updatedTask.TaskID);

                // Get all the milestones related to the task
                foreach (var milestoneTask in task.MilestoneTasks)
                {
                    // foreach task related to the milestone, check if all tasks are completed
                    var milestone = await ctx.Milestones.FirstOrDefaultAsync(m => m.MilestoneID == milestoneTask.MilestoneID);

                    if (milestone.MilestoneTasks.All(mt => mt.Task.Completed))
                    {
                        milestone.Completed = true;
                    }
                    else
                    {
                        milestone.Completed = false;
                    }
                }

                await ctx.SaveChangesAsync();

                return(true);
            }
        }
Ejemplo n.º 2
0
        public async Task <object> CreateTask([FromBody] AspNetTasks task)
        {
            using (var ctx = new AckeeCtx())
            {
                var existingTask = ctx.Tasks.FirstOrDefault(t => t.TaskName == task.TaskName && t.Project.ProjectID == task.Project.ProjectID);
                var project      = ctx.Projects.FirstOrDefault(p => p.ProjectID == task.Project.ProjectID);

                if (task == null || existingTask != null || project == null)
                {
                    return(BadRequest());
                }

                task.Project = project;

                ctx.Tasks.Add(task);
                await ctx.SaveChangesAsync();

                return(true);
            }
        }