public static async Task <ResultsItem> DeleteBBThread(int threadId, LocalAccount.PegaUser user) { try { using (PegasunDBContext db = new PegasunDBContext()) { BBThreads serviceThread = db.BBThreads.FirstOrDefault(x => x.ThreadId == threadId); if (serviceThread == null) { return(ResultsItem.Error("This thread was not found or has already been deleted.")); } if (serviceThread.UserId != user.UserId) { return(ResultsItem.Error("This thread belongs to another user.")); } db.Remove(serviceThread); await db.SaveChangesAsync(); return(ResultsItem.Success("Successfully deleted this thread. Please refresh the page to see changes.")); } } catch (Exception ex) { Utilities.LogException(new[] { $"User:{user.Username}" }, ex); return(ResultsItem.Error($"Unable to delete this thread. {ex.Message}")); } }
public static async Task <ResultsItem> VoteBBComment(long commentId, bool isUpvote, LocalAccount.PegaUser user) { try { string message = "Successfully updated your vote. It will be reflected shortly."; using (PegasunDBContext db = new PegasunDBContext()) { var getServiceCommentTask = db.BBComments.FirstOrDefaultAsync(x => x.CommentId == commentId); var getExistingVoteTask = db.BBCommentVotes.FirstOrDefaultAsync(x => x.CommentId == commentId && x.UserId == user.UserId); var serviceComment = await getServiceCommentTask; var existingVote = await getExistingVoteTask; if (serviceComment == null) { return(ResultsItem.Error("This comment does not exist.")); } if (existingVote != null) { if (existingVote.IsUpvote == isUpvote) { db.Remove(existingVote); message = "Successfully removed your vote. It will be reflected shortly."; } // Same vote (e.g. clicked on upvote twice), which means they are re-tracing their vote. else { existingVote.IsUpvote = isUpvote; } } else { var newServiceVote = new BBCommentVotes { CommentId = commentId, IsUpvote = isUpvote, UserId = user.UserId }; db.BBCommentVotes.Add(newServiceVote); } db.SaveChanges(); return(ResultsItem.Success(message)); } } catch (Exception ex) { Utilities.LogException(new[] { $"User:{user.Username}", $"CommentId:{commentId}" }, ex); return(ResultsItem.Error($"Unable to submit your vote. {ex.Message}")); } }