public ActionResult GetRates(long id) { var product = _productService.GetProduct(id); if (product == null) { return(NotFound()); } List <ProductRatingVM> result = new List <ProductRatingVM>(); foreach (var orderDetail in product.OrderDetails) { if (orderDetail.Star != null) { ProductRatingVM item = orderDetail.Adapt <ProductRatingVM>(); item.Rate = orderDetail.Star.Value; item.FullName = orderDetail.Order.MyUser.FullName; result.Add(item); } } if (result.Count > 0) { var rate = result.Sum(_ => _.Rate) / result.Count; var votes = result.Count; result.Add(new ProductRatingVM { Rate = rate, Comment = votes + " votes" }); } return(Ok(result)); }
public async Task <ActionResult <ProductRatingVM> > Post(ProductRatingVM ratingVM) { if (!ModelState.IsValid) { return(BadRequest()); } var data = repository.Add(ratingVM); return(CreatedAtAction(nameof(data), new { id = data.ID }, data)); }
public async Task <ActionResult <ProductRatingVM> > Put(ProductRatingVM ratingVM) { if (!ModelState.IsValid) { return(BadRequest()); } else { var mainData = repository.Get(ratingVM.ID); if (mainData == null) { return(NotFound()); } var data = repository.Update(ratingVM); return(data); } }