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); } } } }
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 <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); } }
///<inheritdoc/> public async Task <int> AddFather(OrphanageDataModel.Persons.Father father, OrphanageDbCNoBinary orphanageDBC) { _logger.Information($"Trying to add new father"); if (father == null) { _logger.Error($"the parameter object father is null, NullReferenceException will be thrown"); throw new NullReferenceException(); } if (father.Name == null) { _logger.Error($"the Name object of the parameter object father is null, NullReferenceException will be thrown"); throw new NullReferenceException(); } 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 ret = GetFathersByName(father.Name, orphanageDBC).FirstOrDefault(); if (ret != null) { _logger.Error($"father with id({father.Id}) has the same name, DuplicatedObjectException will be thrown"); throw new DuplicatedObjectException(father.GetType(), ret.GetType(), ret.Id); } else { _logger.Information($"didn't found any similar names to ({father.Name.FullName()}) in the database"); } } } var fatherName = father.Name; var taskFatherName = _regularDataService.AddName(fatherName, orphanageDBC); father.NameId = await taskFatherName; if (father.NameId == -1) { _logger.Warning($"Name object has not been added, nothing will be added, -1 will be returned"); return(-1); } father.Name = null; if (father.ActingUser != null) { father.ActingUser = null; } if (father.Families != null) { father.Families = null; } orphanageDBC.Fathers.Add(father); if (await orphanageDBC.SaveChangesAsync() == 1) { _logger.Information($"new father object with id {father.Id} has been added"); return(father.Id); } else { _logger.Warning($"something went wrong, nothing was added, -1 will be returned"); return(-1); } }
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); } } } }