Exemplo n.º 1
0
        public async Task <IActionResult> Search(SearchTypeData searchType, [FromBody] LoadSearchCriteria loadSearchCriteria)
        {
            try
            {
                if (!_userContext.UserId.HasValue)
                {
                    throw new EmptyUserIdException("Invalid UserId");
                }

                switch (searchType)
                {
                case SearchTypeData.UserLanes:
                    // We can do this out of order of a typical linq query b/c the Pageable Query will handle applying it to the IQueryable in the correct order.
                    var pageableLoads = _loadService
                                        .GetAllOpenLoads(_userContext.UserId.Value)
                                        .HandleSorting(Request)
                                        .HandlePaging(Request);

                    if (loadSearchCriteria != null)
                    {
                        pageableLoads.Filter(QueryFilters.GetCarrierLoadFilter(loadSearchCriteria));
                    }

                    return(Success(await pageableLoads.ToPageableResponse()));

                case SearchTypeData.Booked:
                {
                    pageableLoads = _loadService
                                    .GetBookedLoads(_userContext.UserId.Value)
                                    .HandleSorting(Request)
                                    .HandlePaging(Request);

                    if (loadSearchCriteria != null)
                    {
                        pageableLoads.Filter(QueryFilters.GetCarrierLoadFilter(loadSearchCriteria));
                    }

                    var pageableResult = await pageableLoads.ToPageableResponse();

                    if (string.IsNullOrWhiteSpace(Request.Query[PageableQueryConstants.OrderByQuery]))
                    {
                        var loads = pageableResult.Data;

                        var visibilityTypes = _util.GetVisibilityTypes(_userContext.UserId.Value);

                        if (visibilityTypes != null && visibilityTypes.Count > 0)
                        {
                            var loadsToProcess = loads.Where(x => x.DestLateDtTm >= DateTime.Now).ToList();

                            if (loadsToProcess.Count() > 0)
                            {
                                _util.CheckLoadsForVisibility(loadsToProcess, _userContext.Token, visibilityTypes);
                            }
                        }

                        var loadsWithWarnings = loads.Where(x => x.ShowVisibilityWarning == true).OrderBy(x => x.OriginLateDtTm).ToList();
                        var loadsNoWarnings   = loads.Where(x => x.ShowVisibilityWarning == false).OrderByDescending(x => x.OriginLateDtTm).ToList();

                        pageableResult.Data.Clear();
                        pageableResult.Data.AddRange(loadsWithWarnings);
                        pageableResult.Data.AddRange(loadsNoWarnings);
                    }
                    return(Success(pageableResult));
                }

                case SearchTypeData.Delivered:

                    pageableLoads = _loadService
                                    .GetDeliveredLoads(_userContext.UserId.Value)
                                    .HandleSorting(Request)
                                    .HandlePaging(Request);

                    if (loadSearchCriteria != null)
                    {
                        pageableLoads.Filter(QueryFilters.GetCarrierLoadFilter(loadSearchCriteria));
                    }

                    return(Success(await pageableLoads.ToPageableResponse()));

                default:
                    return(Error <List <LoadData> >(new Exception("SearchType not valid")));
                }
            }
            catch (UnauthorizedAccessException ex)
            {
                return(Forbidden <ResponseMessage <List <LoadData> > >(ex));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }