Ejemplo n.º 1
0
        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();
        }
Ejemplo n.º 2
0
 public async Task <int> GetVoteAsync(int productId, int userId)
 {
     return((await ProductVotes.FindAsync(new object[] { productId, userId })).Vote);
 }
Ejemplo n.º 3
0
 public async Task <bool> IsVotedAsync(int productId, int userId)
 {
     return((await ProductVotes.FindAsync(new object[] { productId, userId })) != null);
 }