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> SaveCaregiver(OrphanageDataModel.Persons.Caregiver caregiver) { _logger.Information($"Trying to save caregiver"); if (caregiver == null) { _logger.Error($"the parameter object caregiver is null, NullReferenceException will be thrown"); throw new NullReferenceException(); } if (caregiver.NameId <= 0) { _logger.Error($"the NameID of the parameter object caregiver equals {caregiver.NameId}, 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 orginalCaregiver = await orphanageDc.Caregivers. Include(m => m.Address). Include(c => c.Name). FirstOrDefaultAsync(m => m.Id == caregiver.Id); if (orginalCaregiver == null) { _logger.Error($"the original caregiver object with id {caregiver.Id} object is not founded, ObjectNotFoundException will be thrown"); throw new Exceptions.ObjectNotFoundException(); } _logger.Information($"processing the address object of the caregiver with id({caregiver.Id})"); if (caregiver.Address != null) { if (orginalCaregiver.Address != null) { //edit existing caregiver address ret += await _regularDataService.SaveAddress(caregiver.Address, orphanageDc); } else { //create new address for the caregiver var addressId = await _regularDataService.AddAddress(caregiver.Address, orphanageDc); orginalCaregiver.AddressId = addressId; ret++; } } else if (orginalCaregiver.Address != null) { //delete existing caregiver address int alAdd = orginalCaregiver.AddressId.Value; orginalCaregiver.AddressId = null; await orphanageDc.SaveChangesAsync(); await _regularDataService.DeleteAddress(alAdd, orphanageDc); } _logger.Information($"processing the name object of the caregiver with id({caregiver.Id})"); ret += await _regularDataService.SaveName(caregiver.Name, orphanageDc); orginalCaregiver.IdentityCardId = caregiver.IdentityCardId; orginalCaregiver.ColorMark = caregiver.ColorMark; orginalCaregiver.Income = caregiver.Income; orginalCaregiver.Jop = caregiver.Jop; orginalCaregiver.Note = caregiver.Note; ret += await orphanageDc.SaveChangesAsync(); if (ret > 0) { _logger.Information($"caregiver with id({caregiver.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> 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); } } }