using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kendo.Mvc.UI;
using Kendo.Mvc.Extensions;
using RelatedGrids.Models;

namespace RelatedGrids.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult MasterRead([DataSourceRequest]DataSourceRequest request)
        {
            using (var context = new SampleEntities())
            {
                return Json(context.Categories.ToDataSourceResult(request, c => new CategoryViewModel
                {
                    CategoryID = c.CategoryID,
                    CategoryName = c.CategoryName
                }));
            }
        }

        public ActionResult DetailRead([DataSourceRequest]DataSourceRequest request, int categoryID)
        {
            using (var context = new SampleEntities())
            {
                var data = context.Products.Select(p => new ProductViewModel
                {
                    ProductID = p.ProductID,
                    ProductName = p.ProductName,
                    CategoryID = p.CategoryID
                })
                    // filter by the passed parent key
                .Where(p => p.CategoryID == categoryID);

                return Json(data.ToDataSourceResult(request));
            }
        }
    }
}
