public async Task <ShopItemFeedback> Post(ShopItemFeedback shopItemFeedback) { if (ModelState.IsValid) { await shopItemFeedbacksDatabaseService.Add(shopItemFeedback); return(shopItemFeedback); } throw new Exception(); }
public async Task Add(ShopItemFeedback shopItemFeedback) { shopItemFeedback.Date = DateTime.Now; var dbContext = new LMDbContext(); await dbContext.ShopItemFeedbacks.AddAsync(shopItemFeedback); await dbContext.SaveChangesAsync(); shopItemFeedback.ShopItem = dbContext.ShopItems.FirstOrDefault(s => s.Id == shopItemFeedback.ShopItemId); shopItemFeedback.User = dbContext.Users.FirstOrDefault(a => a.Id == shopItemFeedback.UserId); var feedbacks = dbContext.ShopItemFeedbacks.Where(sh => sh.ShopItemId == shopItemFeedback.ShopItemId); shopItemFeedback.ShopItem.Rating = feedbacks.Sum(a => a.Rating) / feedbacks.Count(); await dbContext.SaveChangesAsync(); }