Esempio n. 1
0
        public ServiceResult UpdateCurrentPrice(Guid id, decimal price)
        {
            var stock = _StockQuery.Get(id);

            if (stock == null)
            {
                return(ServiceResult.NotFound());
            }

            if (stock.EffectivePeriod.FromDate == Date.MinValue)
            {
                return(ServiceResult.Error("Stock is not listed"));
            }

            if (stock.EffectivePeriod.ToDate != Date.MaxValue)
            {
                return(ServiceResult.Error("Stock is delisted"));
            }

            if (price < 0.00m)
            {
                return(ServiceResult.Error("Closing price is negative"));
            }

            var stockPriceHistory = _StockPriceHistoryCache.Get(id);

            stockPriceHistory.UpdateCurrentPrice(price);

            return(ServiceResult.Ok());
        }
Esempio n. 2
0
        public async Task <IEnumerable <string> > GetNotFoundIds(string[] ids)
        {
            var cachedIds = Cache.Get(ids);

            if (cachedIds.Count() == ids.Length && cachedIds.All(e => e != null))
            {
                return(null);
            }
            return(await LookupRepository.GetNotFoundIds(ids));
        }
Esempio n. 3
0
        public async Task <CommunityGoal> GetAsync(int id, CancellationToken cancellationToken = default)
        {
            await _lock.WaitAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                CommunityGoal  result;
                CachingOptions cachingOptions = _cachingOptions.Get(CacheOptionName);
                if (cachingOptions.Enabled && (result = _cgCache.Get(id)) != null)
                {
                    _log.LogTrace("Community goal {ID} ({Name}) found in cache", id, result.Name);
                    return(result);
                }

                // get from DB
                _log.LogTrace("Retrieving community goal {ID} from history database", id);
                result = await _collection.Find(dbData => dbData.ID == id).FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);

                if (result != null)
                {
                    _cgCache.AddOrReplace(result.ID, result, cachingOptions.Lifetime);
                }
                return(result);
            }
            finally
            {
                _lock.Release();
            }
        }
Esempio n. 4
0
        public async Task <UserData> GetAsync(ulong userID, CancellationToken cancellationToken = default)
        {
            await _lock.WaitAsync(cancellationToken).ConfigureAwait(false);

            try
            {
                UserData       result;
                CachingOptions cachingOptions = _cachingOptions.Get(CacheOptionName);
                if (cachingOptions.Enabled && (result = _userDataCache.Get(userID)) != null)
                {
                    _log.LogTrace("User data for user {UserID} found in cache", userID);
                    return(result);
                }

                // get from DB
                _log.LogTrace("Retrieving user data for user {UserID} from database", userID);
                result = await _collection.Find(dbData => dbData.ID == userID).FirstOrDefaultAsync(cancellationToken).ConfigureAwait(false);

                // if not found, return default data
                if (result == null)
                {
                    _log.LogTrace("User data for user {UserID} not found, creating new with defaults", userID);
                    result = new UserData(userID);
                }

                _userDataCache.AddOrReplace(result.ID, result, cachingOptions.Lifetime);
                return(result);
            }
            finally
            {
                _lock.Release();
            }
        }
Esempio n. 5
0
 public IEnumerable <TEntity> GetEntities(QueryRequest queryRequest)
 {
     if (queryRequestToIdsCache.ContainsKey(queryRequest))
     {
         var ids         = queryRequestToIdsCache[queryRequest];
         var cahcedCount = ids.Where(id => Cache.IsCached(id)).Count();
         if (cahcedCount >= ids.Count() - 1)
         {
             return(Cache.Get(ids));
         }
     }
     return(null);
 }
        public void Handle(IEnumerator <IProgrammeCreated> eventEnumerator)
        {
            InitializeCaches();

            var prgDictToProcess        = new HashSet <ProgrammeDictionary>();
            var prgCatToProcess         = new List <ProgrammeCategoryEntity>();
            var prgToProcess            = new List <ProgrammeEntity>();
            var prgCatLinkToProcess     = new List <ProgrammeCategoryLink>();
            var prgDictEpisodeToProcess = new HashSet <ProgrammeEpisode>();
            var scheduleToProcess       = new List <ScheduleEntity>();
            var schPrgToProcess         = new List <ScheduleProgrammeEntity>();

            while (eventEnumerator.MoveNext())
            {
                var eventModel = eventEnumerator.Current;

                if (eventModel is null)
                {
                    continue;
                }

                var salesArea = _salesAreaCache.Get(eventModel.SalesArea);
                if (salesArea is null)
                {
                    throw new DataSyncException(DataSyncErrorCode.SalesAreaNotFound,
                                                $"Invalid Sales Area: {eventModel.SalesArea}");
                }

                if (!(eventModel.ProgrammeCategories is null) && eventModel.ProgrammeCategories.Any())
                {
                    var invalidCategories = eventModel.ProgrammeCategories
                                            .Except(_categoryHierarchyNames, StringComparer.InvariantCultureIgnoreCase).ToArray();
                    if (invalidCategories.Length != 0)
                    {
                        throw new DataSyncException(DataSyncErrorCode.ProgrammeCategoryNotFound,
                                                    "Invalid programme categories: " + string.Join(",", invalidCategories));
                    }
                }

                var programmeDictionary = _programmeDictionaryCache.Get(eventModel.ExternalReference);

                if (programmeDictionary is null)
                {
                    programmeDictionary = _mapper.Map <ProgrammeDictionary>(eventModel);
                    _programmeDictionaryCache.Add(programmeDictionary);
                }
                else
                {
                    programmeDictionary.Name           = eventModel.ProgrammeName;
                    programmeDictionary.Description    = eventModel.Description;
                    programmeDictionary.Classification = eventModel.Classification;
                }

                _ = prgDictToProcess.Add(programmeDictionary);

                var programme = _mapper.Map <ProgrammeEntity>(eventModel);
                programme.Id = Guid.NewGuid();
                programme.ProgrammeDictionary = programmeDictionary;
                programme.SalesAreaId         = salesArea.Id;
                _prgtNoCounter.Process(programme);
                prgToProcess.Add(programme);

                foreach (var catName in eventModel.ProgrammeCategories ?? Enumerable.Empty <string>())
                {
                    var category = _programmeCategoryCache.GetOrAdd(catName, key =>
                    {
                        var newCat = new ProgrammeCategoryEntity {
                            Name = key
                        };
                        prgCatToProcess.Add(newCat);

                        return(newCat);
                    });

                    if (programme.ProgrammeCategoryLinks.All(x => !string.Equals(x.ProgrammeCategory.Name, category.Name)))
                    {
                        var link = new ProgrammeCategoryLink
                        {
                            ProgrammeCategory = category,
                            Programme         = programme,
                            ProgrammeId       = programme.Id
                        };

                        programme.ProgrammeCategoryLinks.Add(link);
                        prgCatLinkToProcess.Add(link);
                    }
                }

                if (!(eventModel.Episode is null))
                {
                    var episode = programmeDictionary.ProgrammeEpisodes.FirstOrDefault(x => x.Number == eventModel.Episode.Number);

                    if (episode is null)
                    {
                        episode = _mapper.Map <ProgrammeEpisodeEntity>(eventModel.Episode);
                        episode.ProgrammeDictionary = programmeDictionary;

                        programmeDictionary.ProgrammeEpisodes.Add(episode);
                    }
                    else
                    {
                        episode.Name = eventModel.Episode.Name;
                    }

                    programme.Episode = episode;
                    _ = prgDictEpisodeToProcess.Add(episode);
                }

                var schedule = _scheduleCache.GetOrAdd(programme.ScheduleUniqueKey,
                                                       key =>
                {
                    var sch = new ScheduleEntity
                    {
                        SalesAreaId = programme.SalesAreaId,
                        Date        = programme.StartDateTime.Date
                    };
                    scheduleToProcess.Add(sch);

                    return(sch);
                });

                schPrgToProcess.Add(new ScheduleProgrammeEntity {
                    Programme = programme, Schedule = schedule
                });
            }

            using (var dbContext = _dbContextFactory.Create())
            {
                using (var transaction = dbContext.Specific.Database.BeginTransaction())
                {
                    dbContext.BulkInsertEngine.BulkInsertOrUpdate(prgDictToProcess.ToList(),
                                                                  new BulkInsertOptions {
                        PreserveInsertOrder = true, SetOutputIdentity = true
                    });

                    foreach (var episode in prgDictEpisodeToProcess)
                    {
                        episode.ProgrammeDictionaryId = episode.ProgrammeDictionary.Id;
                    }

                    dbContext.BulkInsertEngine.BulkInsertOrUpdate(prgDictEpisodeToProcess.ToList(),
                                                                  new BulkInsertOptions {
                        PreserveInsertOrder = true, SetOutputIdentity = true
                    });

                    dbContext.BulkInsertEngine.BulkInsert(prgCatToProcess,
                                                          new BulkInsertOptions {
                        PreserveInsertOrder = true, SetOutputIdentity = true
                    });

                    foreach (var prg in prgToProcess)
                    {
                        prg.ProgrammeDictionaryId = prg.ProgrammeDictionary.Id;
                        prg.EpisodeId             = prg.Episode?.Id;
                    }

                    dbContext.BulkInsertEngine.BulkInsert(prgToProcess,
                                                          new BulkInsertOptions {
                        PreserveInsertOrder = true
                    });

                    foreach (var catLink in prgCatLinkToProcess)
                    {
                        catLink.ProgrammeCategoryId = catLink.ProgrammeCategory.Id;
                    }

                    dbContext.BulkInsertEngine.BulkInsert(prgCatLinkToProcess);

                    dbContext.BulkInsertEngine.BulkInsert(scheduleToProcess,
                                                          new BulkInsertOptions {
                        PreserveInsertOrder = true, SetOutputIdentity = true
                    });

                    foreach (var scheduleProgramme in schPrgToProcess)
                    {
                        scheduleProgramme.ProgrammeId = scheduleProgramme.Programme.Id;
                        scheduleProgramme.ScheduleId  = scheduleProgramme.Schedule.Id;
                    }

                    dbContext.BulkInsertEngine.BulkInsert(schPrgToProcess);

                    transaction.Commit();
                }
            }
        }
Esempio n. 7
0
 public IReadOnlyStock GetStock(Guid id)
 {
     return(_StockCache.Get(id));
 }
 public Stock Get(Guid id)
 {
     return(_Cache.Get(id));
 }