public List <BasicCommentThread> GetAllCommentInThread(int threadId, string currentUserName) { int CurrenUser = 0; if (currentUserName != "") { using (var db = new UserDAL()) { CurrenUser = db.GetUserByUserNameOrEmail(currentUserName).UserID; } } List <BasicCommentThread> list = new List <BasicCommentThread>(); using (var db = new Ws_DataContext()) { try { var listComment = db.CommentThreads .Where(x => x.Status == true && x.ThreadId == threadId) .Select(x => new { x.UserId, x.Ws_User.UserName, x.Ws_User.User_Information.ProfileImage, x.CommentThreadId, x.Content, x.CommentDate, x.SubComment.Count }) .OrderByDescending(x => x.CommentDate).ToList(); foreach (var item in listComment) { BasicCommentThread bs = new BasicCommentThread(); bs.UserCommentedId = item.UserId; bs.UserCommentedName = item.UserName; bs.UserImageProfile = item.ProfileImage; bs.CommentId = item.CommentThreadId; bs.Content = item.Content; if (currentUserName != "") { if (db.LikeCommentThreads.Where(x => x.CommentId == item.CommentThreadId && x.UserId == CurrenUser && x.Status == true).SingleOrDefault() != null) { bs.isLiked = true; } } else { bs.isLiked = false; } if (CurrenUser == item.UserId) { bs.isDeleted = true; } else { bs.isDeleted = false; } bs.NumberSubComment = item.Count; if (DateTime.Now.Subtract(item.CommentDate).TotalHours <= 24 && DateTime.Now.Subtract(item.CommentDate).TotalHours >= 1) { bs.CommentedTime = Math.Round(DateTime.Now.Subtract(item.CommentDate).TotalHours, 0) + " Tiếng cách đây"; } else if (DateTime.Now.Subtract(item.CommentDate).TotalHours > 24) { bs.CommentedTime = item.CommentDate.ToString("H:mm:ss dd/MM/yy"); } else if (DateTime.Now.Subtract(item.CommentDate).Minutes == 0) { bs.CommentedTime = "Vừa xong"; } else { bs.CommentedTime = DateTime.Now.Subtract(item.CommentDate).Minutes + " Phút cách đây"; } bs.NumberOfLikes = db.LikeCommentThreads.Where(x => x.CommentId == item.CommentThreadId && x.Status == true).Count(); list.Add(bs); } } catch (Exception) { return(null); } } return(list); }
public List <ConservationBasicInfoDTO> GetAllConservationByUserIdAndStatus(string name, bool isRead) { int CurrenUser = 0; using (var db = new UserDAL()) { CurrenUser = db.GetUserByUserNameOrEmail(name).UserID; } List <ConservationBasicInfoDTO> ConservationList = new List <ConservationBasicInfoDTO>(); using (var db = new Ws_DataContext()) { var list = (from p in db.Conversation where (p.CreatorId == CurrenUser || p.ReceiverId == CurrenUser) select p).OrderByDescending(x => x.UpdatedTime).ToList(); foreach (var item in list) { if (CurrenUser == item.CreatorId && item.IsCreatorRead == isRead) { ConservationBasicInfoDTO current = new ConservationBasicInfoDTO(); //Set Image if (item.ReceiverId == CurrenUser) { current.AvatarUrl = item.Creator.User_Information.ProfileImage; current.CreatorName = item.Creator.UserName; } else { current.AvatarUrl = item.Receiver.User_Information.ProfileImage; current.CreatorName = item.Receiver.UserName; } //Set Time if (DateTime.Now.Subtract(item.UpdatedTime).TotalHours <= 24 && DateTime.Now.Subtract(item.UpdatedTime).TotalHours >= 1) { current.CreatedDate = Math.Round(DateTime.Now.Subtract(item.CreatedDate).TotalHours, 0) + " Tiếng cách đây"; } else if (DateTime.Now.Subtract(item.UpdatedTime).TotalHours > 24) { current.CreatedDate = item.UpdatedTime.ToString("H:mm:ss dd/MM/yy"); } else if (DateTime.Now.Subtract(item.CreatedDate).Minutes == 0) { current.CreatedDate = "Vừa xong"; } else { current.CreatedDate = DateTime.Now.Subtract(item.UpdatedTime).Minutes + " Phút cách đây"; } //Set other attributes current.Title = item.Title; current.ConservationId = item.ConservationId; ConservationList.Add(current); } if (CurrenUser == item.ReceiverId && item.IsReceiverRead == isRead) { ConservationBasicInfoDTO current = new ConservationBasicInfoDTO(); //Set Image if (item.ReceiverId == CurrenUser) { current.AvatarUrl = item.Creator.User_Information.ProfileImage; current.CreatorName = item.Creator.UserName; } else { current.AvatarUrl = item.Receiver.User_Information.ProfileImage; current.CreatorName = item.Receiver.UserName; } //Set Time if (DateTime.Now.Subtract(item.UpdatedTime).TotalHours <= 24 && DateTime.Now.Subtract(item.UpdatedTime).TotalHours >= 1) { current.CreatedDate = Math.Round(DateTime.Now.Subtract(item.CreatedDate).TotalHours, 0) + " Tiếng cách đây"; } else if (DateTime.Now.Subtract(item.UpdatedTime).TotalHours > 24) { current.CreatedDate = item.UpdatedTime.ToString("H:mm:ss dd/MM/yy"); } else if (DateTime.Now.Subtract(item.CreatedDate).Minutes == 0) { current.CreatedDate = "Vừa xong"; } else { current.CreatedDate = DateTime.Now.Subtract(item.UpdatedTime).Minutes + " Phút cách đây"; } //Set other attributes current.Title = item.Title; current.ConservationId = item.ConservationId; ConservationList.Add(current); } } return(ConservationList); } }