Example #1
0
    public static IObjectFieldDescriptor UseLimitOffsetPaging <TSchemaType>(
        this IObjectFieldDescriptor descriptor)
        where TSchemaType : class
    {
        descriptor
        .Type <PaginationPayloadType <TSchemaType> >()
        .Argument("pageIndex", a => a.Type <IntType>())
        .Argument("pageSize", a => a.Type <IntType>())
        .Use(next => async context =>
        {
            await next(context);

            if (context.Result is IQueryable <TSchemaType> list)
            {
                var paginatedList = await PaginatedList <TSchemaType> .CreateAsync(list,
                                                                                   context.Argument <int>("pageIndex"), context.Argument <int>("pageSize"));
                var result = new PaginationPayload <TSchemaType>(paginatedList.ToList(),
                                                                 paginatedList.HasNextPage, paginatedList.HasPreviousPage);
                context.Result = result;
            }
        });

        return(descriptor);
    }
        public async Task <IActionResult> Get([FromQuery] int?offset, int?limit)
        {
            IActionResult response = Unauthorized();

            if (CheckIsValidJwt())
            {
                // Data Payload
                var employees = new List <EmployeesPayload>();

                var resp = await _fireObj.GetEmployees();

                var jsonString = resp.Content.ReadAsStringAsync().Result;

                if (resp.StatusCode == HttpStatusCode.OK && (jsonString != "null" && jsonString != null))
                {
                    var jObj = JObject.Parse(jsonString);

                    foreach (var item in jObj)
                    {
                        var emp = item.Value.ToObject <EmployeesPayload>();

                        var listTypes = new List <string>();
                        listTypes.Add("application/json");

                        emp.links = new EmployeesPayloadLinksDetail
                        {
                            rel    = "Employee information of " + emp.Name + " " + emp.Surname,
                            href   = Request.Scheme + "://" + Request.Host + Request.Path.Value + "/" + emp.Id,
                            action = "GET",
                            types  = listTypes
                        };

                        if (emp.Status == true)
                        {
                            employees.Add(emp);
                        }
                    }

                    // Pagination Payload
                    var pTotal = employees.Count;

                    var pOffset = 0;
                    var pLimit  = 0;
                    var pNext   = 0;
                    var pPrev   = 0;
                    var pFirst  = 0;
                    var pLast   = 0;

                    if ((offset != null && limit != null)) // With Pagination QueryString
                    {
                        pOffset = offset.Value;

                        if (offset >= 0 && limit >= 1)
                        {
                            employees = employees.OrderBy(u => u.Id).Skip(offset.Value).Take(limit.Value).ToList();

                            pLimit = limit.Value;

                            pNext = pOffset + pLimit >= pTotal ? pOffset : pOffset + pLimit;
                            pPrev = pOffset - pLimit < 0 ? 0 : pOffset - pLimit;
                            pLast = (((pTotal - 1) - pOffset) % pLimit) == 0 ? pTotal - 1 : (pTotal - 1) - (((pTotal - 1) - pOffset) % pLimit);
                        }
                    }
                    else // Without Pagination QueryString
                    {
                        pOffset = 0;

                        if (pTotal < DEFAULT_LIMIT)
                        {
                            employees = employees.OrderBy(u => u.Id).Skip(0).Take(pTotal).ToList();

                            pLimit = pTotal;

                            pNext = 0;
                            pPrev = 0;
                            pLast = 0;
                        }
                        else
                        {
                            employees = employees.OrderBy(u => u.Id).Skip(0).Take(DEFAULT_LIMIT).ToList();

                            pLimit = DEFAULT_LIMIT;

                            pNext = pOffset + pLimit >= pTotal ? pOffset : pOffset + pLimit;
                            pPrev = 0;
                            pLast = (((pTotal - 1) - pOffset) % pLimit) == 0 ? pTotal - 1 : (pTotal - 1) - (((pTotal - 1) - pOffset) % pLimit);
                        }
                    }

                    var pagination = new PaginationPayload
                    {
                        offset = pOffset,
                        limit  = pLimit,
                        total  = pTotal,
                        links  = new PaginationPayloadLinksDetail
                        {
                            next  = Request.Scheme + "://" + Request.Host + Request.Path.Value + "?offset=" + pNext + "&limit=" + pLimit,
                            prev  = Request.Scheme + "://" + Request.Host + Request.Path.Value + "?offset=" + pPrev + "&limit=" + pLimit,
                            first = Request.Scheme + "://" + Request.Host + Request.Path.Value + "?offset=" + pFirst + "&limit=" + pLimit,
                            last  = Request.Scheme + "://" + Request.Host + Request.Path.Value + "?offset=" + pLast + "&limit=" + pLimit
                        }
                    };

                    response = Ok(new { employees, pagination });
                }
                else
                {
                    response = CustomHttpResponse.Error(HttpStatusCode.NotFound, "UND002", "No Data Found.", "ไม่พบข้อมูล", "No data found.");
                }
            }
            else
            {
                response = CustomHttpResponse.Error(HttpStatusCode.Unauthorized, "UND999", "Unauthorized, Invalid AccessToken.", "แอพฯ ของคุณไม่มีสิทธิ์ใช้งาน เนื่องจาก AccessToken ไม่ถูกต้อง หรือหมดอายุแล้ว, กรุณาติดต่อผู้ดูแลแอพฯ ของคุณ", "The AccessToken is invalid or expired, please contact your Application Administrator.");
            }

            return(response);
        }