Example #1
0
File: Db.cs Project: IGionny/Molto
        public Page <T> Page <T>(long page, long itemsPerPage, string sql = null, params object[] args)
        {
            if (page < 1)
            {
                throw new ArgumentException($"{nameof(page)} can't be less than 1 (it's 1 base).");
            }

            if (itemsPerPage < 1)
            {
                throw new ArgumentException($"{nameof(itemsPerPage)} can't be less than 1.");
            }

            var result = new Page <T>();

            result.TotalItems = Count <T>(sql, args);
            var selectSql = _sqlQueryBuilder.SelectSql <T>(sql);
            var pageSql   = _sqlQueryBuilder.PageSql <T>(selectSql, page, itemsPerPage, result.TotalItems);

            result.Items        = Query <T>(pageSql).ToList();
            result.CurrentPage  = page;
            result.ItemsPerPage = itemsPerPage;
            result.TotalItems   = Count <T>(sql, args);
            result.TotalPages   = result.TotalItems / itemsPerPage;
            if ((result.TotalItems % itemsPerPage) != 0)
            {
                result.TotalPages++;
            }

            return(result);
        }