Пример #1
0
        public ActionResult GetComments(WorksViewModel work)
        {
            HttpCookie cookieReqs = Request.Cookies["My localhost cookie"];
            int        idsWork    = work.Id;

            int idsUser = 0;

            if (cookieReqs != null)
            {
                idsUser = Convert.ToInt32(cookieReqs["ids"]);
            }
            else
            {
                FormsAuthentication.SignOut();
            }

            if (work.UserId != 0)
            {
                RatingsBO userRatingForWork = ratingServ.GetRatings().Where(x => x.UserId == idsUser && x.WorkId == idsWork).FirstOrDefault();

                RatingsBO newRating = new RatingsBO();
                newRating.Rank   = work.UserId;
                newRating.UserId = idsUser;
                newRating.WorkId = idsWork;

                if (userRatingForWork != null)
                {
                    RatingsBO old = ratingServ.GetRating(userRatingForWork.Id);
                    ratingServ.DeleteRating(old.Id);
                    ratingServ.Create(newRating);
                }
                else
                {
                    ratingServ.Create(newRating);
                }
            }



            if (work.Name != null)
            {
                CommentsBO newComment = new CommentsBO();
                newComment.Comment = work.Name;
                newComment.UserId  = idsUser;
                newComment.WorkId  = idsWork;
                CommentsBO test = commentServ.GetComments().Where(x => x.UserId == idsUser && x.WorkId == idsWork).FirstOrDefault();

                if (commentServ.GetComments().Where(x => x.UserId == idsUser && x.WorkId == idsWork).FirstOrDefault() != null)
                {
                    CommentsBO old = commentServ.GetComment(test.Id);
                    commentServ.DeleteComment(old.Id);
                    commentServ.Create(newComment);
                }
                else
                {
                    commentServ.Create(newComment);
                }
            }

            var commentList = commentServ.GetComments().Join(workServ.GetWorks(),
                                                             c => c.WorkId,
                                                             w => w.Id, (c, w) => new { Id = c.Id, Comment = c.Comment, UserId = c.UserId, WorkId = c.WorkId }).Join(userServ.GetUsers(),
                                                                                                                                                                     c => c.UserId,
                                                                                                                                                                     u => u.Id, (c, u) => new InfoModelWithComments {
                Id = c.Id, UserName = u.Login, Comment = c.Comment, WorkId = c.WorkId, IsDelete = u.IsDelete
            }).ToList();

            foreach (var item in commentList)
            {
                if (item.IsDelete == true)
                {
                    item.UserName = "******";
                }
            }
            ViewBag.CommentsList = commentList.Where(c => c.WorkId == idsWork).ToList();



            List <RatingsBO> lst = ratingServ.GetRatings().Where(x => x.WorkId == idsWork).Join(userServ.GetUsers(),
                                                                                                c => c.UserId,
                                                                                                u => u.Id, (c, u) => new RatingsBO {
                Id = c.Id, Rank = c.Rank, UserId = c.UserId, WorkId = c.WorkId, IsDeleteCheck = u.IsDelete
            }).Where(x => x.IsDeleteCheck == false).ToList();

            if (lst.Count != 0)
            {
                int sum    = 0;
                int rating = 0;
                foreach (var item in lst)
                {
                    sum += item.Rank;
                }
                rating          = sum / lst.Count;
                ViewBag.Ratings = rating;
            }
            else
            {
                ViewBag.Ratings = 0;
            }
            return(View("_CommentsTable"));
        }