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); } } } }
public async Task <int> SaveMother(OrphanageDataModel.Persons.Mother mother) { if (mother == null) { throw new NullReferenceException(); } using (OrphanageDbCNoBinary orphanageDc = new OrphanageDbCNoBinary()) { int ret = 0; orphanageDc.Configuration.AutoDetectChangesEnabled = true; var motherToReplace = await orphanageDc.Mothers.Include(m => m.Address).Where(m => m.Id == mother.Id).FirstAsync(); if (motherToReplace == null) { throw new ObjectNotFoundException(); } if (mother.Address != null) { if (motherToReplace.Address != null) { ret += await _regularDataService.SaveAddress(mother.Address, orphanageDc); } else { var addressId = await _regularDataService.AddAddress(mother.Address, orphanageDc); motherToReplace.AddressId = addressId; ret++; } } else if (motherToReplace.Address != null) { int alAdd = motherToReplace.AddressId.Value; motherToReplace.AddressId = null; await orphanageDc.SaveChangesAsync(); await _regularDataService.DeleteAddress(alAdd, orphanageDc); } ret += await _regularDataService.SaveName(mother.Name, orphanageDc); motherToReplace.Birthday = mother.Birthday; motherToReplace.ColorMark = mother.ColorMark; motherToReplace.DateOfDeath = mother.DateOfDeath; motherToReplace.HasSheOrphans = mother.HasSheOrphans; motherToReplace.HusbandName = mother.HusbandName; motherToReplace.IsDead = mother.IsDead; motherToReplace.IsMarried = mother.IsMarried; motherToReplace.Jop = mother.Jop; motherToReplace.NameId = mother.NameId; motherToReplace.Note = mother.Note; motherToReplace.Salary = mother.Salary; motherToReplace.Story = mother.Story; ret += await orphanageDc.SaveChangesAsync(); return(ret); } }
public async Task <bool> SaveFamily(OrphanageDataModel.RegularData.Family family) { _logger.Information($"trying to save family"); if (family == null) { _logger.Error($"family parameter is null, false will be returned"); return(false); } _logger.Information($"trying to save family with id({family.Id})"); using (var orphanageDbc = new OrphanageDbCNoBinary()) { int ret = 0; orphanageDbc.Configuration.LazyLoadingEnabled = true; orphanageDbc.Configuration.ProxyCreationEnabled = true; orphanageDbc.Configuration.AutoDetectChangesEnabled = true; var savedFamily = await orphanageDbc.Families. Include(f => f.AlternativeAddress). Include(f => f.PrimaryAddress) .Where(fam => fam.Id == family.Id).FirstOrDefaultAsync(); _logger.Information($"processing alternative address to the family with id({family.Id})"); if (family.AlternativeAddress != null) { if (savedFamily.AlternativeAddress == null) { var addressID = await _regularDataService.AddAddress(family.AlternativeAddress, orphanageDbc); savedFamily.AlternativeAddressId = addressID; } else { ret += await _regularDataService.SaveAddress(family.AlternativeAddress, orphanageDbc); } } else { if (savedFamily.AlternativeAddress != null) { int alAdd = savedFamily.AlternativeAddressId.Value; savedFamily.AlternativeAddressId = null; await orphanageDbc.SaveChangesAsync(); await _regularDataService.DeleteAddress(alAdd, orphanageDbc); } } _logger.Information($"processing primary address to the family with id({family.Id})"); if (family.PrimaryAddress != null) { if (savedFamily.PrimaryAddress == null) { var addressID = await _regularDataService.AddAddress(family.PrimaryAddress, orphanageDbc); savedFamily.AddressId = addressID; ret++; } else { ret += await _regularDataService.SaveAddress(family.PrimaryAddress, orphanageDbc); } } else { if (savedFamily.PrimaryAddress != null) { int Add = savedFamily.AddressId.Value; savedFamily.AddressId = null; await orphanageDbc.SaveChangesAsync(); await _regularDataService.DeleteAddress(Add, orphanageDbc); } } savedFamily.FatherId = family.FatherId; ret += await _fatherDbService.SaveFather(family.Father); savedFamily.MotherId = family.MotherId; ret += await _motherDbService.SaveMother(family.Mother); savedFamily.BailId = family.BailId; savedFamily.ColorMark = family.ColorMark; savedFamily.FinncialStatus = family.FinncialStatus; savedFamily.IsBailed = family.IsBailed; savedFamily.IsExcluded = family.IsExcluded; savedFamily.IsTheyRefugees = family.IsTheyRefugees; savedFamily.Note = family.Note; savedFamily.ResidenceStatus = family.ResidenceStatus; savedFamily.ResidenceType = family.ResidenceType; ret += await orphanageDbc.SaveChangesAsync(); if (ret > 0) { _logger.Information($"family with id({family.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); } } }
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); } } }
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); } } } }