public async Task <bool> CommentVoting(CommentVoting commentVoting) { try { var upateCommentVoting = await db.CommentVotings .FirstOrDefaultAsync(d => d.UserId == commentVoting.UserId && d.CommentId == commentVoting.CommentId ); if (upateCommentVoting != null) { commentVoting.UpdatedDate = DateTime.Now; upateCommentVoting.VotingType = commentVoting.VotingType; await db.SaveChangesAsync(); return(true); } commentVoting.CreatedDate = DateTime.Now; db.CommentVotings.Add(commentVoting); await db.SaveChangesAsync(); return(true); } catch (Exception ex) { return(false); } }
private static async Task CalculateSentimentAsync(FeedbackDbContext dbContext) { // NOTE: Cognetive services accepts up to 1000 "documents" at a time. var feedbackBatch = dbContext.Feedbacks.Where(f => !f.SentimentScore.HasValue).Take(1000).ToList(); if (feedbackBatch.Any()) { Console.WriteLine("Found feedback to analyze..."); var documents = feedbackBatch.Select(f => new TextDocument(f.Id, f.Content)); var request = new TextAnalysisRequest(documents); var response = await TextAnalyzer.CalculateSentimentAsync(request); foreach (var sentiment in response.Documents) { var feedback = feedbackBatch.Single(f => f.Id == sentiment.Id); feedback.SentimentScore = sentiment.Score; } await dbContext.SaveChangesAsync(); await CalculateSentimentAsync(dbContext); // Recursively process until we are done. } else { Console.WriteLine("Did not find feedback to analyze."); } }
public async Task <FeedbackEntry> AddFeedback(IFeedback feedback) { var playerObject = await _context.Players.FindAsync(feedback.PlayerID); if (playerObject == null) { throw new InvalidPlayerIDException(); } var gameSessionObject = await _context.GameSessions.FindAsync(feedback.GameSessionID); if (gameSessionObject == null) { throw new InvalidGameSessionException(); } var priorFeedback = await _context.Feedback.Where(x => x.GameSessionID == feedback.GameSessionID && x.PlayerID == feedback.PlayerID).FirstOrDefaultAsync(); if (priorFeedback != null) { throw new DuplicateFeedbackException(priorFeedback.SubmissionDate); } var gameObject = await _context.Games.Where(x => x.GameID == gameSessionObject.GameID).FirstOrDefaultAsync(); _context.Feedback.Add(feedback as Feedback); await _context.SaveChangesAsync(); return(new FeedbackEntry { Feedback = feedback as Feedback, Gamer = playerObject, GameSession = gameSessionObject, Game = gameObject }); }