예제 #1
0
        public static Pedigree FromPedigreeDTOToPedigree(PedigreeDTO pedigreeDTO)
        {
            if (pedigreeDTO == null)
            {
                return(null);
            }

            var pedigree = new Pedigree
            {
                Father               = pedigreeDTO.Father,
                Father_Father        = pedigreeDTO.Father_Father,
                Father_Mother        = pedigreeDTO.Father_Mother,
                Father_Father_Father = pedigreeDTO.Father_Father_Father,
                Father_Father_Mother = pedigreeDTO.Father_Father_Mother,
                Father_Mother_Father = pedigreeDTO.Father_Mother_Father,
                Father_Mother_Mother = pedigreeDTO.Father_Mother_Mother,
                Mother               = pedigreeDTO.Mother,
                Mother_Father        = pedigreeDTO.Mother_Father,
                Mother_Mother        = pedigreeDTO.Mother_Mother,
                Mother_Father_Father = pedigreeDTO.Mother_Father_Father,
                Mother_Father_Mother = pedigreeDTO.Mother_Father_Mother,
                Mother_Mother_Father = pedigreeDTO.Mother_Mother_Father,
                Mother_Mother_Mother = pedigreeDTO.Mother_Mother_Mother
            };

            return(pedigree);
        }
예제 #2
0
        public PedigreeDTO AddChild(int id, PersonInfoDTO personInfo)
        {
            User loggedUser = this.Authenticate();

            if (!ModelState.IsValid)
            {
                string errorMessage = GetModelStateErrors(ModelState);
                throw new FamilyValidationException(errorMessage);
            }

            Person parent = this.data.People.GetFull(id, loggedUser.Id);

            if (parent == null)
            {
                throw new FamilyException("No person found. Maybe the person is already deleted.");
            }

            Person dbPerson = this.map.ToSinglePerson(personInfo);

            dbPerson.PedigreeId = parent.PedigreeId;
            parent.ChildrenFirst.Add(dbPerson);
            if (parent.Spouse != null)
            {
                parent.Spouse.ChildrenSecond.Add(dbPerson);
            }

            this.data.Save();
            Pedigree    pedigree       = this.data.Pedigrees.GetById(loggedUser.Id, dbPerson.PedigreeId);
            PedigreeDTO outputPedigree = this.map.ToSinglePedigreeDTO(pedigree);

            return(outputPedigree);
        }
예제 #3
0
        public PedigreeDTO Update(int id, PersonInfoDTO personInfo)
        {
            User loggedUser = this.Authenticate();

            if (!ModelState.IsValid)
            {
                string errorMessage = GetModelStateErrors(ModelState);
                throw new FamilyValidationException(errorMessage);
            }

            Person dbPerson = this.data.People.GetById(id);

            if (dbPerson == null)
            {
                throw new FamilyException("No person found. Maybe the person is already deleted.");
            }

            this.map.UpdatePerson(personInfo, dbPerson);

            this.data.Save();
            Pedigree    pedigree       = this.data.Pedigrees.GetById(loggedUser.Id, dbPerson.PedigreeId);
            PedigreeDTO outputPedigree = this.map.ToSinglePedigreeDTO(pedigree);

            return(outputPedigree);
        }
예제 #4
0
        public PedigreeDTO AddParent(int id, PersonInfoDTO personInfo)
        {
            User loggedUser = this.Authenticate();

            if (!ModelState.IsValid)
            {
                string errorMessage = GetModelStateErrors(ModelState);
                throw new FamilyValidationException(errorMessage);
            }

            Person child = this.data.People.GetFull(id, loggedUser.Id);

            if (child == null)
            {
                throw new FamilyException("No person found. Maybe the person is already deleted.");
            }

            if (child.FirstParent != null && child.SecondParent != null)
            {
                throw new FamilyException("You can't add a third parent to a person. Consider deleting or editing an existing one.");
            }

            Person parentToAdd = this.map.ToSinglePerson(personInfo);

            parentToAdd.PedigreeId = child.PedigreeId;
            parentToAdd.Spouse     = child.FirstParent ?? child.SecondParent;
            if (parentToAdd.Spouse == null)
            {
                child.FirstParent = parentToAdd;
            }
            else
            {
                parentToAdd.Spouse.Spouse = parentToAdd;
                IEnumerable <Person> childrenFirst  = parentToAdd.Spouse.ChildrenFirst.ToArray();
                IEnumerable <Person> childrenSecond = parentToAdd.Spouse.ChildrenSecond.ToArray();
                IEnumerable <Person> children       = childrenFirst.Union(childrenSecond);
                foreach (Person singleChild in children)
                {
                    if (singleChild.FirstParent == null)
                    {
                        singleChild.FirstParent = parentToAdd;
                    }
                    else if (singleChild.SecondParent == null)
                    {
                        singleChild.SecondParent = parentToAdd;
                    }
                }
            }

            this.data.Save();
            Pedigree    pedigree       = this.data.Pedigrees.GetById(loggedUser.Id, parentToAdd.PedigreeId);
            PedigreeDTO outputPedigree = this.map.ToSinglePedigreeDTO(pedigree);

            return(outputPedigree);
        }
예제 #5
0
        public PedigreeDTO Delete(int id)
        {
            User loggedUser = this.Authenticate();

            Person person = this.data.People.GetFull(id, loggedUser.Id);

            if (person == null)
            {
                throw new FamilyException("No person found. Maybe the person is already deleted.");
            }
            if (person.Pedigree.People.Count() == 1)
            {
                throw new FamilyException("The person can't be deleted because he is the only person in the pedigree. Consider deleteing the whole pedigree.");
            }

            IEnumerable <Person> personChildren = person.ChildrenFirst.Union(person.ChildrenSecond);

            if (person.Spouse == null && personChildren.Count() > 1)
            {
                throw new FamilyException(String.Format(
                                              "{0} has no spouse but has more than one children. Such a person can't be deleted because the pedigree will be divided in parts.", person.DisplayName));
            }
            // If the person has any parents and has spouse or children he can't be deleted
            if ((person.FirstParentId != null || person.SecondParentId != null) && (person.SpouseId != null || personChildren.Count() > 0))
            {
                throw new FamilyException("The person can't be deleted because the pedigree will be divided in two parts. To delete the person, you must first delete the people in one of the parts.");
            }

            if (person.Spouse != null)
            {
                person.Spouse.Spouse = null;
            }

            foreach (Person child in personChildren)
            {
                if (child.FirstParentId == person.Id)
                {
                    child.FirstParentId = null;
                }
                else if (child.SecondParentId == person.Id)
                {
                    child.SecondParentId = null;
                }
            }

            this.data.People.Delete(person);
            this.data.Save();
            Pedigree    pedigree       = this.data.Pedigrees.GetById(loggedUser.Id, person.PedigreeId);
            PedigreeDTO outputPedigree = this.map.ToSinglePedigreeDTO(pedigree);

            return(outputPedigree);
        }
예제 #6
0
        public IHttpActionResult GetPedigree(int id)
        {
            User     loggedUser = this.Authenticate();
            Pedigree pedigree   = this.data.Pedigrees.GetById(loggedUser.Id, id);

            if (pedigree == null)
            {
                throw new FamilyException("The pedigree doesn't exists. Maybe it was deleted.");
            }

            PedigreeDTO outputPedigree = this.map.ToSinglePedigreeDTO(pedigree);

            return(Ok(outputPedigree));
        }
예제 #7
0
        public PedigreeDTO AddSpouse(int id, PersonInfoDTO personInfo)
        {
            User loggedUser = this.Authenticate();

            if (!ModelState.IsValid)
            {
                string errorMessage = GetModelStateErrors(ModelState);
                throw new FamilyValidationException(errorMessage);
            }

            Person person = this.data.People.GetFull(id, loggedUser.Id);

            if (person == null)
            {
                throw new FamilyException("No person found. Maybe the person is already deleted.");
            }

            if (person.Spouse != null)
            {
                throw new FamilyException("The person already has a spouse. Consider deleting or editing the existing one.");
            }

            Person dbSpouse = this.map.ToSinglePerson(personInfo);

            dbSpouse.PedigreeId = person.PedigreeId;
            // Adding spouse relashionship
            dbSpouse.Spouse = person;
            person.Spouse   = dbSpouse;
            // Adding child -> parent relashionship
            dbSpouse.ChildrenFirst  = person.ChildrenSecond;
            dbSpouse.ChildrenSecond = person.ChildrenFirst;

            this.data.Save();
            Pedigree    pedigree       = this.data.Pedigrees.GetById(loggedUser.Id, dbSpouse.PedigreeId);
            PedigreeDTO outputPedigree = this.map.ToSinglePedigreeDTO(pedigree);

            return(outputPedigree);
        }