コード例 #1
0
        public ActionResult Index()
        {
            HttpContext.Cache.Insert("Post", 1);
            Response.AddCacheItemDependency("Post");

            IList <Category> categories = new List <Category>();

            using (GoodBlogDbContext db = new GoodBlogDbContext())
            {
                categories = db.Categories
                             .Include(c => c.Posts)
                             .ToList();
            }

            IList <Post> posts = new List <Post>();

            using (GoodBlogDbContext db = new GoodBlogDbContext())
            {
                posts = db.Posts
                        .OrderByDescending(p => p.Created)
                        .Take(2)
                        .ToList();
            }

            return(PartialView("_SideBar", new SideBarModel()
            {
                Categories = categories, Posts = posts
            }));
        }
コード例 #2
0
        public override bool ValidateUser(string username, string password)
        {
            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentException("Argument cannot be null or empty", "username");
            }

            if (string.IsNullOrEmpty(password))
            {
                throw new ArgumentException("Argument cannot be null or empty", "password");
            }

            using (GoodBlogDbContext db = new GoodBlogDbContext())
            {
                User user = db.Users.Where(u => u.Username == username).FirstOrDefault();

                if (user == null)
                {
                    return(false);
                }

                HashAlgorithm hash = null;
                hash = SHA1.Create();

                byte[] passBits       = hash.ComputeHash(Encoding.UTF8.GetBytes(password));
                string hashedPassword = BitConverter.ToString(passBits).Replace("-", "").ToLower();

                return(user.Password == hashedPassword);
            }
            //return base.ValidateUser(username, password);
        }
コード例 #3
0
        public ActionResult Comment(Comment newComment)
        {
            Post post = new Post();

            using (GoodBlogDbContext db = new GoodBlogDbContext())
            {
                post = db.Posts
                       .Include(p => p.Comments)
                       .Include(p => p.Category)
                       .Include(p => p.User)
                       .Where(p => p.Id == newComment.PostId)
                       .FirstOrDefault();
            }

            if (!this.ModelState.IsValid)
            {
                return(View("Details", post));
            }
            using (GoodBlogDbContext db = new GoodBlogDbContext())
            {
                newComment.Created = DateTime.Now;
                db.Comments.Add(newComment);
                db.SaveChanges();
            }

            return(RedirectToAction("Details", new { slug = post.Slug }));
        }
コード例 #4
0
        public ActionResult Index(int page = 1)
        {
            IList <Post> posts = new List <Post>();

            using (GoodBlogDbContext db = new GoodBlogDbContext())
            {
                int startIndex = page <= 1 ? 0 : (page - 1) * this.ItemsByPage;

                posts = db.Posts
                        .Include(p => p.Category)
                        .Include(p => p.User)
                        .OrderByDescending(p => p.Created)
                        .Skip(startIndex)
                        .Take(this.ItemsByPage)
                        .ToList();
            }

            if (HttpContext.Cache["Post"] == null)
            {
                HttpContext.Cache["Post"] = DateTime.UtcNow.ToString();
            }
            Response.AddCacheItemDependency("Post");

            return(View(posts));
        }
コード例 #5
0
        public ActionResult Pager(string id)
        {
            int count = 0;
            int pages = 1;
            using(GoodBlogDbContext db = new GoodBlogDbContext())
            {
                count = db.Posts.Where(p => p.Category.Slug == id).Count();
            }

            pages = (count / this.ItemsByPage) + 1;

            return PartialView("~/Views/Shared/_Pager.cshtml", new PagerModel() { Count = count, Pages = pages });
        }
コード例 #6
0
        public ActionResult Pager()
        {
            int count = 0;
            int pages = 1;

            using (GoodBlogDbContext db = new GoodBlogDbContext())
            {
                count = db.Posts.Count();
            }
            pages = (count / this.ItemsByPage) + 1;
            return(PartialView("~/Views/Shared/_Pager.cshtml", new PagerModel()
            {
                Count = count, Pages = pages
            }));
        }
コード例 #7
0
        public ActionResult Details(string slug)
        {
            Post post = new Post();

            using (GoodBlogDbContext db = new GoodBlogDbContext())
            {
                post = db.Posts
                       .Include(p => p.Comments)
                       .Include(p => p.Category)
                       .Include(p => p.User)
                       .Where(p => p.Slug == slug)
                       .FirstOrDefault();
            }

            return(View(post));
        }