Пример #1
0
        public async Task <IEnumerable <OrphanageDataModel.Persons.Caregiver> > GetCaregivers(int Uid)
        {
            IList <OrphanageDataModel.Persons.Caregiver> caregiversList = new List <OrphanageDataModel.Persons.Caregiver>();

            using (var _orphanageDBC = new OrphanageDbCNoBinary())
            {
                var caregivers = await _orphanageDBC.Caregivers.AsNoTracking()
                                 .Include(c => c.Address)
                                 .Include(c => c.Name)
                                 .Include(c => c.Orphans)
                                 .Where(c => c.UserId == Uid)
                                 .ToListAsync();

                foreach (var caregiver in caregivers)
                {
                    OrphanageDataModel.Persons.Caregiver caregiverToFill = caregiver;
                    _selfLoopBlocking.BlockCaregiverSelfLoop(ref caregiverToFill);
                    _uriGenerator.SetCaregiverUris(ref caregiverToFill);
                    caregiversList.Add(caregiverToFill);
                }
            }
            return(caregiversList);
        }
Пример #2
0
        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);
                    }
                }
            }
        }