예제 #1
0
        public object Get(GetMediaFolders request)
        {
            var items = _libraryManager.GetUserRootFolder().Children.Concat(_libraryManager.RootFolder.VirtualChildren).OrderBy(i => i.SortName).ToList();

            if (request.IsHidden.HasValue)
            {
                var val = request.IsHidden.Value;

                items = items.Where(i => i.IsHidden == val).ToList();
            }

            var dtoOptions = GetDtoOptions(_authContext, request);

            var result = new ItemsResult
            {
                TotalRecordCount = items.Count,

                Items = items.Select(i => _dtoService.GetBaseItemDto(i, dtoOptions)).ToArray()
            };

            return(ToOptimizedResult(result));
        }
예제 #2
0
        public object Get(GetPlaylistItems request)
        {
            var playlist = (Playlist)_libraryManager.GetItemById(request.Id);
            var user     = !string.IsNullOrWhiteSpace(request.UserId) ? _userManager.GetUserById(new Guid(request.UserId)) : null;

            var items = playlist.GetManageableItems().ToArray();

            var count = items.Length;

            if (request.StartIndex.HasValue)
            {
                items = items.Skip(request.StartIndex.Value).ToArray();
            }

            if (request.Limit.HasValue)
            {
                items = items.Take(request.Limit.Value).ToArray();
            }

            var dtos = items
                       .Select(i => _dtoService.GetBaseItemDto(i.Item2, request.GetItemFields().ToList(), user))
                       .ToArray();

            var index = 0;

            foreach (var item in dtos)
            {
                item.PlaylistItemId = items[index].Item1.Id;
                index++;
            }

            var result = new ItemsResult
            {
                Items            = dtos,
                TotalRecordCount = count
            };

            return(ToOptimizedResult(result));
        }
예제 #3
0
        private async Task <ItemsResult> GetInstantMixResult(BaseGetSimilarItems request, IEnumerable <string> genres)
        {
            var user = request.UserId.HasValue ? _userManager.GetUserById(request.UserId.Value) : null;

            var fields = request.GetItemFields().ToList();

            var dtoBuilder = new DtoBuilder(Logger, _libraryManager, _userDataRepository, _itemRepo);

            var inputItems = user == null
                                 ? _libraryManager.RootFolder.RecursiveChildren
                                 : user.RootFolder.GetRecursiveChildren(user);

            var genresDictionary = genres.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);

            var limit = request.Limit.HasValue ? request.Limit.Value * 2 : 100;

            var items = inputItems
                        .OfType <Audio>()
                        .Select(i => new Tuple <Audio, int>(i, i.Genres.Count(genresDictionary.ContainsKey)))
                        .OrderByDescending(i => i.Item2)
                        .ThenBy(i => Guid.NewGuid())
                        .Select(i => i.Item1)
                        .Take(limit)
                        .OrderBy(i => Guid.NewGuid())
                        .ToArray();

            var result = new ItemsResult
            {
                TotalRecordCount = items.Length
            };

            var tasks = items.Take(request.Limit ?? items.Length)
                        .Select(i => dtoBuilder.GetBaseItemDto(i, fields, user));

            result.Items = await Task.WhenAll(tasks).ConfigureAwait(false);

            return(result);
        }
예제 #4
0
        /// <summary>
        /// Gets the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>System.Object.</returns>
        public async Task <object> Get(GetIntros request)
        {
            var user = _userManager.GetUserById(request.UserId);

            var item = string.IsNullOrEmpty(request.Id) ? user.RootFolder : _libraryManager.GetItemById(request.Id);

            var items = await _libraryManager.GetIntros(item, user).ConfigureAwait(false);

            // Get everything
            var fields = Enum.GetNames(typeof(ItemFields))
                         .Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true))
                         .ToList();

            var dtos = items.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
                       .ToArray();

            var result = new ItemsResult
            {
                Items            = dtos,
                TotalRecordCount = dtos.Length
            };

            return(ToOptimizedSerializedResultUsingCache(result));
        }
예제 #5
0
        private async Task <Boolean> GeSearchPeopleAsync()
        {
            SearchHintResult searchHintResult = await _apiClient.GetSearchHintsAsync(new SearchQuery { UserId = _apiClient.CurrentUserId, SearchTerm = CurrentSearch, IncludePeople = true, IncludeArtists = false, IncludeGenres = false, IncludeMedia = false, IncludeStudios = false, Limit = 9 });

            var ids = (searchHintResult.TotalRecordCount > 0) ? searchHintResult.SearchHints.Select(s => s.ItemId).ToArray() : null;

            if (ids != null && ids.Length > 0)
            {
                var query = new ItemQuery
                {
                    Ids    = ids,
                    UserId = _apiClient.CurrentUserId,
                    Fields = FolderPage.QueryFields
                };

                _searchedPeopleItemsResult = await _apiClient.GetItemsAsync(query);
            }
            else
            {
                _searchedPeopleItemsResult = _emptyItemsResult;
            }

            return(true);
        }
예제 #6
0
        private ItemsResult GetInstantMixResult(BaseGetSimilarItems request, IEnumerable <string> genres)
        {
            var user = request.UserId.HasValue ? _userManager.GetUserById(request.UserId.Value) : null;

            var fields = request.GetItemFields().ToList();

            var inputItems = user == null
                                 ? _libraryManager.RootFolder.RecursiveChildren
                                 : user.RootFolder.GetRecursiveChildren(user);

            var genresDictionary = genres.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);

            var limit = request.Limit.HasValue ? request.Limit.Value * 2 : 100;

            var items = inputItems
                        .OfType <Audio>()
                        .Select(i => new Tuple <Audio, int>(i, i.Genres.Count(genresDictionary.ContainsKey)))
                        .OrderByDescending(i => i.Item2)
                        .ThenBy(i => Guid.NewGuid())
                        .Select(i => i.Item1)
                        .Take(limit)
                        .OrderBy(i => Guid.NewGuid())
                        .ToList();

            var result = new ItemsResult
            {
                TotalRecordCount = items.Count
            };

            var dtos = items.Take(request.Limit ?? items.Count)
                       .Select(i => _dtoService.GetBaseItemDto(i, fields, user));

            result.Items = dtos.ToArray();

            return(result);
        }
        public async Task GetAllWarehouses_Should_ReturnAllWarehouses_When_DbTableNotEmpty()
        {
            #region Arrange

            var connection = DbConnectionFactory.CreateTransient();
            var dbContext  = new WarehouseDbContext(connection);
            var mapper     = MapperFactory.CreateMapper();

            var query   = new GetAllWarehouses();
            var handler = new GetAllWarehousesHandler(dbContext, mapper);

            //insert some objects to the database
            dbContext.Warehouses.Add(new DataAccess.Entities.Warehouse()
            {
                Address = new DataAccess.Entities.Address()
                {
                    City       = "Warsaw",
                    Country    = "Poland",
                    Id         = 1,
                    PostalCode = "12345",
                    Street     = "Test 1/2"
                },
                AddressId         = 1,
                HazardousProducts = true,
                Id   = 1,
                Name = "Warehouse 1",
                Size = 200
            });
            dbContext.Warehouses.Add(new DataAccess.Entities.Warehouse()
            {
                Address = new DataAccess.Entities.Address()
                {
                    City       = "London",
                    Country    = "UK",
                    Id         = 2,
                    PostalCode = "23456",
                    Street     = "Test 2/3"
                },
                AddressId         = 2,
                HazardousProducts = false,
                Id   = 2,
                Name = "Warehouse 2",
                Size = 150
            });
            dbContext.Warehouses.Add(new DataAccess.Entities.Warehouse()
            {
                Address = new DataAccess.Entities.Address()
                {
                    City       = "Oslo",
                    Country    = "Norway",
                    Id         = 3,
                    PostalCode = "34567",
                    Street     = "Test 3/4"
                },
                AddressId         = 3,
                HazardousProducts = true,
                Id   = 3,
                Name = "Warehouse 3",
                Size = 300
            });
            dbContext.SaveChanges();

            #endregion

            #region Act

            ItemsResult <WarehouseInfo> result = await handler.Handle(query, CancellationToken.None);

            #endregion

            #region Assert

            result.ShouldNotBeNull();
            result.Items.ShouldNotBeEmpty();
            result.Items.Count.ShouldBe(3);

            #endregion
        }
예제 #8
0
        private async Task <ItemsResult> GetSimilarItemsResult(BaseGetSimilarItemsFromItem request, Func <BaseItem, List <PersonInfo>, List <PersonInfo>, BaseItem, int> getSimilarityScore)
        {
            var user = !string.IsNullOrWhiteSpace(request.UserId) ? _userManager.GetUserById(request.UserId) : null;

            var item = string.IsNullOrEmpty(request.Id) ?
                       (!string.IsNullOrWhiteSpace(request.UserId) ? user.RootFolder :
                        _libraryManager.RootFolder) : _libraryManager.GetItemById(request.Id);

            var query = new InternalItemsQuery(user)
            {
                IncludeItemTypes = new[] { typeof(Movie).Name }
            };

            if (user == null || user.Configuration.IncludeTrailersInSuggestions)
            {
                var includeList = query.IncludeItemTypes.ToList();
                includeList.Add(typeof(Trailer).Name);
                query.IncludeItemTypes = includeList.ToArray();
            }

            var parentIds = new string[] { };
            var list      = _libraryManager.GetItemList(query, parentIds)
                            .Where(i =>
            {
                // Strip out secondary versions
                var v = i as Video;
                return(v != null && !v.PrimaryVersionId.HasValue);
            })
                            .DistinctBy(i => i.GetProviderId(MetadataProviders.Imdb) ?? Guid.NewGuid().ToString("N"))
                            .ToList();

            if (item is Video)
            {
                var imdbId = item.GetProviderId(MetadataProviders.Imdb);

                // Use imdb id to try to filter duplicates of the same item
                if (!string.IsNullOrWhiteSpace(imdbId))
                {
                    list = list
                           .Where(i => !string.Equals(imdbId, i.GetProviderId(MetadataProviders.Imdb), StringComparison.OrdinalIgnoreCase))
                           .ToList();
                }
            }

            var items = SimilarItemsHelper.GetSimilaritems(item, _libraryManager, list, getSimilarityScore).ToList();

            IEnumerable <BaseItem> returnItems = items;

            if (request.Limit.HasValue)
            {
                returnItems = returnItems.Take(request.Limit.Value);
            }

            var dtoOptions = GetDtoOptions(request);

            var result = new ItemsResult
            {
                Items = _dtoService.GetBaseItemDtos(returnItems, dtoOptions, user).ToArray(),

                TotalRecordCount = items.Count
            };

            return(result);
        }
예제 #9
0
        /// <summary>
        /// Gets the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>Task{ItemsResult}.</returns>
        protected ItemsResult GetResult(GetItemsByName request)
        {
            var dtoOptions = GetDtoOptions(request);

            User            user = null;
            BaseItem        parentItem;
            List <BaseItem> libraryItems = null;

            if (!string.IsNullOrWhiteSpace(request.UserId))
            {
                user       = UserManager.GetUserById(request.UserId);
                parentItem = string.IsNullOrEmpty(request.ParentId) ? user.RootFolder : LibraryManager.GetItemById(request.ParentId);

                if (RequiresLibraryItems(request, dtoOptions))
                {
                    libraryItems = user.RootFolder.GetRecursiveChildren(user).ToList();
                }
            }
            else
            {
                parentItem = string.IsNullOrEmpty(request.ParentId) ? LibraryManager.RootFolder : LibraryManager.GetItemById(request.ParentId);
                if (RequiresLibraryItems(request, dtoOptions))
                {
                    libraryItems = LibraryManager.RootFolder.GetRecursiveChildren().ToList();
                }
            }

            IEnumerable <BaseItem> items;

            var excludeItemTypes = request.GetExcludeItemTypes();
            var includeItemTypes = request.GetIncludeItemTypes();
            var mediaTypes       = request.GetMediaTypes();

            var query = new InternalItemsQuery(user)
            {
                ExcludeItemTypes = excludeItemTypes,
                IncludeItemTypes = includeItemTypes,
                MediaTypes       = mediaTypes
            };

            Func <BaseItem, bool> filter = i => FilterItem(request, i, excludeItemTypes, includeItemTypes, mediaTypes);

            if (parentItem.IsFolder)
            {
                var folder = (Folder)parentItem;

                if (!string.IsNullOrWhiteSpace(request.UserId))
                {
                    items = request.Recursive ?
                            folder.GetRecursiveChildren(user, query) :
                            folder.GetChildren(user, true).Where(filter);
                }
                else
                {
                    items = request.Recursive ?
                            folder.GetRecursiveChildren(filter) :
                            folder.Children.Where(filter);
                }
            }
            else
            {
                items = new[] { parentItem }.Where(filter);
            }

            var extractedItems = GetAllItems(request, items);

            var filteredItems = FilterItems(request, extractedItems, user);

            filteredItems = FilterByLibraryItems(request, filteredItems.Cast <IItemByName>(), user, libraryItems).Cast <BaseItem>();

            filteredItems = LibraryManager.Sort(filteredItems, user, request.GetOrderBy(), request.SortOrder ?? SortOrder.Ascending);

            var ibnItemsArray = filteredItems.ToList();

            IEnumerable <BaseItem> ibnItems = ibnItemsArray;

            var result = new ItemsResult
            {
                TotalRecordCount = ibnItemsArray.Count
            };

            if (request.StartIndex.HasValue || request.Limit.HasValue)
            {
                if (request.StartIndex.HasValue)
                {
                    ibnItems = ibnItems.Skip(request.StartIndex.Value);
                }

                if (request.Limit.HasValue)
                {
                    ibnItems = ibnItems.Take(request.Limit.Value);
                }
            }

            IEnumerable <Tuple <BaseItem, List <BaseItem> > > tuples;

            if (dtoOptions.Fields.Contains(ItemFields.ItemCounts))
            {
                tuples = ibnItems.Select(i => new Tuple <BaseItem, List <BaseItem> >(i, ((IItemByName)i).GetTaggedItems(libraryItems).ToList()));
            }
            else
            {
                tuples = ibnItems.Select(i => new Tuple <BaseItem, List <BaseItem> >(i, new List <BaseItem>()));
            }

            var syncProgess = DtoService.GetSyncedItemProgress(dtoOptions);
            var dtos        = tuples.Select(i => DtoService.GetItemByNameDto(i.Item1, dtoOptions, i.Item2, syncProgess, user));

            result.Items = dtos.Where(i => i != null).ToArray();

            return(result);
        }
예제 #10
0
        /// <summary>
        /// Gets the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>Task{ItemsResult}.</returns>
        protected ItemsResult GetResult(GetItemsByName request)
        {
            User     user = null;
            BaseItem item;

            if (request.UserId.HasValue)
            {
                user = UserManager.GetUserById(request.UserId.Value);
                item = string.IsNullOrEmpty(request.ParentId) ? user.RootFolder : DtoService.GetItemByDtoId(request.ParentId, user.Id);
            }
            else
            {
                item = string.IsNullOrEmpty(request.ParentId) ? LibraryManager.RootFolder : DtoService.GetItemByDtoId(request.ParentId);
            }

            IEnumerable <BaseItem> items;

            if (item.IsFolder)
            {
                var folder = (Folder)item;

                if (request.UserId.HasValue)
                {
                    items = request.Recursive ? folder.GetRecursiveChildren(user) : folder.GetChildren(user, true);
                }
                else
                {
                    items = request.Recursive ? folder.GetRecursiveChildren() : folder.Children;
                }
            }
            else
            {
                items = new[] { item };
            }

            items = FilterItems(request, items);

            var extractedItems = GetAllItems(request, items);

            var filteredItems = FilterItems(request, extractedItems, user);

            filteredItems = FilterByLibraryItems(request, filteredItems, user);

            filteredItems = ItemsService.ApplySortOrder(request, filteredItems, user, LibraryManager).Cast <TItemType>();

            var ibnItemsArray = filteredItems.ToList();

            IEnumerable <TItemType> ibnItems = ibnItemsArray;

            var result = new ItemsResult
            {
                TotalRecordCount = ibnItemsArray.Count
            };

            if (request.StartIndex.HasValue || request.Limit.HasValue)
            {
                if (request.StartIndex.HasValue)
                {
                    ibnItems = ibnItems.Skip(request.StartIndex.Value);
                }

                if (request.Limit.HasValue)
                {
                    ibnItems = ibnItems.Take(request.Limit.Value);
                }
            }

            var fields = request.GetItemFields().ToList();

            var dtos = ibnItems.Select(i => GetDto(i, user, fields));

            result.Items = dtos.Where(i => i != null).ToArray();

            return(result);
        }
예제 #11
0
        /// <summary>
        /// Gets the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>Task{ItemsResult}.</returns>
        protected async Task <ItemsResult> GetResult(GetItemsByName request)
        {
            User     user = null;
            BaseItem item;

            if (request.UserId.HasValue)
            {
                user = UserManager.GetUserById(request.UserId.Value);
                item = string.IsNullOrEmpty(request.ParentId) ? user.RootFolder : DtoBuilder.GetItemByClientId(request.ParentId, UserManager, LibraryManager, user.Id);
            }
            else
            {
                item = string.IsNullOrEmpty(request.ParentId) ? LibraryManager.RootFolder : DtoBuilder.GetItemByClientId(request.ParentId, UserManager, LibraryManager);
            }

            IEnumerable <BaseItem> items;

            if (item.IsFolder)
            {
                var folder = (Folder)item;

                if (request.UserId.HasValue)
                {
                    items = request.Recursive ? folder.GetRecursiveChildren(user) : folder.GetChildren(user, true);
                }
                else
                {
                    items = request.Recursive ? folder.RecursiveChildren: folder.Children;
                }
            }
            else
            {
                items = new[] { item };
            }

            items = FilterItems(request, items);

            var extractedItems = GetAllItems(request, items);

            extractedItems = FilterItems(request, extractedItems, user);
            extractedItems = SortItems(request, extractedItems);

            var ibnItemsArray = extractedItems.ToArray();

            IEnumerable <IbnStub <TItemType> > ibnItems = ibnItemsArray;

            var result = new ItemsResult
            {
                TotalRecordCount = ibnItemsArray.Length
            };

            if (request.StartIndex.HasValue || request.Limit.HasValue)
            {
                if (request.StartIndex.HasValue)
                {
                    ibnItems = ibnItems.Skip(request.StartIndex.Value);
                }

                if (request.Limit.HasValue)
                {
                    ibnItems = ibnItems.Take(request.Limit.Value);
                }
            }

            var fields = request.GetItemFields().ToList();

            var tasks = ibnItems.Select(i => GetDto(i, user, fields));

            var resultItems = await Task.WhenAll(tasks).ConfigureAwait(false);

            result.Items = resultItems.Where(i => i != null).ToArray();

            return(result);
        }
예제 #12
0
        /// <summary>
        /// Gets the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>Task{ItemsResult}.</returns>
        protected ItemsResult GetResult(GetItemsByName request)
        {
            User            user = null;
            BaseItem        item;
            List <BaseItem> libraryItems;

            if (request.UserId.HasValue)
            {
                user = UserManager.GetUserById(request.UserId.Value);
                item = string.IsNullOrEmpty(request.ParentId) ? user.RootFolder : LibraryManager.GetItemById(request.ParentId);

                libraryItems = user.RootFolder.GetRecursiveChildren(user).ToList();
            }
            else
            {
                item = string.IsNullOrEmpty(request.ParentId) ? LibraryManager.RootFolder : LibraryManager.GetItemById(request.ParentId);

                libraryItems = LibraryManager.RootFolder.RecursiveChildren.ToList();
            }

            IEnumerable <BaseItem> items;

            if (item.IsFolder)
            {
                var folder = (Folder)item;

                if (request.UserId.HasValue)
                {
                    items = request.Recursive ? folder.GetRecursiveChildren(user) : folder.GetChildren(user, true);
                }
                else
                {
                    items = request.Recursive ? folder.GetRecursiveChildren() : folder.Children;
                }
            }
            else
            {
                items = new[] { item };
            }

            items = FilterItems(request, items);

            var extractedItems = GetAllItems(request, items);

            var filteredItems = FilterItems(request, extractedItems, user);

            filteredItems = FilterByLibraryItems(request, filteredItems, user, libraryItems);

            filteredItems = LibraryManager.Sort(filteredItems, user, request.GetOrderBy(), request.SortOrder ?? SortOrder.Ascending).Cast <TItemType>();

            var ibnItemsArray = filteredItems.ToList();

            IEnumerable <TItemType> ibnItems = ibnItemsArray;

            var result = new ItemsResult
            {
                TotalRecordCount = ibnItemsArray.Count
            };

            if (request.StartIndex.HasValue || request.Limit.HasValue)
            {
                if (request.StartIndex.HasValue)
                {
                    ibnItems = ibnItems.Skip(request.StartIndex.Value);
                }

                if (request.Limit.HasValue)
                {
                    ibnItems = ibnItems.Take(request.Limit.Value);
                }
            }

            var tuples = ibnItems.Select(i => new Tuple <TItemType, List <BaseItem> >(i, i.GetTaggedItems(libraryItems).ToList()));

            var dtoOptions = request.GetDtoOptions();

            var dtos = tuples.Select(i => GetDto(i.Item1, user, dtoOptions, i.Item2));

            result.Items = dtos.Where(i => i != null).ToArray();

            return(result);
        }
예제 #13
0
        private async Task <ItemsResult> GetSimilarItemsResult(BaseGetSimilarItemsFromItem request, Func <BaseItem, bool> includeInSearch, Func <BaseItem, BaseItem, int> getSimilarityScore)
        {
            var user = request.UserId.HasValue ? _userManager.GetUserById(request.UserId.Value) : null;

            var item = string.IsNullOrEmpty(request.Id) ?
                       (request.UserId.HasValue ? user.RootFolder :
                        _libraryManager.RootFolder) : _libraryManager.GetItemById(request.Id);

            Func <BaseItem, bool> filter = i => i.Id != item.Id && includeInSearch(i);

            var inputItems = user == null
                                 ? _libraryManager.RootFolder.GetRecursiveChildren(filter)
                                 : user.RootFolder.GetRecursiveChildren(user, filter);

            var list = inputItems.ToList();

            if (item is Movie && user != null && user.Configuration.IncludeTrailersInSuggestions)
            {
                var trailerResult = await _channelManager.GetAllMediaInternal(new AllChannelMediaQuery
                {
                    ContentTypes = new[] { ChannelMediaContentType.MovieExtra },
                    ExtraTypes   = new[] { ExtraType.Trailer },
                    UserId       = user.Id.ToString("N")
                }, CancellationToken.None).ConfigureAwait(false);

                var newTrailers = trailerResult.Items;

                list.AddRange(newTrailers);

                list = list
                       .DistinctBy(i => i.Name, StringComparer.OrdinalIgnoreCase)
                       .DistinctBy(i => i.GetProviderId(MetadataProviders.Imdb) ?? Guid.NewGuid().ToString(), StringComparer.OrdinalIgnoreCase)
                       .ToList();
            }

            if (item is Video)
            {
                var imdbId = item.GetProviderId(MetadataProviders.Imdb);

                // Use imdb id to try to filter duplicates of the same item
                if (!string.IsNullOrWhiteSpace(imdbId))
                {
                    list = list
                           .Where(i => !string.Equals(imdbId, i.GetProviderId(MetadataProviders.Imdb), StringComparison.OrdinalIgnoreCase))
                           .ToList();
                }
            }

            var items = SimilarItemsHelper.GetSimilaritems(item, list, getSimilarityScore).ToList();

            IEnumerable <BaseItem> returnItems = items;

            if (request.Limit.HasValue)
            {
                returnItems = returnItems.Take(request.Limit.Value);
            }

            var dtoOptions = GetDtoOptions(request);

            var result = new ItemsResult
            {
                Items = _dtoService.GetBaseItemDtos(returnItems, dtoOptions, user).ToArray(),

                TotalRecordCount = items.Count
            };

            return(result);
        }