Beispiel #1
0
        public async Task <OrphanageDataModel.Persons.Caregiver> AddCaregiver(OrphanageDataModel.Persons.Caregiver caregiver)
        {
            _logger.Information($"Trying to add new caregiver");
            if (caregiver == null)
            {
                _logger.Error($"the parameter object caregiverToAdd is null, NullReferenceException will be thrown");
                throw new NullReferenceException();
            }
            if (caregiver.Name == null)
            {
                _logger.Error($"the Name object of the parameter object caregiverToAdd is null, NullReferenceException will be thrown");
                throw new NullReferenceException();
            }
            using (var orphanageDBC = new OrphanageDbCNoBinary())
            {
                using (var Dbt = orphanageDBC.Database.BeginTransaction())
                {
                    if (!Properties.Settings.Default.ForceAdd)
                    {
                        _logger.Information($"ForceAdd option is not activated");
                        if (Properties.Settings.Default.CheckName)
                        {
                            _logger.Information($"CheckName option is activated, trying to get the equal names from database");
                            var retCaregivers = GetCaregiversByName(caregiver.Name, orphanageDBC).FirstOrDefault();
                            if (retCaregivers != null)
                            {
                                _logger.Error($"caregiver with id({retCaregivers.Id}) has the same name, DuplicatedObjectException will be thrown");
                                throw new DuplicatedObjectException(caregiver.GetType(), retCaregivers.GetType(), retCaregivers.Id);
                            }
                            else
                            {
                                _logger.Information($"didn't found any similar names to ({caregiver.Name.FullName()}) in the database");
                            }
                        }
                        if (Properties.Settings.Default.CheckContactData)
                        {
                            _logger.Information($"CheckContactData option is activated, trying to get the equal contact data for the caregiver address from database");
                            var retCaregivers = GetCaregiversByAddress(caregiver.Address, orphanageDBC).FirstOrDefault();
                            if (retCaregivers != null)
                            {
                                _logger.Error($"caregiver with id({retCaregivers.Id}) has the same address, DuplicatedObjectException will be thrown");
                                throw new DuplicatedObjectException(caregiver.GetType(), retCaregivers.GetType(), retCaregivers.Id);
                            }
                            else
                            {
                                _logger.Information($"didn't found any similar contact data to caregiver address in the database");
                            }
                        }
                    }
                    var nameId = await _regularDataService.AddName(caregiver.Name, orphanageDBC);

                    if (nameId == -1)
                    {
                        Dbt.Rollback();
                        _logger.Warning($"Name object has not been added, nothing will be added, null will be returned");
                        return(null);
                    }
                    caregiver.NameId = nameId;
                    if (caregiver.Address != null)
                    {
                        var addressId = await _regularDataService.AddAddress(caregiver.Address, orphanageDBC);

                        if (addressId == -1)
                        {
                            Dbt.Rollback();
                            _logger.Warning($"Address object has not been added, nothing will be added, null will be returned");
                            return(null);
                        }
                        caregiver.AddressId = addressId;
                    }

                    if (caregiver.Orphans != null && caregiver.Orphans.Count > 0)
                    {
                        caregiver.Orphans = null;
                    }
                    if (caregiver.ActingUser != null)
                    {
                        caregiver.ActingUser = null;
                    }
                    orphanageDBC.Caregivers.Add(caregiver);
                    var ret = await orphanageDBC.SaveChangesAsync();

                    if (ret >= 1)
                    {
                        Dbt.Commit();
                        _logger.Information($"new caregiver object with id {caregiver.Id} has been added");
                        _uriGenerator.SetCaregiverUris(ref caregiver);
                        _logger.Information($"the caregiver object with id {caregiver.Id}  will be returned");
                        return(caregiver);
                    }
                    else
                    {
                        Dbt.Rollback();
                        _logger.Warning($"something went wrong, nothing was added, null will be returned");
                        return(null);
                    }
                }
            }
        }
Beispiel #2
0
        public async Task <OrphanageDataModel.RegularData.Family> AddFamily(OrphanageDataModel.RegularData.Family family)
        {
            _logger.Information($"trying to add new family");
            if (family == null)
            {
                _logger.Error($"the given family parameter is null, null will be returned");
                return(null);
            }
            if (family.PrimaryAddress == null)
            {
                _logger.Error($"the given primary address of the given family parameter is null, null will be returned");
                return(null);
            }

            using (var orphanageDbc = new OrphanageDbCNoBinary())
            {
                using (var DbT = orphanageDbc.Database.BeginTransaction())
                {
                    if (!Properties.Settings.Default.ForceAdd)
                    {
                        _logger.Information($"ForceAdd option is not activated");
                        if (Properties.Settings.Default.CheckContactData)
                        {
                            _logger.Information($"CheckContactData option is activated, trying to get the equal contact data for the primary address from database");
                            var ret = GetFamiliesByAddress(family.PrimaryAddress, orphanageDbc).FirstOrDefault();
                            if (ret != null)
                            {
                                _logger.Error($"family with id({ret.Id}) has the same address, DuplicatedObjectException will be thrown");
                                throw new DuplicatedObjectException(family.GetType(), ret.GetType(), ret.Id);
                            }
                            else
                            {
                                _logger.Information($"didn't found any similar contact data to family primary address in the database");
                            }
                            if (family.AlternativeAddress != null)
                            {
                                _logger.Information($"CheckContactData option is activated, trying to get the equal contact data for the alternative address from database");
                                ret = GetFamiliesByAddress(family.AlternativeAddress, orphanageDbc).FirstOrDefault();
                                if (ret != null)
                                {
                                    _logger.Error($"family with id({ret.Id}) has the same address, DuplicatedObjectException will be thrown");
                                    throw new DuplicatedObjectException(family.GetType(), ret.GetType(), ret.Id);
                                }
                                else
                                {
                                    _logger.Information($"didn't found any similar contact data to family alternative address in the database");
                                }
                            }
                        }
                    }

                    var        addressPrim      = family.PrimaryAddress;
                    var        addressAlter     = family.AlternativeAddress;
                    var        father           = family.Father;
                    var        mother           = family.Mother;
                    var        taskPrimAddress  = _regularDataService.AddAddress(addressPrim, orphanageDbc);
                    Task <int> taskAlterAddress = null;
                    family.AddressId      = await taskPrimAddress;
                    family.PrimaryAddress = addressPrim;
                    if (family.AlternativeAddress != null)
                    {
                        taskAlterAddress = _regularDataService.AddAddress(addressAlter, orphanageDbc);
                    }
                    if (family.Orphans != null && family.Orphans.Count > 0)
                    {
                        family.Orphans = null;
                    }
                    if (taskAlterAddress != null)
                    {
                        family.AlternativeAddressId = await taskAlterAddress;
                        family.AlternativeAddress   = addressAlter;
                    }
                    // set father
                    var taskFather = _fatherDbService.AddFather(father, orphanageDbc);
                    family.FatherId = await taskFather;
                    family.Father   = null;
                    // set mother
                    var taskMother = _motherDbService.AddMother(mother, orphanageDbc);
                    family.MotherId = await taskMother;
                    family.Mother   = null;

                    if (family.Bail != null)
                    {
                        family.Bail = null;
                    }
                    if (family.ActingUser != null)
                    {
                        family.ActingUser = null;
                    }
                    if (family.Orphans != null)
                    {
                        family.Orphans = null;
                    }
                    orphanageDbc.Families.Add(family);
                    if (await orphanageDbc.SaveChangesAsync() > 0)
                    {
                        DbT.Commit();
                        _logger.Information($"family with id({family.Id}) has been successfully added to the database");
                    }
                    else
                    {
                        DbT.Rollback();
                        _logger.Information($"nothing has changed, family has not added, null will be returned");
                        return(null);
                    }
                }
            }
            return(await GetFamily(family.Id));
        }
        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);
            }
        }
Beispiel #4
0
        public async Task <OrphanageDataModel.Persons.User> AddUser(OrphanageDataModel.Persons.User user)
        {
            _logger.Information("trying to add new user");
            if (user == null)
            {
                _logger.Error("user is null, NullReferenceException will be thrown");
                throw new NullReferenceException();
            }
            using (var orphanageDBC = new OrphanageDbCNoBinary())
            {
                using (var Dbt = orphanageDBC.Database.BeginTransaction())
                {
                    int ret = 0;
                    if (user.Name != null)
                    {
                        var nameId = await _regularDataService.AddName(user.Name, orphanageDBC);

                        if (nameId > 0)
                        {
                            user.NameId = nameId;
                        }
                        else
                        {
                            Dbt.Rollback();
                            _logger.Warning($"Name object has not been added, nothing will be added, null will be returned");
                            return(null);
                        }
                    }
                    if (user.Address != null)
                    {
                        var addressId = await _regularDataService.AddAddress(user.Address, orphanageDBC);

                        if (addressId == -1)
                        {
                            Dbt.Rollback();
                            _logger.Warning($"Address object has not been added, nothing will be added, null will be returned");
                            return(null);
                        }
                        user.AddressId = addressId;
                    }
                    user.Accounts   = null;
                    user.Bails      = null;
                    user.Caregivers = null;
                    user.Famlies    = null;
                    user.Fathers    = null;
                    user.Guarantors = null;
                    user.Mothers    = null;
                    user.Orphans    = null;
                    _logger.Information("trying to hash the user password");
                    user.Password = _passwordHasher.Hash(user.Password);
                    orphanageDBC.Users.Add(user);
                    ret = await orphanageDBC.SaveChangesAsync();

                    if (ret >= 1)
                    {
                        Dbt.Commit();
                        _logger.Information($"new user object with id {user.Id} has been added");
                        _logger.Information($"the caregiver object with id {user.Id}  will be returned");
                        return(user);
                    }
                    else
                    {
                        Dbt.Rollback();
                        _logger.Warning($"something went wrong, nothing was added, null will be returned");
                        return(null);
                    }
                }
            }
        }
        public async Task <OrphanageDataModel.Persons.Guarantor> AddGuarantor(OrphanageDataModel.Persons.Guarantor guarantor)
        {
            _logger.Information($"Trying to add new Guarantor");
            if (guarantor == null)
            {
                _logger.Error($"the parameter object guarantorToAdd is null, NullReferenceException will be thrown");
                throw new NullReferenceException();
            }
            if (guarantor.Name == null)
            {
                _logger.Error($"the Name object of the parameter object guarantorToAdd is null, NullReferenceException will be thrown");
                throw new NullReferenceException();
            }
            if (guarantor.AccountId <= 0)
            {
                _logger.Error($"the AccountId of the parameter object guarantorToAdd equals {guarantor.AccountId}, NullReferenceException will be thrown");
                throw new NullReferenceException();
            }
            using (var orphanageDBC = new OrphanageDbCNoBinary())
            {
                using (var Dbt = orphanageDBC.Database.BeginTransaction())
                {
                    if (!Properties.Settings.Default.ForceAdd)
                    {
                        _logger.Information($"ForceAdd option is not activated");
                        if (Properties.Settings.Default.CheckName)
                        {
                            _logger.Information($"CheckName option is activated, trying to get the equal names from database");
                            var retguarantors = GetGuarantorByName(guarantor.Name, orphanageDBC).FirstOrDefault();
                            if (retguarantors != null)
                            {
                                _logger.Error($"guarantor with id({retguarantors.Id}) has the same name, DuplicatedObjectException will be thrown");
                                throw new DuplicatedObjectException(guarantor.GetType(), retguarantors.GetType(), retguarantors.Id);
                            }
                            else
                            {
                                _logger.Information($"didn't found any similar names to ({guarantor.Name.FullName()}) in the database");
                            }
                        }
                        if (Properties.Settings.Default.CheckContactData)
                        {
                            _logger.Information($"CheckContactData option is activated, trying to get the equal contact data for the guarantor address from database");
                            var retguarantors = GetGuarantorByAddress(guarantor.Address, orphanageDBC).FirstOrDefault();
                            if (retguarantors != null)
                            {
                                _logger.Warning($"guarantor with id({retguarantors.Id}) has the same address, no DuplicatedObjectException will be thrown");
                                //    throw new DuplicatedObjectException(guarantor.GetType(), retguarantors.GetType(), retguarantors.Id);
                            }
                            else
                            {
                                _logger.Information($"didn't found any similar contact data to guarantor address in the database");
                            }
                        }
                    }
                    var nameId = await _regularDataService.AddName(guarantor.Name, orphanageDBC);

                    if (nameId == -1)
                    {
                        Dbt.Rollback();
                        _logger.Warning($"Name object has not been added, nothing will be added, null will be returned");
                        return(null);
                    }
                    guarantor.NameId = nameId;
                    if (guarantor.Address != null)
                    {
                        var addressId = await _regularDataService.AddAddress(guarantor.Address, orphanageDBC);

                        if (addressId == -1)
                        {
                            Dbt.Rollback();
                            _logger.Warning($"Address object has not been added, nothing will be added, null will be returned");
                            return(null);
                        }
                        guarantor.AddressId = addressId;
                    }
                    if (guarantor.Orphans != null)
                    {
                        guarantor.Orphans = null;
                    }
                    if (guarantor.Account != null)
                    {
                        guarantor.Account = null;
                    }
                    if (guarantor.Address != null)
                    {
                        guarantor.Address = null;
                    }
                    if (guarantor.Name != null)
                    {
                        guarantor.Name = null;
                    }
                    if (guarantor.Bails != null)
                    {
                        guarantor.Bails = null;
                    }
                    if (guarantor.ActingUser != null)
                    {
                        guarantor.ActingUser = null;
                    }
                    orphanageDBC.Guarantors.Add(guarantor);
                    var ret = await orphanageDBC.SaveChangesAsync();

                    if (ret >= 1)
                    {
                        Dbt.Commit();
                        _logger.Information($"new guarantor object with id {guarantor.Id} has been added");
                        _selfLoopBlocking.BlockGuarantorSelfLoop(ref guarantor);
                        _logger.Information($"the guarantor object with id {guarantor.Id}  will be returned");
                        return(guarantor);
                    }
                    else
                    {
                        Dbt.Rollback();
                        _logger.Warning($"something went wrong, nothing was added, null will be returned");
                        return(null);
                    }
                }
            }
        }