Example #1
0
        public void ForceExpireModels<T>(IEnumerable<T> models = null) where T : MappableModel
        {
            List<T> expireList;
            if (models != null)
            {
                expireList = models.ToList();
            }
            else
            {
                expireList = ModelCache.GetOfType<T>().ToList();
            }

            expireList.Where(x => x != null).ForEach(x => x.IsExpired = true);
        }
Example #2
0
        public async Task<IEnumerable<T>> GetModelsAsync<T>(IEnumerable<long[]> modelIds = null, bool update = false, bool reload = false) where T : MappableModel
        {
            object[] data = null;
            List<T> modelList = new List<T>();
            List<T> updateList = new List<T>();
            List<long[]> getModelIds = new List<long[]>();

            if (modelIds != null)
            {
                foreach (var modelId in modelIds)
                {
                    var loadedModel = ModelCache.GetModel<T>(modelId.Cast<object>().ToArray());
                    if (loadedModel != null)
                    {
                        modelList.Add(loadedModel);
                        if (reload || loadedModel.IsExpired)
                        {
                            getModelIds.Add(modelId);
                            loadedModel.InitReset();
                        }
                        else
                            updateList.Add(loadedModel);
                    }
                    else
                        getModelIds.Add(modelId);
                }
            }
            else
            {
                getModelIds = null;
            }

            if (update)
            {
                _ = GetModelsAsync<T>(updateList.Where(x => x.ContainsChanges == false).Select(x => x.ModelId).ToArray(), update: false, reload: true);
                _ = UpdateModelsAsync(updateList.ToArray());
            }

            if (getModelIds == null || getModelIds.Count > 0)
            {
                if (typeof(T).Equals(typeof(SeasonModel)))
                {
                    data = await ModelDataProvider.GetAsync<SeasonDataDTO>(getModelIds?.Select(x => x.ToArray()).ToArray());
                }
                else if (typeof(T).Equals(typeof(LeagueMember)))
                {
                    data = await ModelDataProvider.GetAsync<LeagueMemberDataDTO>(getModelIds?.Select(x => x.ToArray()).ToArray());
                }
                else if (typeof(T).Equals(typeof(ScheduleModel)))
                {
                    data = await ModelDataProvider.GetAsync<ScheduleDataDTO>(getModelIds?.Select(x => x.ToArray()).ToArray());
                }
                else if (typeof(T).Equals(typeof(SessionModel)))
                {
                    data = await ModelDataProvider.GetAsync<SessionDataDTO>(getModelIds?.Select(x => x.ToArray()).ToArray());
                }
                else if (typeof(T).Equals(typeof(RaceSessionModel)))
                {
                    data = await ModelDataProvider.GetAsync<RaceSessionDataDTO>(getModelIds?.Select(x => x.ToArray()).ToArray());
                }
                else if (typeof(T).Equals(typeof(IncidentReviewModel)))
                {
                    data = await ModelDataProvider.GetAsync<IncidentReviewDataDTO>(getModelIds?.Select(x => x.ToArray()).ToArray());
                }
                else if (typeof(T).Equals(typeof(CommentModel)))
                {
                    data = await ModelDataProvider.GetAsync<CommentDataDTO>(getModelIds?.Select(x => x.ToArray()).ToArray());
                }
                else if (typeof(T).Equals(typeof(ReviewCommentModel)))
                {
                    data = await ModelDataProvider.GetAsync<ReviewCommentDataDTO>(getModelIds?.Select(x => x.ToArray()).ToArray());
                }
                else if (typeof(T).Equals(typeof(ResultModel)))
                {
                    data = await ModelDataProvider.GetAsync<ResultDataDTO>(getModelIds?.Select(x => x.ToArray()).ToArray());
                }
                else if (typeof(T).Equals(typeof(LeagueMember)))
                {
                    data = await ModelDataProvider.GetAsync<LeagueMemberDataDTO>(getModelIds?.Select(x => x.ToArray()).ToArray());
                }
                else if (typeof(T).Equals(typeof(ScoringModel)))
                {
                    data = await ModelDataProvider.GetAsync<ScoringDataDTO>(getModelIds?.Select(x => x.ToArray()).ToArray());
                }
                else if (typeof(T).Equals(typeof(ScoredResultModel)))
                {
                    data = await ModelDataProvider.GetAsync<ScoredResultDataDTO>(getModelIds?.Select(x => x.ToArray()).ToArray());
                }
                else if (typeof(T).Equals(typeof(ScoredResultRowModel)))
                {
                    data = await ModelDataProvider.GetAsync<ScoredResultRowDataDTO>(getModelIds?.Select(x => x.ToArray()).ToArray());
                }
                else if (typeof(T).Equals(typeof(ResultRowModel)))
                {
                    data = await ModelDataProvider.GetAsync<ResultRowDataDTO>(getModelIds?.Select(x => x.ToArray()).ToArray());
                }
                else if (typeof(T).Equals(typeof(StandingsModel)))
                {
                    data = await ModelDataProvider.GetAsync<StandingsDataDTO>(getModelIds?.Select(x => x.ToArray()).ToArray());
                }
                else if (typeof(T).Equals(typeof(AddPenaltyModel)))
                {
                    data = await ModelDataProvider.GetAsync<AddPenaltyDTO>(getModelIds?.Select(x => x.ToArray()).ToArray());
                }
                else if (typeof(T).Equals(typeof(ScoringTableModel)))
                {
                    data = await ModelDataProvider.GetAsync<ScoringTableDataDTO>(getModelIds?.Select(x => x.ToArray()).ToArray());
                }
                else if (typeof(T).Equals(typeof(TeamModel)))
                {
                    data = await ModelDataProvider.GetAsync<TeamDataDTO>(getModelIds?.Select(x => x.ToArray()).ToArray());
                }
                else if (typeof(T).Equals(typeof(VoteCategoryModel)))
                {
                    data = await ModelDataProvider.GetAsync<VoteCategoryDTO>(getModelIds?.ToArray());
                }
                else if (typeof(T).Equals(typeof(CustomIncidentModel)))
                {
                    data = await ModelDataProvider.GetAsync<CustomIncidentDTO>(getModelIds?.ToArray());
                }
                else if (typeof(T).Equals(typeof(ResultsFilterOptionModel)))
                {
                    data = await ModelDataProvider.GetAsync<ResultsFilterOptionDTO>(getModelIds?.ToArray());
                }
                else if (typeof(T).Equals(typeof(SeasonStatisticSetModel)))
                {
                    data = await ModelDataProvider.GetAsync<SeasonStatisticSetDTO>(getModelIds?.ToArray());
                }
                else if (typeof(T).Equals(typeof(LeagueStatisticSetModel)))
                {
                    data = await ModelDataProvider.GetAsync<LeagueStatisticSetDTO>(getModelIds?.ToArray());
                }
                else if (typeof(T).Equals(typeof(ImportedStatisticSetModel)))
                {
                    data = await ModelDataProvider.GetAsync<ImportedStatisticSetDTO>(getModelIds?.ToArray());
                }
                else if (typeof(T).Equals(typeof(StatisticSetModel)))
                {
                    data = await ModelDataProvider.GetAsync<StatisticSetDTO>(getModelIds?.ToArray());
                }
                else if (typeof(T).Equals(typeof(DriverStatisticModel)))
                {
                    data = await ModelDataProvider.GetAsync<DriverStatisticDTO>(getModelIds?.ToArray());
                }
                else
                {
                    throw new UnknownModelTypeException("Could not load Model of type " + typeof(T).ToString() + ". Model type not known.");
                }
                //data = await DbContext.GetAsync(getModelIds?.Select(x => x.ToArray()).ToArray(), typeof(T));

                if (data == null)
                {
                    if (getModelIds == null)
                        return null;

                    foreach (var modelId in getModelIds)
                    {
                        var add = await GetModelAsync<T>(modelId.ToArray());
                        if (add == null)
                            return new T[0];
                        if (modelList.Any(x => x != null && x.ModelId.SequenceEqual(add.ModelId)))
                            add.CopyTo(modelList.SingleOrDefault(x => x.ModelId.SequenceEqual(add.ModelId)));
                        else
                            modelList.Add(add);
                    }
                }
                else
                {
                    var mapper = MapperConfiguration.CreateMapper();
                    var addList = new List<T>();
                    var validData = data.Where(x => x != null);
                    //mapper.Map(validData, addList);
                    foreach(var dto in validData)
                    {
                        var add = mapper.Map<T>(dto);
                        addList.Add(add);
                    }
                    //modelList.AddRange(addList);
                    //foreach (var add in addList)
                    //{
                    //    if (modelList.Any(x => x != null && x.ModelId.SequenceEqual(add.ModelId)))
                    //        add.CopyTo(modelList.SingleOrDefault(x => x.ModelId.SequenceEqual(add.ModelId)));
                    //    else
                    //        modelList.Add(add);
                    //}
                    modelList = addList;
                }
            }

            foreach (var model in modelList)
            {
                if (model != null)
                {
                    model.ResetChangedState();
                    model.InitializeModel();
                }
            }

            if (modelIds == null)
                return modelList;
            else
            {
                return modelIds.Select(x => modelList.SingleOrDefault(y => y != null && y.ModelId.SequenceEqual(x)));
            }
        }
Example #3
0
 public void ForceExpireModels<T>(IEnumerable<long[]> modelIds) where T : MappableModel
 {
     ForceExpireModels<T>(ModelCache.GetOfType<T>().Where(x => modelIds.Any(y => y.SequenceEqual(x.ModelId))).ToList());
 }
Example #4
0
        public async Task<bool> DeleteModelsAsync<T>(long[][] modelIds) where T : MappableModel
        {
            if (modelIds != null)
            {
                foreach (var modelId in modelIds)
                {
                    ModelCache.RemoveReference<T>(modelId.Cast<object>().ToArray());
                }
            }

            if (typeof(T).Equals(typeof(SeasonModel)))
            {
                return await ModelDataProvider.DelAsync<SeasonDataDTO>(modelIds);
            }
            else if (typeof(T).Equals(typeof(SessionModel)))
            {
                return await ModelDataProvider.DelAsync<SessionDataDTO>(modelIds);
            }
            else if (typeof(T).Equals(typeof(RaceSessionModel)))
            {
                return await ModelDataProvider.DelAsync<RaceSessionDataDTO>(modelIds);
            }
            else if (typeof(T).Equals(typeof(ScheduleModel)))
            {
                return await ModelDataProvider.DelAsync<ScheduleDataDTO>(modelIds);
            }
            else if (typeof(T).Equals(typeof(IncidentReviewModel)))
            {
                return await ModelDataProvider.DelAsync<IncidentReviewDataDTO>(modelIds);
            }
            else if (typeof(T).Equals(typeof(CommentModel)))
            {
                return await ModelDataProvider.DelAsync<CommentDataDTO>(modelIds);
            }
            else if (typeof(T).Equals(typeof(ReviewCommentModel)))
            {
                return await ModelDataProvider.DelAsync<ReviewCommentDataDTO>(modelIds);
            }
            else if (typeof(T).Equals(typeof(ResultModel)))
            {
                return await ModelDataProvider.DelAsync<ResultDataDTO>(modelIds);
            }
            else if (typeof(T).Equals(typeof(LeagueMember)))
            {
                return await ModelDataProvider.DelAsync<LeagueMemberDataDTO>(modelIds);
            }
            else if (typeof(T).Equals(typeof(ScoringModel)))
            {
                return await ModelDataProvider.DelAsync<ScoringDataDTO>(modelIds);
            }
            else if (typeof(T).Equals(typeof(ResultRowModel)))
            {
                return await ModelDataProvider.DelAsync<ResultRowDataDTO>(modelIds);
            }
            else if (typeof(T).Equals(typeof(AddPenaltyModel)))
            {
                return await ModelDataProvider.DelAsync<AddPenaltyDTO>(modelIds);
            }
            else if (typeof(T).Equals(typeof(ScoredResultModel)))
            {
                return await ModelDataProvider.DelAsync<ScoredResultDataDTO>(modelIds);
            }
            else if (typeof(T).Equals(typeof(ScoredResultRowModel)))
            {
                return await ModelDataProvider.DelAsync<ScoredResultRowDataDTO>(modelIds);
            }
            else if (typeof(T).Equals(typeof(ScoringTableModel)))
            {
                return await ModelDataProvider.DelAsync<ScoringTableDataDTO>(modelIds);
            }
            else if (typeof(T).Equals(typeof(TeamModel)))
            {
                return await ModelDataProvider.DelAsync<TeamDataDTO>(modelIds);
            }
            else if (typeof(T).Equals(typeof(VoteCategoryModel)))
            {
                return await ModelDataProvider.DelAsync<VoteCategoryDTO>(modelIds);
            }
            else if (typeof(T).Equals(typeof(CustomIncidentModel)))
            {
                return await ModelDataProvider.DelAsync<CustomIncidentDTO>(modelIds);
            }
            else if (typeof(T).Equals(typeof(ResultsFilterOptionModel)))
            {
                return await ModelDataProvider.DelAsync<ResultsFilterOptionDTO>(modelIds);
            }
            else if (typeof(T).Equals(typeof(SeasonStatisticSetModel)))
            {
                return await ModelDataProvider.DelAsync<SeasonStatisticSetDTO>(modelIds);
            }
            else if (typeof(T).Equals(typeof(LeagueStatisticSetModel)))
            {
                return await ModelDataProvider.DelAsync<LeagueStatisticSetDTO>(modelIds);
            }
            else if (typeof(T).Equals(typeof(ImportedStatisticSetModel)))
            {
                return await ModelDataProvider.DelAsync<ImportedStatisticSetDTO>(modelIds);
            }
            else if (typeof(T).Equals(typeof(StatisticSetModel)))
            {
                return await ModelDataProvider.DelAsync<StatisticSetDTO>(modelIds);
            }
            else if (typeof(T).Equals(typeof(DriverStatisticModel)))
            {
                return await ModelDataProvider.DelAsync<DriverStatisticDTO>(modelIds);
            }
            else if (typeof(T).Equals(typeof(ScoringRuleBase)))
            {
                throw new NotImplementedException("Loading of model from type " + typeof(T).ToString() + " not yet supported.");
            }
            else
            {
                throw new UnknownModelTypeException("Could not load Model of type " + typeof(T).ToString() + ". Model type not known.");
            }
        }