public bool AddCommentHistory(int userid) { try { var comments = _dbContext.Comments.ToList(); foreach (var comment in comments) { var item = _dbContext.SeenComments.FirstOrDefault(x => x.UserID == comment.UserID && x.UserID == comment.ID); if (item == null) { var seencmt = new SeenComment(); seencmt.CommentID = comment.ID; seencmt.UserID = userid; seencmt.Status = true; _dbContext.SeenComments.Add(seencmt); _dbContext.SaveChanges(); } } return(true); } catch (Exception) { return(false); } }
public async Task <bool> AddCommentHistory(int userid, int dataid) { try { var comments = await _dbContext.Comments.Where(x => x.DataID == dataid).ToListAsync(); foreach (var comment in comments) { var item = await _dbContext.SeenComments.FirstOrDefaultAsync(x => x.UserID == userid && x.CommentID == comment.ID); if (item == null) { var seencmt = new SeenComment(); seencmt.CommentID = comment.ID; seencmt.UserID = userid; seencmt.Status = true; _dbContext.SeenComments.Add(seencmt); await _dbContext.SaveChangesAsync(); } } return(true); } catch (Exception) { return(false); } }
/// <summary> /// Tạo mới SeenComment /// </summary> /// <param name="seenComment"></param> /// <returns>Trả về item SeenComment</returns> public async Task <SeenComment> CreateSeenComment(SeenComment seenComment) { try { _dbContext.SeenComments.Add(seenComment); await _dbContext.SaveChangesAsync(); return(seenComment); } catch (Exception) { return(seenComment); } }
public async Task <AddCommentVM> AddComment(AddCommentViewModel entity, int levelIDOfUserComment) { var listEmail = new List <string[]>(); var listTags = new List <Tag>(); var listFullNameTag = new List <string>(); var user = _dbContext.Users; var dataModel = _dbContext.Datas; try { //add vao comment var comment = new Comment(); comment.CommentMsg = entity.CommentMsg; comment.DataID = entity.DataID; comment.UserID = entity.UserID;//sender comment.Link = entity.Link; comment.Title = entity.Title; await CreateComment(comment); //B1: Xu ly viec gui thong bao den Owner khi nguoi gui cap cao hon comment //Tim levelNumber cua user comment var kpilevelIDResult = await _dbContext.KPILevels.FirstOrDefaultAsync(x => x.KPILevelCode == entity.KPILevelCode); var userIDResult = await _dbContext.Owners.FirstOrDefaultAsync(x => x.KPILevelID == kpilevelIDResult.ID && x.CategoryID == entity.CategoryID); var userModel = await _dbContext.Users.FindAsync(userIDResult.UserID); //Lay ra danh sach owner thuoc categoryID va KPILevelCode var owners = await _dbContext.Owners.Where(x => x.KPILevelID == kpilevelIDResult.ID && x.CategoryID == entity.CategoryID).ToListAsync(); //Neu nguoi comment ma la cap cao hon owner thi moi gui thong bao va gui mail cho owner if (await CheckLevelNumberOfUser(levelIDOfUserComment, userModel.LevelID)) { owners.ForEach(userItem => { //Add Tag gui thong bao den cac owner if (entity.UserID != userItem.ID) //Neu chinh owner do binh luan thi khong gui thong bao { var itemtag = new Tag(); itemtag = new Tag(); itemtag.UserID = userItem.ID; itemtag.CommentID = comment.ID; listTags.Add(itemtag); //Day la danh sach tag //Add vao list gui mail string[] arrayString = new string[5]; arrayString[0] = user.Find(entity.UserID).Alias; //Bi danh arrayString[1] = user.Find(entity.UserID).Email; arrayString[2] = comment.Link; arrayString[3] = comment.Title; arrayString[4] = comment.CommentMsg; listEmail.Add(arrayString); } }); //B2: Neu ma nguoi cap cao hon owner tag ai do vao comment cua ho thi se gui mail va thong bao den nguoi do if (!entity.Tag.IsNullOrEmpty()) { var result = await CreateTag(entity.Tag, entity.UserID, comment); listEmail = result.Item1; listFullNameTag = result.Item2; listTags = result.Item3; } } else //Neu user co level nho hon owner commnent thi gui den owner { //B1: Gui thong bao den cac owner owners.ForEach(x => { //Add vao Tag de gui thong if (entity.UserID != x.UserID) { var itemtag = new Tag(); itemtag = new Tag(); itemtag.UserID = x.UserID; itemtag.CommentID = comment.ID; listTags.Add(itemtag); //Day la danh sach tag } }); //B2: Neu tag ai thi gui thong bao den nguoi do if (!entity.Tag.IsNullOrEmpty()) { var result = await CreateTag(entity.Tag, entity.UserID, comment); listEmail = result.Item1; listFullNameTag = result.Item2; listTags = result.Item3; } } //Add vao seencomment var seenComment = new SeenComment(); seenComment.CommentID = comment.ID; seenComment.UserID = entity.UserID; seenComment.Status = true; _dbContext.Tags.AddRange(listTags); await _dbContext.SaveChangesAsync(); await CreateSeenComment(seenComment); if (listTags.Count > 0) { //Add vao Notification var notify = new Notification(); notify.CommentID = comment.ID; notify.Content = comment.CommentMsg; notify.UserID = entity.UserID; //sender notify.Title = comment.Title; notify.Link = comment.Link; notify.Action = "Comment"; notify.Tag = string.Join(",", listFullNameTag); await new NotificationDAO().Add(notify); } return(new AddCommentVM { Status = true, ListEmails = listEmail }); } catch (Exception) { return(new AddCommentVM { Status = false }); } }
public object ListComments(string kpilevelcode) { //Cat chuoi var value = kpilevelcode.ToSafetyString().Split(','); //Chuoi nhan tu client bao gom kpilevelcode, period, userid var code = value[0].Substring(0, value[0].Length - 1).ToSafetyString(); //KPILevelCode var period = value[0].Substring(value[0].Length - 1, 1).ToUpper().ToSafetyString(); var userid = value[1].ToInt(); //lay tat ca comment cua kpi var listcmts = _dbContext.Comments.Where(x => x.KPILevelCode == code && x.Period == period).ToList(); //Tong tat ca cac comment cua kpi var totalcomment = listcmts.Count(); //Tong cmt cua userid da xem kpi var totalseencomment = _dbContext.SeenComments.Where(x => x.UserID == userid).Count(); //Lay ra tat ca cac comment cua kpi(userid nao post comment len thi mac dinh userid do da xem comment cua chinh minh roi) var data = _dbContext.Comments.Where(x => x.KPILevelCode == code && x.Period == period) .Select(x => new CommentVM { CommentID = x.ID, UserID = x.UserID, CommentMsg = x.CommentMsg, KPILevelCode = x.KPILevelCode, CommentedDate = x.CommentedDate, FullName = _dbContext.Users.FirstOrDefault(a => a.ID == x.UserID).FullName, Period = x.Period, Read = true }) .OrderByDescending(x => x.CommentedDate) .ToList(); // if (totalcomment > totalseencomment) { //Neu userid nao ma chua xem comment thi hien thi la chua xem data = data.Select(x => new CommentVM { CommentID = x.CommentID, UserID = x.UserID, CommentMsg = x.CommentMsg, KPILevelCode = x.KPILevelCode, CommentedDate = x.CommentedDate, FullName = _dbContext.Users.FirstOrDefault(a => a.ID == x.UserID).FullName, Period = x.Period, //read=true la da xem, nguoc lai la chua xem Read = _dbContext.SeenComments.FirstOrDefault(a => a.UserID == userid && a.CommentID == x.CommentID) == null ? false : true }) .OrderByDescending(x => x.CommentedDate) .ToList(); foreach (var item in listcmts) { //Neu userid xem comment roi thi luu vao lich su la da xem if (_dbContext.SeenComments.FirstOrDefault(x => x.UserID == userid && x.CommentID == item.ID) == null) { var seencomment = new SeenComment() { CommentID = item.ID, UserID = userid }; _dbContext.SeenComments.Add(seencomment); _dbContext.SaveChanges(); } } } return(new { data, total = _dbContext.Comments.Where(x => x.KPILevelCode == code && x.Period == period).Count() }); }
public async Task <CommentForReturnViewModel> AddComment(AddCommentViewModel entity, int levelIDOfUserComment) { var listEmail = new List <string[]>(); var listTags = new List <Tag>(); var listFullNameTag = new List <string>(); var user = _dbContext.Users; var dataModel = _dbContext.Datas; string queryString = string.Empty; var LinkDedault = entity.DefaultLink; try { //add vao comment var tamp = new List <string>(); var message = string.Empty; foreach (var item in entity.Tag.Split(",")) { if (item == "") { tamp.Add(entity.CommentMsg); message = entity.CommentMsg; } else { var Msgs = user.FirstOrDefault(x => x.Username == item).Alias; tamp.Add("@" + Msgs); message = entity.CommentMsg.Split("@").First() + string.Join(" ", tamp); } } //string content = @"<p><b style='color:red'>" + string.Join(" ", tamp) + "</b></p>"; var comment = await CreateComment(new Comment { CommentMsg = message, //CommentMsg = entity.CommentMsg, DataID = entity.DataID, UserID = entity.UserID,//sender Link = entity.Link, Title = entity.Title }); //if (comment.ID > 0) //{ // var updateLink = await _dbContext.Comments.FirstOrDefaultAsync(x => x.ID == comment.ID); // if (!updateLink.Link.Contains("title")) // { // //Replace Remark become Action Plan // var title = Regex.Replace(comment.Title.Split('-')[0].ToSafetyString(), @"\s+", "-"); // updateLink.Link = updateLink.Link ; // await _dbContext.SaveChangesAsync(); // queryString = updateLink.Link; // } //} if (comment.ID > 0) { var updateLink = await _dbContext.Comments.FirstOrDefaultAsync(x => x.ID == comment.ID); if (!updateLink.Link.Contains("title")) { //Replace Remark become Action Plan var title = Regex.Replace(comment.Title.Split('-')[0].ToSafetyString(), @"\s+", "-"); var qrlink = updateLink.Link.Split('#').First(); var udlink = LinkDedault + $"/remark/{comment.ID}/{comment.DataID}/{title}"; updateLink.Link = udlink; await _dbContext.SaveChangesAsync(); queryString = qrlink + '#' + updateLink.Link; } } //B1: Xu ly viec gui thong bao den Owner khi nguoi gui cap cao hon comment //Tim levelNumber cua user comment var kpilevelIDResult = await _dbContext.KPILevels.FirstOrDefaultAsync(x => x.KPILevelCode == entity.KPILevelCode); var userIDResult = await _dbContext.Owners.FirstOrDefaultAsync(x => x.KPILevelID == kpilevelIDResult.ID && x.CategoryID == entity.CategoryID); var userModel = await _dbContext.Users.FindAsync(userIDResult.UserID); //Lay ra danh sach owner thuoc categoryID va KPILevelCode var owners = await _dbContext.Owners.Where(x => x.KPILevelID == kpilevelIDResult.ID && x.CategoryID == entity.CategoryID).ToListAsync(); //Neu nguoi comment ma la cap cao hon owner thi moi gui thong bao va gui mail cho owner if (await CheckLevelNumberOfUser(levelIDOfUserComment, userModel.LevelID)) { owners.ForEach(userItem => { //Add Tag gui thong bao den cac owner if (entity.UserID != userItem.ID) //Neu chinh owner do binh luan thi khong gui thong bao { var itemtag = new Tag(); itemtag = new Tag(); itemtag.UserID = userItem.ID; itemtag.CommentID = comment.ID; listTags.Add(itemtag); //Day la danh sach tag //Add vao list gui mail string[] arrayString = new string[5]; arrayString[0] = user.Find(entity.UserID).Alias; //Bi danh arrayString[1] = user.Find(entity.UserID).Email; arrayString[2] = comment.Link; arrayString[3] = comment.Title; arrayString[4] = comment.CommentMsg; listEmail.Add(arrayString); } }); //B2: Neu ma nguoi cap cao hon owner tag ai do vao comment cua ho thi se gui mail va thong bao den nguoi do if (!entity.Tag.IsNullOrEmpty()) { var result = await CreateTag(entity.Tag, entity.UserID, comment); listEmail = result.Item1; listFullNameTag = result.Item2; listTags = result.Item3; } } else //Neu user co level nho hon owner commnent thi gui den owner { //B1: Gui thong bao den cac owner owners.ForEach(x => { //Add vao Tag de gui thong if (entity.UserID != x.UserID) { var itemtag = new Tag(); itemtag = new Tag(); itemtag.UserID = x.UserID; itemtag.CommentID = comment.ID; listTags.Add(itemtag); //Day la danh sach tag } }); //B2: Neu tag ai thi gui thong bao den nguoi do if (!entity.Tag.IsNullOrEmpty()) { var result = await CreateTag(entity.Tag, entity.UserID, comment); listEmail = result.Item1; listFullNameTag = result.Item2; listTags = result.Item3; } } //Add vao seencomment var seenComment = new SeenComment(); seenComment.CommentID = comment.ID; seenComment.UserID = entity.UserID; seenComment.Status = true; _dbContext.Tags.AddRange(listTags); await _dbContext.SaveChangesAsync(); await CreateSeenComment(seenComment); if (listTags.Count > 0) { //Add vao Notification var notify = new Notification(); notify.CommentID = comment.ID; notify.Content = comment.CommentMsg; notify.UserID = entity.UserID; //sender notify.Title = comment.Title; notify.Link = comment.Link; notify.Action = "Comment"; notify.Tag = string.Join(",", listFullNameTag); await _notificationService.Add(notify); } return(new CommentForReturnViewModel { Status = true, ListEmails = listEmail, QueryString = queryString, NotifiUserID = listTags }); } catch (Exception ex) { return(new CommentForReturnViewModel { Status = false }); } }
public AddCommentVM AddComment(AddCommentViewModel entity) { var listEmail = new List <string[]>(); var listTags = new List <Tag>(); var listFullNameTag = new List <string>(); var itemTag = _dbContext.Tags; var user = _dbContext.Users; try { //add vao comment var comment = new Comment(); comment.CommentMsg = entity.CommentMsg; comment.DataID = entity.DataID; comment.UserID = entity.UserID; comment.Link = entity.Link; comment.Title = entity.Title; _dbContext.Comments.Add(comment); _dbContext.SaveChanges(); if (!entity.Tag.IsNullOrEmpty()) { if (entity.Tag.IndexOf(',') == -1) { var Username = entity.Tag.Trim(); var userItem = user.FirstOrDefault(x => x.Username == Username); if (userItem != null) { var itemtag = new Tag(); itemtag = new Tag(); itemtag.UserID = (int?)userItem.ID ?? 0; itemtag.CommentID = comment.ID; string[] arrayString = new string[5]; arrayString[0] = user.FirstOrDefault(x => x.ID == entity.UserID).FullName; arrayString[1] = userItem.Email; arrayString[2] = comment.Link; arrayString[3] = comment.Title; arrayString[4] = comment.CommentMsg; listFullNameTag.Add(userItem.FullName); listEmail.Add(arrayString); _dbContext.Tags.Add(itemtag); _dbContext.SaveChanges(); } } else { var list = entity.Tag.Split(','); var commentID = comment.ID; var listUserID = itemTag.Where(x => x.ActionPlanID == comment.ID).Select(x => x.UserID).ToList(); var listUsers = _dbContext.Users.Where(x => list.Contains(x.Username)).ToList(); foreach (var item in listUsers) { string[] arrayString = new string[5]; var tag = new Tag(); tag.CommentID = comment.ID; tag.UserID = item.ID; listTags.Add(tag); arrayString[0] = user.FirstOrDefault(x => x.ID == entity.UserID).FullName; arrayString[1] = item.Email; arrayString[2] = comment.Link; arrayString[3] = comment.Title; arrayString[4] = comment.CommentMsg; listFullNameTag.Add(item.FullName); listEmail.Add(arrayString); } _dbContext.Tags.AddRange(listTags); _dbContext.SaveChanges(); } } //Add vao Notification var notify = new Notification(); notify.CommentID = comment.ID; notify.Content = comment.CommentMsg; notify.UserID = comment.UserID; notify.Title = comment.Title; notify.Link = comment.Link; notify.Tag = string.Join(",", listFullNameTag); notify.Title = comment.Title; new NotificationDAO().Add(notify); //Add vao seencomment var seenComment = new SeenComment(); seenComment.CommentID = comment.ID; seenComment.UserID = entity.UserID; seenComment.Status = true; _dbContext.SeenComments.Add(seenComment); _dbContext.SaveChanges(); return(new AddCommentVM { Status = true, ListEmails = listEmail }); } catch (Exception) { return(new AddCommentVM { Status = false }); } }