public ActionResult Id(int id) { var writing = _workBL.GetWorks().Join(_userBL.GetUsers(), w => w.UserId, u => u.Id, (w, u) => new { Id = w.Id, Name = w.Title, DateOfPublication = w.DateOfPublication, Content = w.Content, IsDelete = u.IsDelete, Username = u.Login, GenreId = w.GenreId }).Join(_genreBL.GetGenres(), w => w.GenreId, g => g.Id, (w, g) => new UserWithWorkVM { Id = w.Id, Name = w.Name, DateOfPublication = w.DateOfPublication.Date, Content = w.Content, UserName = w.Username, GenreName = g.Name, IsDelete = w.IsDelete }).FirstOrDefault(item => item.Id == id); if (writing == null) { return(HttpNotFound()); } var ratings = _ratingBL.GetRatings().Where(q => q.WorkId == writing.Id).ToList(); double rating = ratings.Average(q => q.Rank); var comments = _commentBL.GetComments().Join(_workBL.GetWorks(), c => c.WorkId, w => w.Id, (c, w) => new { Id = c.Id, Comment = c.Comment, UserId = c.UserId, WorkId = c.WorkId }).Join(_userBL.GetUsers(), c => c.UserId, u => u.Id, (c, u) => new UserWithCommentVM { Id = c.Id, UserName = u.Login, Comment = c.Comment, WorkId = c.WorkId, IsDelete = u.IsDelete }).ToList(); var result = new WritingWithCommentsVM(writing, rating, comments); return(View(result)); }
public ActionResult GetComments(WorkVM work) { HttpCookie cookieReqs = Request.Cookies["Localhost cookie"]; int idsWork = work.Id; int idsUser = 0; if (cookieReqs != null) { idsUser = Convert.ToInt32(cookieReqs["ids"]); } else { FormsAuthentication.SignOut(); } if (work.UserId != 0) { RatingBL userRatingForWork = ratingBL.GetRatings().Where(x => x.UserId == idsUser && x.WorkId == idsWork).FirstOrDefault(); RatingBL newRating = new RatingBL { Rank = work.UserId, UserId = idsUser, WorkId = idsWork }; if (userRatingForWork != null) { RatingBL old = ratingBL.GetRating(userRatingForWork.Id); ratingBL.DeleteRating(old.Id); ratingBL.Create(newRating); } else { ratingBL.Create(newRating); } } if (work.Name != null) { CommentBL newComment = new CommentBL(); newComment.Comment = work.Name; newComment.UserId = idsUser; newComment.WorkId = idsWork; CommentBL test = commentBL.GetComments().Where(x => x.UserId == idsUser && x.WorkId == idsWork).FirstOrDefault(); if (null != commentBL.GetComments().Where(x => x.UserId == idsUser && x.WorkId == idsWork).FirstOrDefault()) { CommentBL old = commentBL.GetComment(test.Id); commentBL.DeleteComment(old.Id); commentBL.Create(newComment); } else { commentBL.Create(newComment); } } var commentList = commentBL.GetComments().Join(workBL.GetWorks(), c => c.WorkId, w => w.Id, (c, w) => new { Id = c.Id, Comment = c.Comment, UserId = c.UserId, WorkId = c.WorkId }).Join(userBL.GetUsers(), c => c.UserId, u => u.Id, (c, u) => new UserWithCommentVM { 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 <RatingBL> lst = ratingBL.GetRatings().Where(x => x.WorkId == idsWork).Join(userBL.GetUsers(), c => c.UserId, u => u.Id, (c, u) => new RatingBL { 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")); }