Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 2
0
        public async Task <int> SaveFather(OrphanageDataModel.Persons.Father father)
        {
            _logger.Information($"Trying to save father");
            if (father == null)
            {
                _logger.Error($"the parameter object guarantor is null, NullReferenceException will be thrown");
                throw new NullReferenceException();
            }
            if (father.NameId <= 0)
            {
                _logger.Error($"the NameID of the parameter object father equals {father.NameId}, NullReferenceException will be thrown");
                throw new NullReferenceException();
            }
            using (OrphanageDbCNoBinary orphanageDc = new OrphanageDbCNoBinary())
            {
                int ret = 0;
                orphanageDc.Configuration.AutoDetectChangesEnabled = true;
                orphanageDc.Configuration.LazyLoadingEnabled       = true;
                orphanageDc.Configuration.ProxyCreationEnabled     = true;

                var fatherToReplace = await orphanageDc.Fathers.Where(f => f.Id == father.Id).FirstAsync();

                if (fatherToReplace == null)
                {
                    _logger.Error($"the original father object with id {father.Id} object is not founded, ObjectNotFoundException will be thrown");
                    throw new Exceptions.ObjectNotFoundException();
                }
                _logger.Information($"processing the name object of the father with id({father.Id})");
                ret += await _regularDataService.SaveName(father.Name, orphanageDc);

                fatherToReplace.Birthday           = father.Birthday;
                fatherToReplace.ColorMark          = father.ColorMark;
                fatherToReplace.DateOfDeath        = father.DateOfDeath;
                fatherToReplace.DeathReason        = father.DeathReason;
                fatherToReplace.IdentityCardNumber = father.IdentityCardNumber;
                fatherToReplace.Jop    = father.Jop;
                fatherToReplace.NameId = father.NameId;
                fatherToReplace.Note   = father.Note;
                fatherToReplace.Story  = father.Story;
                ret += await orphanageDc.SaveChangesAsync();

                if (ret > 0)
                {
                    _logger.Information($"father with id({father.Id}) has been successfully saved to the database, {ret} changes have been made");
                    return(ret);
                }
                else
                {
                    _logger.Information($"nothing has changed, 0 will be returned");
                    return(0);
                }
            }
        }
Exemplo n.º 3
0
        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);
                }
            }
        }
Exemplo n.º 4
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);
                }
            }
        }