Example #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));
        }
Example #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
            }));
        }
Example #3
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 }));
        }
Example #4
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
            }));
        }
Example #5
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);
            }
        }
 public LoginUsersController(BlogMvcContext context)
 {
     _context = context;
 }
Example #8
0
 public CategoriesController(BlogMvcContext context)
 {
     _context = context;
 }
Example #9
0
 public HomeController(BlogMvcContext context)
 {
     _context = context;
 }
Example #10
0
 public TagsController(BlogMvcContext context)
 {
     _context = context;
 }