Exemplo n.º 1
0
        public ActionResult Index(string id, int page = 1)
        {
            //On charge l'ensemble des posts de la catégorie
            IList<Post> posts = new List<Post>();
            using (BlogMvcContext db = new BlogMvcContext())
            {
                int startIndex = page <= 1
                    ? 0
                    : (page - 1) * this.ItemsByPage;

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


            ViewBag.Slug = id;


            //Mise en place d'une dépendance sur le cache
            if (HttpContext.Cache["Post"] == null)
                HttpContext.Cache["Post"] = DateTime.UtcNow.ToString();
            Response.AddCacheItemDependency("Post");


            return View(posts);
        }
Exemplo n.º 2
0
        public ActionResult Index()
        {

            HttpContext.Cache.Insert("Post", 1);
            Response.AddCacheItemDependency("Post");


            IList<Category> categories = new List<Category>();
            using (BlogMvcContext db = new BlogMvcContext())
            {
                categories = db.Categories
                    .Include(c => c.Posts)
                    .ToList();
            }


            IList<Post> posts = new List<Post>();
            using (BlogMvcContext db = new BlogMvcContext())
            {
                posts = db.Posts
                    .OrderByDescending(p => p.Created)
                    .Take(2)
                    .ToList();
            }

            return PartialView("_SideBar", new SideBarModel() { Categories = categories, Posts = posts });
        }
Exemplo n.º 3
0
        public ActionResult Pager()
        {
            int count = 0;
            int pages = 1;
            using (BlogMvcContext db = new BlogMvcContext())
            {
                count = db.Posts.Count();
            }
            pages = (count / this.ItemsByPage) + 1;

            return PartialView("~/Views/Shared/_Pager.cshtml", new PagerModel() { Count = count, Pages = pages });
        }
Exemplo n.º 4
0
        public ActionResult Details(string slug)
        {
            Post post = new Post();
            using (BlogMvcContext db = new BlogMvcContext())
            {
                post = db.Posts
                    .Include(p => p.Comments)
                    .Include(p => p.Category)
                    .Include(p => p.User)
                    .Where(p => p.Slug == slug)
                    .FirstOrDefault();

            }



            return View(post);
        }
        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 (BlogMvcContext db = new BlogMvcContext())
            {
                User user = db.Users.Where(u => u.Username == username).FirstOrDefault();


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


                HashAlgorithm hash = null;
                hash = System.Security.Cryptography.SHA1.Create();
                //Valeurs possibles => shA1, md5, KeyedHashAlgorithm, RIPEMD160

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



                return user.Password == hashedPassword;
            }
        }
Exemplo n.º 6
0
        public ActionResult Comment(Comment newComment)
        {
            Post post = new Post();
            using (BlogMvcContext db = new BlogMvcContext())
            {
                post = db.Posts
                    .Include(p => p.Comments)
                    .Include(p => p.Category)
                    .Include(p => p.User)
                    .Where(p => p.Id == newComment.Post_Id)
                    .FirstOrDefault();
            }

            if( !this.ModelState.IsValid )
            {
                return View("Details", post);
            }

            using (BlogMvcContext db = new BlogMvcContext())
            {
                newComment.Created = DateTime.Now;
                db.Comments.Add(newComment);
                db.SaveChanges();
            }

            return RedirectToAction("Details", new { slug = post.Slug });

        }