public async Task <bool> CreateComment(CommentCreateModel model, string userName, string link, int?commentId) { var userProfile = await context .Profiles .Include(u => u.User) .FirstOrDefaultAsync(p => p.User.UserName == userName); var news = await context .News .FirstOrDefaultAsync(n => n.Id == model.NewsId); if (news != null) { await statisticNewsService.SetState(model.NewsId, userName, "view", string.Empty); var now = DateTime.Now; await context.Comments.AddAsync(new Data.dbModels.Comment { News = news, DateComment = now, Text = model.TextComment, Owner = userProfile, UserNameTo = model.UsernameAddresTo, CommentIdTo = model.CommentIdTo }); if (!string.IsNullOrEmpty(model.UsernameAddresTo)) { var profileFrom = userProfile.Id; var userFromName = userProfile.User.UserName; var userToId = (await context .Users .FirstOrDefaultAsync(u => u.UserName == model.UsernameAddresTo)) ?.Id; var profileTo = await context.Profiles.FirstOrDefaultAsync(p => p.UserId == userToId); if (profileTo != null) { await SetNotificationAsync(profileTo, profileFrom, link, userFromName, commentId); } } await context.SaveChangesAsync(); return(true); } return(false); }
public async Task <ActionResult> GetNewsById(int newsId) { var news = await newsService.GetNewsById(newsId); if (news != null) { var username = User.GetUserName(); if (username != null) { await statisticNewsService.SetState(newsId, username, "view", string.Empty); } return(Ok(news)); } return(NotFound()); }