コード例 #1
0
        public async Task <ActionResult <RateableProductViewModel> > RateProductAsync(
            [FromRoute] int productId,
            [FromBody] int productRate,
            CancellationToken cancellationToken)
        {
            Product product = await productRepository.GetProductByIdAsync(productId);

            if (product is null)
            {
                return(NotFound());
            }

            IEnumerable <ProductRate> productRates = await productRepository.GetProductRatesByIdAsync(productId);

            product.ProductRates = (ICollection <ProductRate>)productRates;

            productRepository.RateProduct(product, productRate);

            productRepository.UpdateAvgRate(product, productRates);

            await dbContext.SaveChangesAsync(cancellationToken);

            return(new RateableProductViewModel
            {
                Name = product.Name,
                Description = product.Description,
                Price = product.Price,
                Category = product.Category,
                // We are going to test it as a single user, so it will do the trick
                CurrentRate = productRate,
                AverageRate = product.AverageRate,
                MinRate = product.RateRange.MinRate,
                MaxRate = product.RateRange.MaxRate,
            });
        }
コード例 #2
0
        public async Task InsertRankingAsync(Ranking entry)
        {
            await _db.AddAsync(entry);

            await _db.SaveChangesAsync();
        }