示例#1
0
        public async Task <IEnumerable <CategoryGroupDto> > GetGroupsAsync(string type)
        {
            List <CategoryEntity> entities = await _categoryRepo
                                             .Select
                                             .Where(c => c.IsDeleted == false)
                                             .WhereIf(type.IsNotNullOrEmpty(), c => c.Type.Equals(type))
                                             .ToListAsync();

            List <CategoryEntity>   parents = entities.FindAll(c => c.ParentId == 0).OrderBy(d => d.Sort).ToList();
            List <CategoryGroupDto> dtos    = parents
                                              .Select(c =>
            {
                var dto    = new CategoryGroupDto();
                dto.Name   = c.Name;
                dto.Childs = entities
                             .FindAll(d => d.ParentId == c.Id)
                             .Select(d =>
                {
                    var s     = Mapper.Map <CategoryDto>(d);
                    s.IconUrl = _fileRepo.GetFileUrl(s.IconUrl);
                    return(s);
                }).OrderBy(d => d.Sort)
                             .ToList();
                return(dto);
            })
                                              .ToList();

            return(dtos);
        }
        public async Task <IList <SearchResultDto> > Search(string searchText)
        {
            //This is here just for convenience to avoid create FTS indexes manually
            if (!_ftsEngine.DoIndexesExist())
            {
                await CreateFtsIndexesAsync();
            }

            var result          = new List <SearchResultDto>();
            var restaurants     = _searchManager.SearchRestaurants(searchText).OrderByDescending(x => x.Value.Count).ThenBy(x => x.Key.Rank);
            var foundCategories = _searchManager.SearchCategories(searchText);

            //TODO: Automapper could be used
            foreach (KeyValuePair <Restaurant, List <MenuItem> > keyValuePair in restaurants)
            {
                var resultDto = new SearchResultDto
                {
                    Id             = keyValuePair.Key.Id,
                    LogoPath       = keyValuePair.Key.LogoPath,
                    Rank           = keyValuePair.Key.Rank,
                    RestaurantName = keyValuePair.Key.Name,
                    Suburb         = keyValuePair.Key.Suburb
                };

                result.Add(resultDto);

                foreach (MenuItem menuItem in keyValuePair.Value)
                {
                    var category    = foundCategories.FirstOrDefault(x => x.Name == menuItem.Category.Name);
                    var menuItemDto = new MenuItemDto
                    {
                        Id    = menuItem.Id,
                        Name  = menuItem.Name,
                        Price = menuItem.Price
                    };

                    if (category != null)
                    {
                        var categoryDto = resultDto.CategoryGroups.FirstOrDefault(x => x.Id == category.Id);

                        if (categoryDto == null)
                        {
                            categoryDto = new CategoryGroupDto
                            {
                                Name = category.Name,
                                Id   = category.Id
                            };

                            resultDto.CategoryGroups.Add(categoryDto);
                        }

                        categoryDto.MenuItems.Add(menuItemDto);
                    }
                    else
                    {
                        resultDto.MenuItems.Add(menuItemDto);
                    }
                }
            }

            return(result);
        }