Esempio n. 1
0
        public static async Task ReplaceTasksInList(this TUDUDbContext dbContext, int ListID, IEnumerable <TaskInListDTO> allTasks)
        {
            var list = dbContext.TaskLists.Include((x) => x.TasksInLists).FirstOrDefault((l) => l.ListID == ListID);

            if (list == null)
            {
                throw new ArgumentOutOfRangeException("listID not in table");
            }
            await Task.Run(() => dbContext.TasksInLists.RemoveRange(list.TasksInLists));

            await dbContext.SaveChangesAsync();

            //add new
            var newTils = allTasks.Where((x) => x.TaskID == null);

            if (newTils.Any())
            {
                var newTasks = newTils.Select((x) => new TaskItem()
                {
                    ID = null,
                    OriginalProjectID = list.ProjectID,
                    TaskName          = x.TaskName,
                    TasksInLists      = new List <TaskInList>()
                    {
                        x.ToEntity(ListID)
                    }
                });
                await dbContext.AddRangeAsync(newTasks);

                await dbContext.SaveChangesAsync();
            }


            //insert all tasks in list
            var newTilList = allTasks.Where((x) => x.TaskID != null).Select(t => t.ToEntity(ListID));
            await dbContext.TasksInLists.AddRangeAsync(newTilList);

            await dbContext.SaveChangesAsync();

            var nameTilList = allTasks.Where((x) => x.TaskID != null);

            foreach (var tl in nameTilList)
            {
                var t = await dbContext.TaskItems.SingleAsync((x) => x.ID == tl.TaskID);

                t.TaskName = tl.TaskName;
            }
            await dbContext.SaveChangesAsync();
        }