Пример #1
0
        private async Task AddSibling(FamilyTie tie, Person person, CancellationToken cancellationToken)
        {
            _context.FamilyTies.Add(new FamilyTie()
            {
                Person  = person,
                Parent1 = tie.Parent1,
                Parent2 = tie.Parent2
            });

            if (tie.Parent1 != null)
            {
                FamilyTie par1 = await _context.FamilyTies
                                 .FirstAsync(p => p.PersonId == tie.Parent1Id,
                                             cancellationToken);

                _context.FamilyTies.Add(new FamilyTie()
                {
                    Person         = tie.Parent1,
                    Parent1        = par1.Parent1,
                    Parent2        = par1.Parent2,
                    Child          = person,
                    MarriagePerson = tie.Parent2
                });
            }
            if (tie.Parent2 != null)
            {
                FamilyTie par2 = await _context.FamilyTies
                                 .FirstAsync(p => p.PersonId == tie.Parent2Id,
                                             cancellationToken);

                _context.FamilyTies.Add(new FamilyTie()
                {
                    Person         = tie.Parent2,
                    Parent1        = par2.Parent1,
                    Parent2        = par2.Parent2,
                    Child          = person,
                    MarriagePerson = tie.Parent1
                });
            }
        }
Пример #2
0
        public async Task <Unit> Handle(DeletePersonCommand request, CancellationToken cancellationToken)
        {
            Person person = await _context.People
                            .Include(p => p.FamilyTree)
                            .SingleOrDefaultAsync(p => p.CreatedBy.Equals(request.UserId) &&
                                                  p.Id == request.Id,
                                                  cancellationToken);

            if (person == null)
            {
                throw new NotFoundException(nameof(Person), request.Id);
            }

            bool isMainPerson = false;
            int  familyTreeId = person.FamilyTreeId;

            if (person.FamilyTree.MainPersonId == person.Id)
            {
                isMainPerson = true;
            }

            // Получение id первого челокека, который был создан в дереве
            // и является корнем дерева относительно данной системы
            int idMainPersonTree = person.FamilyTree.MainPersonId.Value;

            // Получение части дерева, которое будет удалено
            List <int> deleteLine = await _familyTreeService
                                    .GetPeopleDeleteList(familyTreeId, request.UserId, idMainPersonTree, person.Id, cancellationToken);

            int valDel = 0;

            for (int i = 0; i < deleteLine.Count; i++)
            {
                valDel = deleteLine[i];

                // Удаление всех уз и корректировка данных
                _context.FamilyTies.RemoveRange(_context.FamilyTies.Where(p => p.PersonId == valDel));
                List <int> ftList  = _context.FamilyTies.Where(p => p.Parent1Id == valDel).Select(p => p.Id).ToList();
                List <int> ftList2 = _context.FamilyTies.Where(p => p.Parent2Id == valDel).Select(p => p.Id).ToList();
                List <int> ftList3 = _context.FamilyTies.Where(p => p.MarriagePersonId == valDel).Select(p => p.Id).ToList();
                List <int> ftList4 = _context.FamilyTies.Where(p => p.ChildId == valDel).Select(p => p.Id).ToList();

                if (ftList != null && ftList.Count > 0)
                {
                    for (int k = 0; k < ftList.Count; k++)
                    {
                        int       id  = ftList[k];
                        FamilyTie ft1 = _context.FamilyTies.First(p => p.Id == id);
                        ft1.Parent1Id = null;
                        await _context.SaveChangesAsync(cancellationToken);
                    }

                    await _context.SaveChangesAsync(cancellationToken);
                }
                if (ftList2 != null && ftList2.Count > 0)
                {
                    for (int k = 0; k < ftList2.Count; k++)
                    {
                        int       id  = ftList2[k];
                        FamilyTie ft2 = _context.FamilyTies.First(p => p.Id == id);
                        ft2.Parent2Id = null;
                        await _context.SaveChangesAsync(cancellationToken);
                    }

                    await _context.SaveChangesAsync(cancellationToken);
                }
                if (ftList3 != null && ftList3.Count > 0)
                {
                    for (int k = 0; k < ftList3.Count; k++)
                    {
                        int       id  = ftList3[k];
                        FamilyTie ft3 = _context.FamilyTies.First(p => p.Id == id);
                        ft3.MarriagePersonId = null;
                        await _context.SaveChangesAsync(cancellationToken);
                    }

                    await _context.SaveChangesAsync(cancellationToken);
                }
                if (ftList4 != null && ftList4.Count > 0)
                {
                    int        val       = ftList4[0];
                    FamilyTie  ftchild   = _context.FamilyTies.First(p => p.Id == val);
                    List <int> ftListBro = _context.FamilyTies
                                           .Where(p => p.Parent1Id == ftchild.Parent1Id &&
                                                  p.Parent2Id == ftchild.Parent2Id)
                                           .Select(p => p.PersonId)
                                           .ToList();

                    ftListBro.Remove(ftchild.PersonId);

                    if (ftListBro.Count > 0)
                    {
                        for (int k = 0; k < ftList4.Count; k++)
                        {
                            int id = ftList4[k];
                            _context.FamilyTies.Remove(_context.FamilyTies.First(p => p.Id == id));
                            await _context.SaveChangesAsync(cancellationToken);
                        }
                    }
                    else
                    {
                        for (int k = 0; k < ftList4.Count; k++)
                        {
                            int       id  = ftList4[k];
                            FamilyTie ft4 = _context.FamilyTies.First(p => p.Id == id);
                            ft4.ChildId = null;
                            await _context.SaveChangesAsync(cancellationToken);
                        }
                    }


                    await _context.SaveChangesAsync(cancellationToken);
                }

                _context.People.Remove(_context.People.First(p => p.Id == valDel));

                await _context.SaveChangesAsync(cancellationToken);
            }

            if (isMainPerson)
            {
                var familyTree = await _context.FamilyTrees
                                 .Include(ft => ft.People)
                                 .SingleOrDefaultAsync(ft => ft.Id == familyTreeId,
                                                       cancellationToken);

                familyTree.MainPersonId = familyTree.People
                                          .FirstOrDefault()?.Id;
            }

            // Сохранение результатов
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Пример #3
0
        private async Task AddLover(FamilyTie tie, Person person, CancellationToken cancellationToken)
        {
            FamilyTie ft = new FamilyTie()
            {
                Person         = person,
                MarriagePerson = tie.Person
            };

            // Получить всех детей у tie.Person, у которых один из родителей отсутствует
            List <int> childrenList = new List <int>();

            childrenList = await _context.FamilyTies
                           .Where(p => (p.Parent1Id == tie.PersonId && p.Parent2 == null) ||
                                  (p.Parent1 == null && p.Parent2Id == tie.PersonId))
                           .Select(p => p.PersonId)
                           .Distinct()
                           .ToListAsync(cancellationToken);

            if (childrenList != null && childrenList.Count > 0)
            {
                for (int i = 0; i < childrenList.Count; i++)
                {
                    int val = childrenList[i];
                    // Добавление связи жены с детьми и второго родителя во главе жены
                    ft.ChildId = childrenList[i];
                    _context.FamilyTies.Add(ft);

                    // Изменение записи у детей
                    List <int> ftChildList = await _context.FamilyTies
                                             .Where(p => p.PersonId == val)
                                             .Select(p => p.Id)
                                             .ToListAsync(cancellationToken);

                    for (int t = 0; t < ftChildList.Count; t++)
                    {
                        int       valList = ftChildList[t];
                        FamilyTie ftChild = await _context.FamilyTies
                                            .FirstAsync(p => p.Id == valList,
                                                        cancellationToken);

                        if (ftChild.Parent1 == null)
                        {
                            ftChild.Parent1 = person;
                        }
                        else
                        {
                            ftChild.Parent2 = person;
                        }
                    }

                    // Изменение записи у родителя tie.PersonId
                    FamilyTie ftParent = await _context.FamilyTies
                                         .FirstAsync(p => p.PersonId == tie.PersonId &&
                                                     p.ChildId == val,
                                                     cancellationToken);

                    ftParent.MarriagePerson = person;
                }
            }
            else
            {
                // Добавление записи жены о связи с tie.PersonId без детей
                FamilyTie ft_1 = await _context.FamilyTies
                                 .FirstOrDefaultAsync(p => p.PersonId == tie.PersonId &&
                                                      p.MarriagePerson == null,
                                                      cancellationToken);

                if (ft_1 == null)
                {
                    _context.FamilyTies.Add(new FamilyTie()
                    {
                        Person         = tie.Person,
                        Parent1        = tie.Parent1,
                        Parent2        = tie.Parent2,
                        MarriagePerson = person
                    });
                }
                else
                {
                    ft_1.MarriagePerson = person;
                }

                _context.FamilyTies.Add(ft);
            }
        }
Пример #4
0
        private async Task AddChild(FamilyTie tie, Person person, CancellationToken cancellationToken)
        {
            _context.FamilyTies.Add(new FamilyTie()
            {
                Person  = person,
                Parent1 = tie.Person,
                Parent2 = tie.MarriagePerson
            });

            // Добавление связи Person и MarriagePerson с ребенком
            List <FamilyTie> ft_l = await _context.FamilyTies
                                    .Where(p => p.PersonId == tie.PersonId &&
                                           p.MarriagePersonId == tie.MarriagePersonId)
                                    .ToListAsync(cancellationToken);

            if (ft_l[0].Child == null)
            {
                FamilyTie ftc = await _context.FamilyTies
                                .FirstAsync(p => p.PersonId == tie.PersonId &&
                                            p.MarriagePersonId == tie.MarriagePersonId,
                                            cancellationToken);

                ftc.Child = person;

                if (tie.MarriagePerson != null)
                {
                    ftc = await _context.FamilyTies
                          .FirstAsync(p => p.PersonId == tie.MarriagePersonId &&
                                      p.MarriagePersonId == tie.PersonId,
                                      cancellationToken);

                    ftc.Child = person;
                }
            }
            else
            {
                _context.FamilyTies.Add(new FamilyTie()
                {
                    Person         = tie.Person,
                    Parent1        = tie.Parent1,
                    Parent2        = tie.Parent2,
                    Child          = person,
                    MarriagePerson = tie.MarriagePerson
                });

                if (tie.MarriagePerson != null)
                {
                    FamilyTie wife = await _context.FamilyTies
                                     .FirstAsync(p => p.PersonId == tie.MarriagePersonId,
                                                 cancellationToken);

                    if (wife.Child == null)
                    {
                        wife.Child = person;
                    }
                    else
                    {
                        _context.FamilyTies.Add(new FamilyTie()
                        {
                            Person         = wife.Person,
                            Parent1        = wife.Parent1,
                            Parent2        = wife.Parent2,
                            Child          = person,
                            MarriagePerson = tie.Person
                        });
                    }
                }
            }
        }
Пример #5
0
        public async Task <int> Handle(CreatePersonCommand request, CancellationToken cancellationToken)
        {
            FamilyTreeEntity familyTree = await _context.FamilyTrees
                                          .Where(t => t.CreatedBy.Equals(request.UserId) &&
                                                 t.Id == request.TreeId)
                                          .Include(ft => ft.People)
                                          .SingleOrDefaultAsync(cancellationToken);

            if (familyTree == null)
            {
                throw new NotFoundException(nameof(FamilyTreeEntity), request.TreeId);
            }

            Person person = new Person();

            if (familyTree.People.Count == 0)
            {
                FamilyTie tie = new FamilyTie();
                tie.Person = person;

                familyTree.MainPerson = person;

                _context.FamilyTies.Add(tie);
            }
            else
            {
                FamilyTie mainPersonTie = null;
                if (request.WifeId == 0)
                {
                    mainPersonTie = await _context.FamilyTies
                                    .FirstAsync(p => p.PersonId == request.MainPersonId,
                                                cancellationToken);
                }
                else
                {
                    mainPersonTie = await _context.FamilyTies
                                    .FirstAsync(p => p.PersonId == request.MainPersonId &&
                                                p.MarriagePersonId == request.WifeId,
                                                cancellationToken);
                }

                switch (request.PersonRelationType)
                {
                case PersonRelationType.Sibling:
                    await AddSibling(mainPersonTie, person, cancellationToken);

                    break;

                case PersonRelationType.Parent:
                    await AddParent(mainPersonTie, person, request.ParentNumber, cancellationToken);

                    break;

                case PersonRelationType.Child:
                    await AddChild(mainPersonTie, person, cancellationToken);

                    break;

                case PersonRelationType.Lover:
                    await AddLover(mainPersonTie, person, cancellationToken);

                    break;

                default:
                    break;
                }
            }

            familyTree.People.Add(person);

            CreateDefaults(request, person);

            _context.People.Add(person);
            await _context.SaveChangesAsync(cancellationToken);

            return(person.Id);
        }
Пример #6
0
        private async Task AddParent(FamilyTie tie, Person person, int parentNumber, CancellationToken cancellationToken)
        {
            FamilyTie ft = new FamilyTie()
            {
                Person = person
            };

            List <int> ftPar1List = new List <int>();

            // Добавление связи с женой, если имеется другой родитель
            if (tie.Parent1 != null)
            {
                ft.MarriagePerson = tie.Parent1;
                ftPar1List        = await _context.FamilyTies
                                    .Where(p => p.PersonId == tie.Parent1Id &&
                                           p.MarriagePerson == null)
                                    .Select(p => p.Id)
                                    .ToListAsync(cancellationToken);
            }
            if (tie.Parent2 != null)
            {
                ft.MarriagePerson = tie.Parent2;
                ftPar1List        = await _context.FamilyTies
                                    .Where(p => p.PersonId == tie.Parent2Id &&
                                           p.MarriagePerson == null)
                                    .Select(p => p.Id)
                                    .ToListAsync(cancellationToken);
            }

            for (int i = 0; i < ftPar1List.Count; i++)
            {
                int       val   = ftPar1List[i];
                FamilyTie ftPar = await _context.FamilyTies
                                  .FirstAsync(p => p.Id == val,
                                              cancellationToken);

                ftPar.MarriagePerson = person;
            }

            // Список всех братьев у person
            List <int> childrenList = new List <int>();

            if (tie.Parent1 != null || tie.Parent2 != null)
            {
                childrenList = await _context.FamilyTies
                               .Where(p => p.Parent1Id == tie.Parent1Id &&
                                      p.Parent2Id == tie.Parent2Id)
                               .Select(p => p.PersonId)
                               .ToListAsync(cancellationToken);
            }
            else
            {
                childrenList.Add(tie.PersonId);
            }

            // Добавление связи родителя с детьми и добавление в бд
            for (int i = 0; i < childrenList.Count; i++)
            {
                ft.ChildId = childrenList[i];
                _context.FamilyTies.Add(ft);
            }

            // Обновление полей у детей
            for (int i = 0; i < childrenList.Count; i++)
            {
                int       valChild     = childrenList[i];
                FamilyTie _contextTies = await _context.FamilyTies
                                         .FirstAsync(p => p.PersonId == valChild,
                                                     cancellationToken);

                if (parentNumber == 1)
                {
                    _contextTies.Parent1 = person;
                }
                else
                {
                    _contextTies.Parent2 = person;
                }
            }
        }