Пример #1
0
        public async Task <bool> DeleteCaregiver(int Cid)
        {
            _logger.Information($"trying to delete Caregiver with id({Cid})");
            if (Cid <= 0)
            {
                _logger.Error($"the integer parameter Caregiver ID is less than zero, false will be returned");
                throw new NullReferenceException();
            }
            using (var orphanageDb = new OrphanageDbCNoBinary())
            {
                using (var dbT = orphanageDb.Database.BeginTransaction())
                {
                    int ret       = 0;
                    var caregiver = await orphanageDb.Caregivers.Where(c => c.Id == Cid)
                                    .Include(c => c.Name)
                                    .Include(c => c.Address)
                                    .Include(c => c.Orphans)
                                    .FirstOrDefaultAsync();

                    if (caregiver == null)
                    {
                        _logger.Error($"the original Caregiver object with id {Cid} object is not founded, ObjectNotFoundException will be thrown");
                        throw new ObjectNotFoundException();
                    }
                    if (caregiver.Orphans.Count > 0)
                    {
                        //the caregiver has another orphans
                        _logger.Error($"the Caregiver object with id {Cid} has not null foreign key on Orphans table, HasForeignKeyException will be thrown");
                        throw new HasForeignKeyException(typeof(OrphanageDataModel.Persons.Caregiver), typeof(OrphanageDataModel.Persons.Orphan));
                    }
                    var caregiverName    = caregiver.Name;
                    var caregiverAddress = caregiver.Address;
                    orphanageDb.Caregivers.Remove(caregiver);
                    ret += await orphanageDb.SaveChangesAsync();

                    ret += await _regularDataService.DeleteName(caregiverName.Id, orphanageDb) ? 1 : 0;

                    if (caregiverAddress != null)
                    {
                        ret += await _regularDataService.DeleteAddress(caregiverAddress.Id, orphanageDb) ? 1 : 0;
                    }
                    if (ret > 0)
                    {
                        dbT.Commit();
                        _logger.Information($"Caregiver with id({Cid}) has been successfully deleted from the database");
                        return(true);
                    }
                    else
                    {
                        dbT.Rollback();
                        _logger.Information($"nothing has changed, false will be returned");
                        return(false);
                    }
                }
            }
        }
Пример #2
0
        public async Task <bool> SaveUser(OrphanageDataModel.Persons.User user)
        {
            _logger.Information($"Trying to save user");
            if (user == null)
            {
                _logger.Error($"the parameter object user is null, NullReferenceException will be thrown");
                throw new NullReferenceException();
            }
            if (user.UserName == null || user.UserName.Length == 0)
            {
                _logger.Error($"the UserName of the parameter object user equals {user.UserName}, NullReferenceException will be thrown");
                throw new NullReferenceException();
            }
            using (OrphanageDbCNoBinary orphanageDc = new OrphanageDbCNoBinary())
            {
                int ret = 0;
                orphanageDc.Configuration.LazyLoadingEnabled       = true;
                orphanageDc.Configuration.ProxyCreationEnabled     = true;
                orphanageDc.Configuration.AutoDetectChangesEnabled = true;

                var orginalUser = await orphanageDc.Users.
                                  Include(m => m.Address).
                                  Include(c => c.Name).
                                  FirstOrDefaultAsync(m => m.Id == user.Id);

                if (orginalUser == null)
                {
                    _logger.Error($"the original user object with id {user.Id} object is not founded, ObjectNotFoundException will be thrown");
                    throw new Exceptions.ObjectNotFoundException();
                }

                _logger.Information($"processing the address object of the user with id({user.Id})");
                if (user.Address != null)
                {
                    if (orginalUser.Address != null)
                    {
                        //edit existing user address
                        ret += await _regularDataService.SaveAddress(user.Address, orphanageDc);
                    }
                    else
                    {
                        //create new address for the user
                        var addressId = await _regularDataService.AddAddress(user.Address, orphanageDc);

                        orginalUser.AddressId = addressId;
                        ret++;
                    }
                }
                else
                {
                    if (orginalUser.Address != null)
                    {
                        //delete existing user address
                        int alAdd = orginalUser.AddressId.Value;
                        orginalUser.AddressId = null;
                        await orphanageDc.SaveChangesAsync();

                        await _regularDataService.DeleteAddress(alAdd, orphanageDc);
                    }
                }
                if (user.Name != null)
                {
                    if (orginalUser.Name != null)
                    {
                        //edit existing user name
                        ret += await _regularDataService.SaveName(user.Name, orphanageDc);
                    }
                    else
                    {
                        //create new name for the user
                        var nameId = await _regularDataService.AddName(user.Name, orphanageDc);

                        orginalUser.NameId = nameId;
                        ret++;
                    }
                }
                else
                {
                    if (orginalUser.Name != null)
                    {
                        //delete existing user name
                        int alAdd = orginalUser.NameId.Value;
                        orginalUser.NameId = null;
                        await orphanageDc.SaveChangesAsync();

                        await _regularDataService.DeleteName(alAdd, orphanageDc);
                    }
                }
                orginalUser.CanAdd     = user.CanAdd;
                orginalUser.CanDelete  = user.CanDelete;
                orginalUser.CanDeposit = user.CanDeposit;
                orginalUser.CanDraw    = user.CanDraw;
                orginalUser.CanRead    = user.CanRead;
                orginalUser.IsAdmin    = user.IsAdmin;
                orginalUser.Password   = _passwordHasher.Hash(user.Password);
                orginalUser.UserName   = user.UserName;
                orginalUser.Note       = user.Note;
                ret += await orphanageDc.SaveChangesAsync();

                if (ret > 0)
                {
                    _logger.Information($"user with id({user.Id}) has been successfully saved to the database, {ret} changes have been made");
                    return(true);
                }
                else
                {
                    _logger.Information($"nothing has changed, false will be returned");
                    return(false);
                }
            }
        }
Пример #3
0
        public async Task <bool> DeleteGuarantor(int Gid, bool forceDelete)
        {
            _logger.Information($"trying to delete Guarantor with id({Gid})");
            if (Gid <= 0)
            {
                _logger.Error($"the integer parameter Guarantor ID is less than zero, false will be returned");
                return(false);
            }
            using (var orphanageDb = new OrphanageDbCNoBinary())
            {
                using (var dbT = orphanageDb.Database.BeginTransaction())
                {
                    int ret       = 0;
                    var guarantor = await orphanageDb.Guarantors.Where(c => c.Id == Gid)
                                    .Include(c => c.Name)
                                    .Include(c => c.Address)
                                    .Include(c => c.Bails)
                                    .Include(c => c.Orphans)
                                    .FirstOrDefaultAsync();

                    if (guarantor == null)
                    {
                        _logger.Error($"the original guarantor object with id {Gid} object is not founded, ObjectNotFoundException will be thrown");
                        throw new Exceptions.ObjectNotFoundException();
                    }
                    if (guarantor.Orphans != null && guarantor.Orphans.Count > 0)
                    {
                        //the guarantor has another orphans
                        if (forceDelete)
                        {
                            _logger.Warning($"the guarantor object with id {Gid} has not null foreign key on Orphans table, all related orphan object will be not bailed");
                            var orphansIds = guarantor.Orphans.Select(o => o.Id).ToList();
                            var succeeded  = await _orphanDbService.BailOrphans(-1, orphansIds);

                            if (succeeded)
                            {
                                _logger.Information($"all related orphans to the guarantor with id ({Gid}) are set to not bailed");
                            }
                            else
                            {
                                _logger.Error($"failed to set orphans to not bailed");
                                dbT.Rollback();
                                return(false);
                            }
                        }
                        else
                        {
                            _logger.Error($"the guarantor object with id {Gid} has not null foreign key on Orphans table, HasForeignKeyException will be thrown");
                            throw new HasForeignKeyException(typeof(OrphanageDataModel.Persons.Guarantor), typeof(OrphanageDataModel.Persons.Orphan));
                        }
                    }
                    if (guarantor.Bails != null && guarantor.Bails.Count > 0)
                    {
                        // the guarantor has bails foreign keys
                        if (forceDelete)
                        {
                            _logger.Warning($"the guarantor object with id {Gid} has not null foreign key on Bails table, all related bails objects will be deleted");
                            foreach (var bail in guarantor.Bails)
                            {
                                var deleted = await _bailDbService.DeleteBail(bail.Id, forceDelete);

                                if (!deleted)
                                {
                                    _logger.Error($"cannot delete guarantor with the id ({Gid}) because it has a undeleted records on bail table, false will be returned");
                                    dbT.Rollback();
                                    return(false);
                                }
                            }
                        }
                        else
                        {
                            _logger.Error($"the guarantor object with id {Gid} has not null foreign key on Bails table, HasForeignKeyException will be thrown");
                            throw new HasForeignKeyException(typeof(OrphanageDataModel.Persons.Guarantor), typeof(OrphanageDataModel.FinancialData.Bail));
                        }
                    }
                    var guarantorName    = guarantor.Name;
                    var guarantorAdderss = guarantor.Address;
                    orphanageDb.Guarantors.Remove(guarantor);
                    ret += await orphanageDb.SaveChangesAsync();

                    ret += await _regularDataService.DeleteName(guarantorName.Id, orphanageDb) ? 1 : 0;

                    if (guarantorAdderss != null)
                    {
                        ret += await _regularDataService.DeleteAddress(guarantorAdderss.Id, orphanageDb) ? 1 : 0;
                    }
                    if (ret > 0)
                    {
                        dbT.Commit();
                        _logger.Information($"guarantor with id({Gid}) has been successfully deleted from the database");
                        return(true);
                    }
                    else
                    {
                        dbT.Rollback();
                        _logger.Information($"nothing has changed, false will be returned");
                        return(false);
                    }
                }
            }
        }