using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyWebSite.Models;

namespace MyWebSite.Controllers
{
    public class BlogController : Controller
    {

        private MyWebSiteEntities _db = new MyWebSiteEntities();

        //
        // GET: /Blog/

        public ActionResult Index()
        {
            var articles = from a in _db.Articles
                           where a.Status.Equals(1)
                           orderby a.Published descending
                           select a;

            return View(articles);
        }

        public ActionResult Category(int id)
        {
            var categoryPosts = from a in _db.Articles
                                where a.Category == id && a.Status.Equals(1)
                                orderby a.Published descending
                                select a;

            return View("Index", categoryPosts);
        }

        [ChildActionOnly]
        public ActionResult CategoryList()
        {
            var categories = from c in _db.Categories
                             from a in _db.Articles
                             where c.CategoryId == a.Category && a.Status.Equals(1)
                             select c;

            return PartialView(categories);
        }

        public ActionResult Details(int id)
        {
            var singlePost = _db.Articles.Single(a => a.ArticleId == id);
            return View(singlePost);
        }

    }
}
