Beispiel #1
0
        public async Task <IEnumerable <OrphanageDataModel.Persons.Guarantor> > GetGuarantors(int Uid)
        {
            IList <OrphanageDataModel.Persons.Guarantor> guarantorsList = new List <OrphanageDataModel.Persons.Guarantor>();

            using (var _orphanageDBC = new OrphanageDbCNoBinary())
            {
                var guarantors = await _orphanageDBC.Guarantors.AsNoTracking()
                                 .Include(g => g.Address)
                                 .Include(c => c.Name)
                                 .Include(g => g.Account)
                                 .Where(g => g.UserId == Uid)
                                 .ToListAsync();

                foreach (var guarantor in guarantors)
                {
                    OrphanageDataModel.Persons.Guarantor guarantorToFill = guarantor;
                    _selfLoopBlocking.BlockGuarantorSelfLoop(ref guarantorToFill);
                    guarantorsList.Add(guarantorToFill);
                }
            }
            return(guarantorsList);
        }
        public async Task <IEnumerable <OrphanageDataModel.Persons.Guarantor> > GetGuarantors(int Aid)
        {
            IList <OrphanageDataModel.Persons.Guarantor> returnedGuarantors = new List <OrphanageDataModel.Persons.Guarantor>();

            using (var dbContext = new OrphanageDbCNoBinary())
            {
                var guarantors = await(from guar in dbContext.Guarantors.AsNoTracking()
                                       where guar.AccountId == Aid
                                       select guar)
                                 .Include(g => g.Address)
                                 .Include(c => c.Name)
                                 .Include(g => g.Account)
                                 .ToListAsync();

                foreach (var guarantor in guarantors)
                {
                    var guarantorToFill = guarantor;
                    _selfLoopBlocking.BlockGuarantorSelfLoop(ref guarantorToFill);
                    returnedGuarantors.Add(guarantorToFill);
                }
            }
            return(returnedGuarantors);
        }
        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);
                    }
                }
            }
        }