public async Task InsertCustomerAsync(Customer customerToInsert) { try { _dbContext.Customers.Add(customerToInsert); await _dbContext.SaveChangesAsync(); //await _dbContext.DisposeAsync(); } catch (Exception e) { Console.WriteLine(e.Message); } }
/// <summary> /// Increases Products Quantity in Product table /// </summary> /// <param name="inventoryItemToCreate"></param> /// <returns></returns> public async Task CreateProductAsync(InventoryItem inventoryItemToCreate) { try { try { _dbContext.Inventory.Add(inventoryItemToCreate); await _dbContext.SaveChangesAsync(); } catch (Exception e) { Console.WriteLine(e.Message); } } catch (Exception) { throw; } }
/// <summary> /// Creates order from customer /// </summary> /// <param name="order"></param> /// <returns></returns> public async Task CreateOrder(Order order) { try { // adds the order including all the orderlineitems _dbContext.Add(order); // gets order var storeId = order.StoreId; var userId = order.CustomerId; foreach (var o in order.OrderLineItems) { var id = storeId; var pid = o.InventoryItemId; var inventoryItems = _dbContext.Inventory .Include(i => i.Product) .Include(i => i.Store) .AsNoTracking() .Where(i => i.StoreId == storeId && i.ProductId == o.InventoryItemId).FirstOrDefault(); var newQuantity = inventoryItems.Quantity - o.Quantity; var inventoryToUpdate = new InventoryItem { Id = inventoryItems.Id, ProductId = pid, StoreId = storeId, Quantity = newQuantity, ChangedDate = DateTimeOffset.Now.LocalDateTime, LoggedUserId = userId }; _dbContext.Update(inventoryToUpdate); } await _dbContext.SaveChangesAsync(); } catch (Exception e) { Console.WriteLine(e.Message); } }