public ActionResult CommentsDelete([DataSourceRequest]DataSourceRequest request, CommentsViewModel commentVm)
        {

            if (ModelState.IsValid)
            {
                var comment = db.Comments.GetById(commentVm.Id);
                db.Comments.Delete(comment);
                db.SaveChanges();
            }
            return Json(new[] { commentVm }.ToDataSourceResult(request, ModelState));
        }
Exemple #2
0
        //public static Laptop ToModel(this Laptop laptop)
        //{
        //    return new Laptop
        //    {
        //        Model = laptop.Laptop.Model,
        //        Description = laptop.Laptop.Description,
        //        AdditionalParts = laptop.Laptop.AdditionalParts,
        //        Harddisk = laptop.Laptop.Harddisk,
        //        Image = laptop.Laptop.Image,
        //        Inches = laptop.Laptop.Inches,
        //        Price = laptop.Laptop.Price,
        //        Ram = laptop.Laptop.Ram,
        //        Weight = laptop.Laptop.Weight,

        //    };
        //}

        public static CommentsViewModel ToViewModel(this Comment comment)
        {
            CommentsViewModel vm = new CommentsViewModel
            {
                Content = comment.Content,
                Id      = comment.Id
            };

            if (comment.Laptop != null)
            {
                vm.OnLaptop = comment.Laptop.Model;
            }
            if (comment.User != null)
            {
                vm.ByUser = comment.User.UserName;
            }

            return(vm);
        }
        //public static Laptop ToModel(this Laptop laptop)
        //{
        //    return new Laptop
        //    {
        //        Model = laptop.Laptop.Model,
        //        Description = laptop.Laptop.Description,
        //        AdditionalParts = laptop.Laptop.AdditionalParts,
        //        Harddisk = laptop.Laptop.Harddisk,
        //        Image = laptop.Laptop.Image,
        //        Inches = laptop.Laptop.Inches,
        //        Price = laptop.Laptop.Price,
        //        Ram = laptop.Laptop.Ram,
        //        Weight = laptop.Laptop.Weight,
                
        //    };
        //}

        public static CommentsViewModel ToViewModel(this Comment comment)
        {
            CommentsViewModel vm = new CommentsViewModel
            {
                Content = comment.Content,
                Id = comment.Id
            };

            if (comment.Laptop != null)
            {
                vm.OnLaptop = comment.Laptop.Model;
            }
            if (comment.User != null)
            {
                vm.ByUser = comment.User.UserName;
            }

            return vm;
        }
        public ActionResult CommentsUpdate([DataSourceRequest]DataSourceRequest request, CommentsViewModel commentVm)
        {
            if (ModelState.IsValid)
            {
                

                var comment = db.Comments.All("User", "Laptop").Single(x => x.Id == commentVm.Id);

                if (comment.User.UserName != commentVm.ByUser)
                {
                    var user = db.Users.All().FirstOrDefault(x => x.UserName == commentVm.ByUser);
                    comment.User = user;
                    comment.Content = commentVm.Content;

                    //UpdateBookFields(book, oldBook);
                    //user.Books.Add(oldBook);
                    //db.Comments.Update(comment);
                    db.SaveChanges();
                }
                if (comment.Laptop == null || comment.Laptop.Model != commentVm.OnLaptop)
                {
                    var laptop = db.Laptops.All().FirstOrDefault(x => x.Model == commentVm.OnLaptop);
                    if (laptop != null)
                    {
                        comment.Laptop = laptop;
                        
                    }
                }
                //UpdateBookFields(book, oldBook);
                comment.Content = commentVm.Content;

                db.Comments.Update(comment);
                db.SaveChanges();

            }
            return Json(new[] { commentVm }.ToDataSourceResult(request, ModelState));
        }