public async Task ChangeVoteAsync(int productId, int userId, int newVote) { if (newVote < 0 || newVote > 5) { return; } var product = Products.Find(productId); var vote = await ProductVotes.FindAsync(new object[] { productId, userId }); if (vote == null) { await ProductVotes.AddAsync(new ProductVote() { ProductId = productId, UserId = userId, Vote = newVote }); var sum = product.Rating * product.CountOfVotes++; product.Rating = (sum + newVote) / product.CountOfVotes; } else { var sum = product.Rating * product.CountOfVotes - vote.Vote; vote.Vote = newVote; product.Rating = (sum + newVote) / product.CountOfVotes; } await SaveChangesAsync(); }
public async Task <int> GetVoteAsync(int productId, int userId) { return((await ProductVotes.FindAsync(new object[] { productId, userId })).Vote); }
public async Task <bool> IsVotedAsync(int productId, int userId) { return((await ProductVotes.FindAsync(new object[] { productId, userId })) != null); }