コード例 #1
0
ファイル: ScenariosController.cs プロジェクト: Morebis-GIT/CI
        public IHttpActionResult SearchLibraryItems([FromUri] SearchQueryModel query)
        {
            var searchQuery = new SearchQueryDto(query);

            IEnumerable <PassDigestListItem> passesContainingTitleFromQuery = new List <PassDigestListItem>();

            if (!string.IsNullOrEmpty(searchQuery.Title))
            {
                var passesSearchQuery = new SearchQueryDto(new SearchQueryModel()
                {
                    OrderBy        = query.OrderBy,
                    OrderDirection = query.OrderDirection,
                    Title          = query.Title,
                    Top            = int.MaxValue,
                    Skip           = 0
                });

                passesContainingTitleFromQuery = _passRepository.MinimalDataSearch(passesSearchQuery, false)
                                                 .Items
                                                 .Where(p =>
                                                        p.Name.IndexOf(searchQuery.Title, StringComparison.CurrentCultureIgnoreCase) >= 0
                                                        );
            }

            var scenariosWithMinimalData = _scenarioRepository.MinimalDataSearch(
                searchQuery,
                true,
                passesContainingTitleFromQuery
                .Select(p => p.Id)
                );

            if (scenariosWithMinimalData.TotalCount > 0)
            {
                var defaultScenarioId = _tenantSettingsRepository.GetDefaultScenarioId();
                var defaultScenario   = scenariosWithMinimalData.Items.FirstOrDefault(c => c.Id == defaultScenarioId);

                if (defaultScenario != null)
                {
                    defaultScenario.IsDefault = true;
                }

                var extractedPasses = ExtractMinimalPassesFromScenarios(scenariosWithMinimalData)
                                      .ToList();

                var passesFromDb = _passRepository.FindByIds(extractedPasses.Select(pass => pass.Id))
                                   .Select(p => (p.Id, p.Name, p.DateModified))
                                   .ToDictionary(p => p.Id);

                List <int> passIdsToRemove = new List <int>();
                UpdatePassesWithMinimalData(extractedPasses, passesFromDb, passIdsToRemove);

                RemoveDeletedPasses(scenariosWithMinimalData, passIdsToRemove);

                var minimalDataSearchResult = _mapper.Map <SearchResultModel <ScenarioDigestListItemModel> >(scenariosWithMinimalData);

                return(Ok(minimalDataSearchResult));
            }
            else
            {
                return(NotFound());
            }

            void UpdatePassesWithMinimalData(
                List <PassDigestListItem> passesWithMinimalData,
                Dictionary <int, (int Id, string Name, DateTime?DateModified)> passesFromDb,
                List <int> passIdsToRemove)
            {
                passesWithMinimalData.ForEach(minimalDataPass =>
                {
                    var pass = passesFromDb.FirstOrDefault(pass => pass.Key == minimalDataPass.Id).Value;

                    if (pass.Id == minimalDataPass.Id)
                    {
                        minimalDataPass.DateModified = pass.DateModified.Value;
                        minimalDataPass.Name         = pass.Name;
                    }
                    else
                    {
                        passIdsToRemove.Add(minimalDataPass.Id);
                    }
                });
            }

            void RemoveDeletedPasses(SearchResultModel <ScenarioDigestListItem> scenariosWithMinimalData, List <int> passIdsToRemove)
            {
                passIdsToRemove.ForEach(id =>
                {
                    scenariosWithMinimalData.Items.ForEach(scenario =>
                    {
                        var passToRemove = scenario.Passes.Find(p => p.Id == id);
                        if (passToRemove != null)
                        {
                            scenario.Passes.Remove(passToRemove);
                        }
                    });
                });
            }

            IEnumerable <PassDigestListItem> ExtractMinimalPassesFromScenarios(SearchResultModel <ScenarioDigestListItem> searchScenarios)
            {
                var passes = new List <PassDigestListItem>();

                foreach (var scenario in searchScenarios.Items)
                {
                    passes.AddRange(scenario.Passes);
                }

                return(passes);
            }
        }