طراحی یک گرید با Angular و ASP.NET Core - قسمت اول - پیاده سازی سمت سرور
نویسنده: وحید نصیری
تاریخ: ۱۳۹۶/۰۴/۳۰ ۱۲:۲۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
namespace AngularTemplateDrivenFormsLab.Models
{
public class Product
{
public int ProductId { set; get; }
public string ProductName { set; get; }
public decimal Price { set; get; }
public bool IsAvailable { set; get; }
}
} using System.Collections.Generic;
namespace AngularTemplateDrivenFormsLab.Models
{
public static class ProductDataSource
{
private static readonly IList<Product> _cachedItems;
static ProductDataSource()
{
_cachedItems = createProductsDataSource();
}
public static IList<Product> LatestProducts
{
get { return _cachedItems; }
}
private static IList<Product> createProductsDataSource()
{
var list = new List<Product>();
for (var i = 0; i < 1500; i++)
{
list.Add(new Product
{
ProductId = i + 1,
ProductName = "نام " + (i + 1),
IsAvailable = (i % 2 == 0),
Price = 1000 + i
});
}
return list;
}
}
} http://localhost:5000/api/Product/GetPagedProducts?sortBy=productId&isAscending=true&page=2&pageSize=7
public interface IPagedQueryModel
{
string SortBy { get; set; }
bool IsAscending { get; set; }
int Page { get; set; }
int PageSize { get; set; }
} public class ProductQueryViewModel : IPagedQueryModel
{
// ... other properties ...
public string SortBy { get; set; }
public bool IsAscending { get; set; }
public int Page { get; set; }
public int PageSize { get; set; }
} public static class IQueryableExtensions
{
public static IQueryable<T> ApplyPaging<T>(
this IQueryable<T> query, IPagedQueryModel model)
{
if (model.Page <= 0)
{
model.Page = 1;
}
if (model.PageSize <= 0)
{
model.PageSize = 10;
}
return query.Skip((model.Page - 1) * model.PageSize).Take(model.PageSize);
}
} http://localhost:5000/api/Product/GetPagedProducts?sortBy=productId&isAscending=true&page=2&pageSize=7
if(model.SortBy == "f1")
{
query = !model.IsAscending ? query.OrderByDescending(x => x.F1) : query.OrderBy(x => x.F1);
} namespace AngularTemplateDrivenFormsLab.Utils
{
public static class IQueryableExtensions
{
public static IQueryable<T> ApplyOrdering<T>(
this IQueryable<T> query,
IPagedQueryModel model,
IDictionary<string, Expression<Func<T, object>>> columnsMap)
{
if (string.IsNullOrWhiteSpace(model.SortBy) || !columnsMap.ContainsKey(model.SortBy))
{
return query;
}
if (model.IsAscending)
{
return query.OrderBy(columnsMap[model.SortBy]);
}
else
{
return query.OrderByDescending(columnsMap[model.SortBy]);
}
}
}
} var columnsMap = new Dictionary<string, Expression<Func<Product, object>>>()
{
["productId"] = p => p.ProductId,
["productName"] = p => p.ProductName,
["isAvailable"] = p => p.IsAvailable,
["price"] = p => p.Price
};
query = query.ApplyOrdering(queryModel, columnsMap); using System.Collections.Generic;
namespace AngularTemplateDrivenFormsLab.Models
{
public class PagedQueryResult<T>
{
public int TotalItems { get; set; }
public IEnumerable<T> Items { get; set; }
}
} [Route("api/[controller]")]
public class ProductController : Controller
{
[HttpGet("[action]")]
public PagedQueryResult<Product> GetPagedProducts(ProductQueryViewModel queryModel)
{
var pagedResult = new PagedQueryResult<Product>();
var query = ProductDataSource.LatestProducts
.AsQueryable();
//TODO: Apply Filtering ... .where(p => p....) ...
var columnsMap = new Dictionary<string, Expression<Func<Product, object>>>()
{
["productId"] = p => p.ProductId,
["productName"] = p => p.ProductName,
["isAvailable"] = p => p.IsAvailable,
["price"] = p => p.Price
};
query = query.ApplyOrdering(queryModel, columnsMap);
pagedResult.TotalItems = query.Count();
query = query.ApplyPaging(queryModel);
pagedResult.Items = query.ToList();
return pagedResult;
}
} public PagedQueryResult<Product> GetPagedProducts(ProductQueryViewModel queryModel)
var query = ProductDataSource.LatestProducts .AsQueryable(); //TODO: Apply Filtering ... .where(p => p....) ...
var columnsMap = new Dictionary<string, Expression<Func<Product, object>>>()
{
["productId"] = p => p.ProductId,
["productName"] = p => p.ProductName,
["isAvailable"] = p => p.IsAvailable,
["price"] = p => p.Price
};
query = query.ApplyOrdering(queryModel, columnsMap); var pagedResult = new PagedQueryResult<Product>(); pagedResult.TotalItems = query.Count(); query = query.ApplyPaging(queryModel); pagedResult.Items = query.ToList(); return pagedResult;