Ejemplo n.º 1
0
        public virtual async Task <GenericSearchResult <FulfillmentCenter> > SearchCentersAsync(FulfillmentCenterSearchCriteria criteria)
        {
            var cacheKey = CacheKey.With(GetType(), "SearchCentersAsync", criteria.GetCacheKey());

            return(await PlatformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(FulfillmentCenterCacheRegion.CreateChangeToken());
                var result = new GenericSearchResult <FulfillmentCenter>();
                using (var repository = RepositoryFactory())
                {
                    repository.DisableChangesTracking();

                    var sortInfos = GetSortInfos(criteria);
                    var query = GetFulfillmentCenterQuery(repository, criteria, sortInfos);

                    result.TotalCount = await query.CountAsync();

                    var fulfillmentCenterEntities = new FulfillmentCenterEntity[0];
                    if (criteria.Take > 0)
                    {
                        fulfillmentCenterEntities = await query.Skip(criteria.Skip).Take(criteria.Take).ToArrayAsync();
                    }

                    result.Results = fulfillmentCenterEntities
                                     .Select(x => x.ToModel(AbstractTypeFactory <FulfillmentCenter> .TryCreateInstance()))
                                     .ToList();
                }
                return result;
            }));
        }
Ejemplo n.º 2
0
        public async Task SaveChangesAsync(IEnumerable <FulfillmentCenter> fulfillmentCenters)
        {
            if (fulfillmentCenters == null)
            {
                throw new ArgumentNullException(nameof(fulfillmentCenters));
            }

            var pkMap          = new PrimaryKeyResolvingMap();
            var changedEntries = new List <GenericChangedEntry <FulfillmentCenter> >();

            using (var repository = _repositoryFactory())
            {
                var existEntities = await repository.GetFulfillmentCentersAsync(fulfillmentCenters.Where(x => !x.IsTransient()).Select(x => x.Id).ToArray());

                foreach (var changedCenter in fulfillmentCenters)
                {
                    var existEntity    = existEntities.FirstOrDefault(x => x.Id == changedCenter.Id);
                    var modifiedEntity = AbstractTypeFactory <FulfillmentCenterEntity> .TryCreateInstance().FromModel(changedCenter, pkMap);

                    if (existEntity != null)
                    {
                        changedEntries.Add(new GenericChangedEntry <FulfillmentCenter>(changedCenter, existEntity.ToModel(AbstractTypeFactory <FulfillmentCenter> .TryCreateInstance()), EntryState.Modified));
                        modifiedEntity.Patch(existEntity);
                    }
                    else
                    {
                        repository.Add(modifiedEntity);
                        changedEntries.Add(new GenericChangedEntry <FulfillmentCenter>(changedCenter, EntryState.Added));
                    }
                }

                await _eventPublisher.Publish(new FulfillmentCenterChangingEvent(changedEntries));

                await repository.UnitOfWork.CommitAsync();

                pkMap.ResolvePrimaryKeys();
                await _eventPublisher.Publish(new FulfillmentCenterChangedEvent(changedEntries));

                FulfillmentCenterCacheRegion.ExpireRegion();
            }
        }
Ejemplo n.º 3
0
        public async Task DeleteAsync(IEnumerable <string> ids)
        {
            using (var repository = _repositoryFactory())
            {
                var changedEntries = new List <GenericChangedEntry <FulfillmentCenter> >();
                var dbCenters      = await repository.GetFulfillmentCentersAsync(ids);

                foreach (var dbCenter in dbCenters)
                {
                    repository.Remove(dbCenter);
                    changedEntries.Add(new GenericChangedEntry <FulfillmentCenter>(dbCenter.ToModel(AbstractTypeFactory <FulfillmentCenter> .TryCreateInstance()), EntryState.Deleted));
                }

                await _eventPublisher.Publish(new FulfillmentCenterChangingEvent(changedEntries));

                await repository.UnitOfWork.CommitAsync();

                await _eventPublisher.Publish(new FulfillmentCenterChangedEvent(changedEntries));

                FulfillmentCenterCacheRegion.ExpireRegion();
            }
        }
Ejemplo n.º 4
0
        public async Task <IEnumerable <FulfillmentCenter> > GetByIdsAsync(IEnumerable <string> ids)
        {
            if (ids == null)
            {
                throw new ArgumentNullException(nameof(ids));
            }
            var cacheKey = CacheKey.With(GetType(), "GetByIdsAsync", string.Join("-", ids.OrderBy(x => x)));

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(FulfillmentCenterCacheRegion.CreateChangeToken());
                IEnumerable <FulfillmentCenter> result = null;
                using (var repository = _repositoryFactory())
                {
                    repository.DisableChangesTracking();

                    var fulfillmentCenters = await repository.GetFulfillmentCentersAsync(ids);
                    result = fulfillmentCenters
                             .Select(x => x.ToModel(AbstractTypeFactory <FulfillmentCenter> .TryCreateInstance())).ToArray();
                }
                return result;
            }));
        }
Ejemplo n.º 5
0
        public async Task <GenericSearchResult <FulfillmentCenter> > SearchCentersAsync(FulfillmentCenterSearchCriteria criteria)
        {
            var cacheKey = CacheKey.With(GetType(), "SearchCentersAsync", criteria.GetCacheKey());

            return(await _platformMemoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(FulfillmentCenterCacheRegion.CreateChangeToken());
                var result = new GenericSearchResult <FulfillmentCenter>();
                using (var repository = _repositoryFactory())
                {
                    repository.DisableChangesTracking();

                    var query = repository.FulfillmentCenters;
                    if (!string.IsNullOrEmpty(criteria.Keyword))
                    {
                        query = query.Where(x => x.Name.Contains(criteria.Keyword));
                    }

                    var sortInfos = criteria.SortInfos;
                    if (sortInfos.IsNullOrEmpty())
                    {
                        sortInfos = new[] { new SortInfo {
                                                SortColumn = "Name"
                                            } };
                    }

                    query = query.OrderBySortInfos(sortInfos);

                    result.TotalCount = await query.CountAsync();
                    var arrayFullfillmentCenters = await query.Skip(criteria.Skip).Take(criteria.Take).ToArrayAsync();
                    result.Results = arrayFullfillmentCenters
                                     .Select(x => x.ToModel(AbstractTypeFactory <FulfillmentCenter> .TryCreateInstance()))
                                     .ToList();
                }
                return result;
            }));
        }