Пример #1
0
        public ActionResult AddComment(int id, string Content, int rating)
        {
            //取得使用者Id
            var UserId = HttpContext.User.Identity.Name;

            var currentDateTime = DateTime.Now;

            using (UserEntities Userdb = new UserEntities())
            {
                var NickName = (from s in Userdb.AspNetUsers where s.UserName == UserId select s.Name).FirstOrDefault();
                var ImgUrl   = (from s in Userdb.AspNetUsers where s.UserName == UserId select s.ImgUrl).FirstOrDefault();
                rating = (rating <= 0) ? 1
                    :(rating >= 5) ? 5
                    : rating;


                var comment = new ProductCommet()
                {
                    ProductId  = id,
                    UserId     = NickName,
                    Content    = Content,
                    CreateDate = currentDateTime,
                    ImgUrl     = ImgUrl,
                    Stars      = rating
                };
                using (CartsEntities db = new CartsEntities())
                {
                    db.ProductCommets.Add(comment);
                    db.SaveChanges();
                }
            }
            return(RedirectToAction("Detail", new { id = id }));
        }
        private void InitProductAndProductComment()
        {
            var product = new Product()
            {
                Id          = 1,
                Name        = "Book",
                Description = "This is a book"
            };

            var productComment1 = new ProductCommet()
            {
                Id         = 1,
                ProductId  = 1,
                Content    = "This is good.",
                CreateDate = DateTime.Now
            };
            var productComment2 = new ProductCommet()
            {
                Id         = 2,
                ProductId  = 1,
                Content    = "This is bad.",
                CreateDate = DateTime.Now
            };

            _context.Products.Add(product);
            _context.ProductCommets.Add(productComment1);
            _context.ProductCommets.Add(productComment2);
            _context.SaveChanges();
        }
        private void InitProductAndComment()
        {
            var productManager = _IOC.GetService <IProductManager>();

            var product = new Product()
            {
                Id          = 1,
                Name        = "Book",
                Description = "This is a book"
            };

            var productComment1 = new ProductCommet()
            {
                Id         = 1,
                ProductId  = 1,
                Content    = "This is good.",
                CreateDate = DateTime.Now
            };
            var productComment2 = new ProductCommet()
            {
                Id         = 2,
                ProductId  = 1,
                Content    = "This is bad.",
                CreateDate = DateTime.Now
            };

            productManager.ProductRepo.Create(product);
            productManager.ProductCommentRepo.Create(productComment1);
            productManager.ProductCommentRepo.Create(productComment2);
        }
        public void ProductManagerInsertTest()
        {
            var productManager = _IOC.GetService <IProductManager>();
            var product        = new Product()
            {
                Id          = 2,
                Name        = "Book",
                Description = "This is a book"
            };

            var productComment1 = new ProductCommet()
            {
                Id         = 998,
                ProductId  = 2,
                Content    = "This is good.",
                CreateDate = DateTime.Now
            };
            var productComment2 = new ProductCommet()
            {
                Id         = 999,
                ProductId  = 2,
                Content    = "This is bad.",
                CreateDate = DateTime.Now
            };

            productManager.ProductRepo.Create(product);
            productManager.ProductCommentRepo.Create(productComment1);
            productManager.ProductCommentRepo.Create(productComment2);

            var result1 = productManager.ProductRepo.FindByCondition(p => p.Id == 2).FirstOrDefault();
            var result2 = productManager.ProductCommentRepo.FindByCondition(p => p.ProductId == 2).FirstOrDefault();

            Assert.NotNull(result1);
            Assert.NotNull(result2);
        }
        [Authorize] // must login to add comment
        public ActionResult AddReview(int id, string pContent)
        {
            var userName = User.FindFirst(ClaimTypes.Name).Value; // will give the user's userName

            var currentDateTime = DateTime.Now;

            var comment = new ProductCommet
            {
                ProductId  = id,
                Content    = pContent,
                UserId     = userName,
                CreateDate = currentDateTime
            };

            _productManager.ProductCommentRepo.Create(comment);

            return(RedirectToAction("Details", new { pId = id }));
        }