Pagination

mail@pastecode.io avatar
unknown
csharp
3 years ago
3.7 kB
3
Indexable
Never
public PageResponse<SPGetSalesOrder> GetSalesOrderData(string? SalesOrderNumber, int PageIndex, int PageSize, string SortColumn, string SortDirection)
        {
            if (string.IsNullOrEmpty(SortColumn)) SortColumn = "ReceivedDate";
            if (string.IsNullOrEmpty(SortDirection)) SortDirection = "DESC";
            String[] SalesOrderInput = SalesOrderNumber.Split(','); // split the salesorders and put in array
            foreach (String s in SalesOrderInput)
                Console.WriteLine(s);

            // Used for mapping the column name sent from UI and the Column to be sorted in the DB
            var propertyMapper = new Dictionary<string, Expression<Func<SPGetSalesOrder, object>>>
            {
                { "SalesOrderId", x => x.SalesOrderNumber },
                { "CustomerNumber", x => x.CustomerNumber },
                { "PurchaseOrderNumber", x => x.PurchaseOrder },
                { "Domsstatus", x => x.DomsStatus },
                { "ReceivedDate", x => x.ReceivedDate },
                { "EstimatedDeliveryDate", x => x.EstimatedDeliveryDate },
                { "ShipByDate", x => x.ShipByDate},
                { "SiteContact", x => x.SiteContact  },
                { "SalesRep", x => x.SalesRep },
                { "BusinessUnitId", x => x.BuId},
                { "Isbtspick", x => x.IsBTSPick },
                { "OrderStatus",  x => x.OrderStatus },
                { "IsFAAS",  x => x.IsFaas },
                { "IsConUS",  x => x.IsConUS },
                { "IsBOF", x => x.IsBOF }
            };
            // For Fetching last 6 months data
            var query = _dbContext.OrderHeader
                .Where(x => x.OrderDate >= DateTime.Now.AddMonths(-6));
            // For Searching input SalesOrderNumber from the user
            if (SalesOrderNumber != "")
            {
                query = query.Where(x => SalesOrderInput.Contains(x.SalesOrderId));
            }
            var totalRecords = query.Count();

            var results = query.Skip((PageIndex - 1) * PageSize)
                .Take(PageSize)
                .Select(x => new SPGetSalesOrder
                {
                    SalesOrderNumber = x.SalesOrderId,
                    CustomerNumber = x.CustomerNumber,
                    PurchaseOrder = x.PurchaseOrderNumber,
                    DomsStatus = x.Domsstatus,
                    ReceivedDate = x.ReceivedDate,
                    EstimatedDeliveryDate = x.EstimatedDeliveryDate,
                    ShipByDate = x.ShipByDate,
                    SiteContact = x.SiteContact,
                    SalesRep = x.SalesRep,
                    BuId = x.BusinessUnitId,
                    IsBTSPick = x.Isbtspick,
                    IsBOF = x.IsBOF,
                    OrderStatus = x.OrderStatusMaster.OrderStatus,
                    IsFaas = x.OrderTypeId == 1,
                    IsConUS = x.OrderTypeId == 0 || x.OrderTypeId == null
                });
            // Accomodate any type of SortDirection Case from UI by changing everything to uppercase
            SortDirection = SortDirection.ToUpper();

            if (SortDirection == "ASC")
                results = results.OrderBy(propertyMapper[SortColumn]);
            else
                results = results.OrderByDescending(propertyMapper[SortColumn]);

            PageResponse<SPGetSalesOrder> pageResponse = new PageResponse<SPGetSalesOrder>
            {
                DataItems = results.ToList(),
                TotalRecords = totalRecords
            };
            return pageResponse;

        }