public async Task <IEnumerable <SearchResultItemViewModel> > SearchItems(string itemsType = "All", string orderBy = "-CreationDate", string searchText = "", SearchRequestFilterUrlParameters filters = null) { SearchRequestModel searchRequestModel; try { searchRequestModel = await ConvertUrlParametersToSearchModelAsync(itemsType, searchText, orderBy, filters); } catch (Exception e) { throw; } var mappedRequest = Mapper.Map(searchRequestModel, new SearchRequest()); IEnumerable <SearchResultItem> searchResult; try { searchResult = await _searchService.SearchWithFiltersAsync( await _securityService.GetUserByIdAsync(User.Identity.GetUserId()), mappedRequest); } catch (Exception e) { throw; } var mappedResult = Mapper.Map <IEnumerable <SearchResultItem>, IEnumerable <SearchResultItemViewModel> >(searchResult); return(mappedResult); }
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) { if (bindingContext.ModelType != typeof(SearchRequestFilterUrlParameters)) { return(false); } ValueProviderResult val = bindingContext.ValueProvider.GetValue( bindingContext.ModelName); if (val == null) { return(false); } string key = val.RawValue as string; if (key == null) { bindingContext.ModelState.AddModelError( bindingContext.ModelName, "Wrong value type"); return(false); } var result = new SearchRequestFilterUrlParameters(); try { var pairs = key.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (var pair in pairs) { var keyValue = pair.Split('='); result.Add(new KeyValuePair <string, string>(keyValue[0], keyValue[1])); } } catch { bindingContext.ModelState.AddModelError( bindingContext.ModelName, "Cannot convert filter parameters"); return(false); } bindingContext.Model = result; return(true); }
private async Task <SearchRequestModel> ConvertUrlParametersToSearchModelAsync(string itemsType, string searchText, string orderBy, SearchRequestFilterUrlParameters filters) { var searchRequestModel = new SearchRequestModel(); searchRequestModel.SearchItemsType = itemsType; searchRequestModel.SearchText = searchText; searchRequestModel.OrderBy = orderBy; var serchResultItemType = (SearchResultItemType)Enum.Parse(typeof(SearchResultItemType), itemsType); var parameters = (await _searchService.GetFilterParametersAsync(serchResultItemType, false)).ToList(); if (filters != null) { foreach (var filter in filters) { var foundFilter = parameters.SelectMany(s => s.Items).Where(f => f.Name == filter.Key).FirstOrDefault(); ((List <SearchRequestFilterModel>)searchRequestModel.Filters).Add(new SearchRequestFilterModel() { FilterType = foundFilter.FilterType.ToString(), Name = filter.Key, Value = filter.Value }); } } return(searchRequestModel); }