Ejemplo n.º 1
0
        public async Task UpdatePlan(PlanDto dto)
        {
            var plan = await _planRepository.GetPlan(dto.Id);

            plan.Name = dto.Name;
            await _planRepository.UpdatePlan(plan);

            var allLevels = (await _planRepository.GetLevels(dto.PatrolId)).ToList();
            var allSkills = (await _planRepository.GetSkills(dto.PatrolId)).ToList();
            var allGroups = (await _groupRepository.GetGroupsForPatrol(dto.PatrolId)).ToList();

            //update existing sections
            var sections = await _planRepository.GetSectionsForPlan(dto.Id);

            foreach (var section in sections)
            {
                var newSection = dto.Sections.SingleOrDefault(x => x.Id == section.Id);

                if (newSection == null)
                {
                    //remove
                    await _planRepository.DeleteSection(section);
                }
                else
                {
                    section.Name  = newSection.Name;
                    section.Color = newSection.Color;
                    await _planRepository.UpdateSection(section);

                    //sync levels
                    var sectionLevels = (await _planRepository.GetSectionLevels(section.Id)).OrderBy(x => x.ColumnIndex).ToList();
                    foreach (var sectionLevel in sectionLevels)
                    {
                        var newSectionlevel = newSection.Levels.SingleOrDefault(x => x.Id == sectionLevel.Id);
                        if (newSectionlevel == null)
                        {
                            await _planRepository.DeleteSectionLevel(sectionLevel);
                        }
                        else
                        {
                            sectionLevel.ColumnIndex = newSectionlevel.ColumnIndex;
                            sectionLevel.LevelId     = newSectionlevel.Level.Id;
                            await _planRepository.UpdateSectionLevel(sectionLevel);
                        }
                    }

                    foreach (var newSectionLevel in newSection.Levels.Where(x => x.Id == default(int)))
                    {
                        var sectionLevel = new SectionLevel()
                        {
                            SectionId   = section.Id,
                            ColumnIndex = newSectionLevel.ColumnIndex,
                            LevelId     = newSectionLevel.Level.Id
                        };
                        await _planRepository.InsertSectionLevel(sectionLevel);
                    }

                    //sync skills
                    var sectionSkills = (await _planRepository.GetSectionSkills(section.Id)).OrderBy(x => x.RowIndex).ToList();
                    foreach (var sectionSkill in sectionSkills)
                    {
                        var newSectionSkill = newSection.Skills.SingleOrDefault(x => x.Id == sectionSkill.Id);
                        if (newSectionSkill == null)
                        {
                            await _planRepository.DeleteSectionSkill(sectionSkill);
                        }
                        else
                        {
                            sectionSkill.RowIndex = newSectionSkill.RowIndex;
                            sectionSkill.SkillId  = newSectionSkill.Skill.Id;
                            await _planRepository.UpdateSectionSkill(sectionSkill);
                        }
                    }

                    foreach (var newSectionSkill in newSection.Skills.Where(x => x.Id == default(int)))
                    {
                        var sectionSkill = new SectionSkill()
                        {
                            SectionId = section.Id,
                            RowIndex  = newSectionSkill.RowIndex,
                            SkillId   = newSectionSkill.Skill.Id
                        };
                        await _planRepository.InsertSectionSkill(sectionSkill);
                    }

                    //sync groups
                    var sectionGroups = (await _planRepository.GetSectionGroups(section.Id)).ToList();
                    foreach (var sectionGroup in sectionGroups)
                    {
                        var newSectionGroup = newSection.Groups.SingleOrDefault(x => x.Id == sectionGroup.Id);
                        if (newSectionGroup == null)
                        {
                            await _planRepository.DeleteSectionGroup(sectionGroup);
                        }
                        else
                        {
                            sectionGroup.GroupId = newSectionGroup.GroupId;
                            await _planRepository.UpdateSectionGroup(sectionGroup);
                        }
                    }

                    foreach (var newSectionGroup in newSection.Groups.Where(x => x.Id == default(int)))
                    {
                        var sectionGroup = new SectionGroup()
                        {
                            SectionId = section.Id,
                            GroupId   = newSectionGroup.GroupId
                        };
                        await _planRepository.InsertSectionGroup(sectionGroup);
                    }
                }
            }

            //create new sections
            foreach (var newSection in dto.Sections.Where(x => x.Id == default(int)))
            {
                //save new sections
                var section = new Section()
                {
                    Name     = newSection.Name,
                    PatrolId = plan.PatrolId,
                    Color    = newSection.Color
                };
                await _planRepository.InsertSection(section);

                var planSection = new PlanSection()
                {
                    PlanId    = plan.Id,
                    SectionId = section.Id
                };
                await _planRepository.InsertPlanSection(planSection);

                foreach (var newLevel in newSection.Levels)
                {
                    var sectionLevel = new SectionLevel()
                    {
                        SectionId   = section.Id,
                        ColumnIndex = newLevel.ColumnIndex,
                        LevelId     = newLevel.Level.Id
                    };
                    await _planRepository.InsertSectionLevel(sectionLevel);
                }

                foreach (var newSkill in newSection.Skills)
                {
                    var sectionSkill = new SectionSkill()
                    {
                        SectionId = section.Id,
                        RowIndex  = newSkill.RowIndex,
                        SkillId   = newSkill.Skill.Id
                    };
                    await _planRepository.InsertSectionSkill(sectionSkill);
                }
            }
        }