public async Task <ActionResult <Product> > DeleteProduct(int id) { var product = await dbContext.Products.FindAsync(id); if (product == null) { return(NotFound()); } dbContext.Products.Remove(product); await dbContext.SaveChangesAsync(); return(product); }
public async Task <ActionResult <Order> > CreateOrder([FromBody] Order order) { order.CreatedAt = DateTime.Now; var user = dbContext.Users.Where(x => x.Id == order.UserId).First(); foreach (var orderProduct in order.OrderProducts) { var orderProductFromDb = dbContext.Products.FirstOrDefault(x => x.Name == orderProduct.Product.Name); if (orderProductFromDb != null) { orderProduct.Product = orderProductFromDb; orderProduct.ProductId = orderProductFromDb.Id; orderProduct.Order = order; orderProduct.OrderId = order.Id; } user.OrderProducts.Add(orderProduct); } //Exchange Rates API CurrencyRateDto jsonResult = GetExchangeRates(); if (user.CurrencyCode != "BGN") { foreach (var rate in jsonResult.Rates) { if (rate.Key == user.CurrencyCode) { order.TotalPrice = Math.Round(order.OrderProducts.Sum(x => x.Product.Price * rate.Value), 2); } } } else { order.TotalPrice = order.OrderProducts.Sum(x => x.Product.Price); } order.User = user; dbContext.Orders.Add(order); await dbContext.SaveChangesAsync(); return(CreatedAtAction("CreateOrder", new { id = order.Id }, order)); }