Esempio n. 1
0
        public async Task <int> AddMother(OrphanageDataModel.Persons.Mother mother, OrphanageDbCNoBinary orphanageDBC)
        {
            if (mother == null)
            {
                throw new NullReferenceException();
            }
            if (mother.Name == null)
            {
                throw new NullReferenceException();
            }
            if (mother.Address == null)
            {
                throw new NullReferenceException();
            }

            if (!Properties.Settings.Default.ForceAdd)
            {
                if (Properties.Settings.Default.CheckName)
                {
                    var ret = GetMothersByName(mother.Name, orphanageDBC).FirstOrDefault();
                    if (ret != null)
                    {
                        throw new DuplicatedObjectException(mother.GetType(), ret.GetType(), ret.Id);
                    }
                }
                if (Properties.Settings.Default.CheckContactData)
                {
                    var ret = GetMothersByAddress(mother.Address, orphanageDBC).FirstOrDefault();
                    if (ret != null)
                    {
                        throw new DuplicatedObjectException(mother.GetType(), ret.GetType(), ret.Id);
                    }
                }
            }
            var motherName     = mother.Name;
            var motherAddress  = mother.Address;
            var taskMotherName = _regularDataService.AddName(motherName, orphanageDBC);

            mother.NameId = await taskMotherName;
            var taskMotherAddress = _regularDataService.AddAddress(motherAddress, orphanageDBC);

            mother.AddressId = await taskMotherAddress;
            if (mother.Address != null)
            {
                mother.Address = null;
            }
            if (mother.ActingUser != null)
            {
                mother.ActingUser = null;
            }
            if (mother.Families != null)
            {
                mother.Families = null;
            }
            if (mother.Name != null)
            {
                mother.Name = null;
            }
            orphanageDBC.Mothers.Add(mother);

            if (await orphanageDBC.SaveChangesAsync() == 1)
            {
                return(mother.Id);
            }
            else
            {
                return(-1);
            }
        }