public IHttpActionResult SubmitStrategyVote(VoteModel model) { string userId = GetAuthUser().Id; var strategy = _dbContext.Strategies.Find(model.id); StrategyVote vote = strategy.Votes.FirstOrDefault(v => v.UserId == userId); if (vote == null) { vote = new StrategyVote() { UserId = userId, Vote = model.direction }; strategy.Votes.Add(vote); } else { if (vote.Vote == model.direction) { strategy.Votes.Remove(vote); vote = null; } else { vote.Vote = model.direction; } } try { _dbContext.SaveChanges(); } catch (DbUpdateException e) { System.Diagnostics.Trace.TraceError("Vote user strategy error: " + e.Message); return(BadRequest("Something went wrong...")); } return(Ok(vote)); }
public async Task <IActionResult> SubmitStrategyVote(VoteModel model) { ApplicationUser user = await GetAuthUser(); StrategyVote vote = await _dbContext.StrategyVotes.FindAsync(model.id, user.Id); if (vote == null) { vote = new StrategyVote() { StratId = model.id, UserId = user.Id, Vote = model.direction }; _dbContext.StrategyVotes.Add(vote); } else { if (vote.Vote == model.direction) { _dbContext.StrategyVotes.Remove(vote); vote = null; } else { vote.Vote = model.direction; } } try { await _dbContext.SaveChangesAsync(); } catch (DbUpdateException e) { System.Diagnostics.Trace.TraceError("Vote user strategy error: " + e.Message); return(BadRequest("Something went wrong...")); } return(Ok(vote)); }