Exemple #1
0
 public static SearchViewModel ToViewModel(this ISearchDocumentDTO searchDocumentDTO)
 {
     return(searchDocumentDTO == null ?
            null :
            new SearchViewModel()
     {
         Count = searchDocumentDTO.Count,
         FormattedCount = searchDocumentDTO.Count.ToKiloFormat(),
         Results = searchDocumentDTO.Results.ToViewModel()
     });
 }
        // [EnableQuery(AllowedQueryOptions = AllowedQueryOptions.)]  // ? AllowedQueryOptions.Search is not present
        // http://192.168.1.124:8888/api/v1/search/get.frontiers(type='All')?$search=test
        public IHttpActionResult GetFrontiersResult([FromODataUri] string type, ODataQueryOptions <SearchViewModel> options)
        {
            SearchType searchType = SearchType.All;

            try
            {
                searchType = type.ParseEnum <SearchType>();
            }
            catch (Exception)
            {
                return(BadRequest($"search type {type} is not found"));
            }

            IEnumerable <KeyValuePair <string, string> > queryParameters = options.Request.GetQueryNameValuePairs();
            var searchData = queryParameters.Where(m => m.Key == "$search");

            if (searchData == null || !searchData.Any())
            {
                return(BadRequest("$search query is mandatory"));
            }

            int    skip       = options.Skip == null ? 0 : options.Skip.Value;
            int    top        = options.Top == null ? 20 : options.Top.Value;
            string searchText = searchData.FirstOrDefault().Value;

            ISearchDocumentDTO searchResult    = _frontiersSearchService.Search(searchText, searchType, skip, top);
            SearchViewModel    searchViewModel = searchResult.ToViewModel();

            if (searchType == SearchType.All)
            {
                IEnumerable <ISearchSummaryDocumentDTO> typeSummary = _frontiersSearchService.GetSearchTypeSummary(searchText, searchType);
                searchViewModel.Types = typeSummary.ToViewModel();
            }

            return(Ok(searchViewModel));
        }