public async Task CreateSale(CreateSaleCommand command, CancellationToken token) { var products = _db.Products.Include(p => p.Inventory); var sale = _mapper.Map <Sale>(command); var items = _db.ItemsSold; //make deductions from the quantity of every product that has been sold foreach (var p in command.ProductsSold) { var product = await products.SingleOrDefaultAsync(x => x.Id == p.Id, token).ConfigureAwait(false); product.Quantity -= p.Quantity; if (product.Inventory != null) { product.Inventory.Quantity -= p.Quantity; } _db.Entry(product).State = EntityState.Modified; var item = new ItemSold { ItemName = p.ItemName, Quantity = p.Quantity, Price = p.Price, ProductId = product.Id }; await items.AddAsync(item, token).ConfigureAwait(false); } //if there are any discounts then you can calculate before persisting. sale.ItemsSold.AddRange(items !); await _set.AddAsync(sale, token).ConfigureAwait(false); }
/// <summary> /// Executes a single sided sale of a given amount of a given <see cref = "Item">Item</see> for a given price. /// </summary> /// <remarks> /// This is where the exchanged amount of Items will be <see cref = "ExchangeComponent.RemoveItem">removed</see> /// and the exchange will be billed for the previously calculated price. /// </remarks> /// <param name="item">The Item to sell.</param> /// <param name="price">The price the Item is sold for.</param> /// <param name="amount">The amount of Items being sold.</param> public void RegisterSale(Item item, float price, int amount = 1) { this.Log($"Exchange registered sale of {amount} {item} {price * amount} ({price})"); RemoveItem(item, amount); ItemSold?.Invoke(this, new ItemSoldEventArgs(item, price, amount)); }
public override object ConvertFromString(string text, IReaderRow row, MemberMapData memberMapData) { // [Item ID-Item Quantity-Item Price] if ((text ?? "").Length > 2) { if (text[0] == '[' && text.Last() == ']') { text = text.Substring(1, text.Length - 2).Trim(); if (!string.IsNullOrWhiteSpace(text)) { var itemsSold = text.Split(',').Select(part => { var parts = part.Split('-'); var itemSold = new ItemSold(); int id = 0; int quantity = 0; decimal price = 0; var parsedId = int.TryParse(parts[0], out id); var parsedQuantity = parts.Length > 1 ? int.TryParse(parts[1], out quantity) : false; var parsedPrice = parts.Length > 2 ? decimal.TryParse(parts[2], NumberStyles.Any, CultureInfo.InvariantCulture, out price) : false; if (parsedId) { itemSold.Id = id; } if (parsedQuantity) { itemSold.Quantity = quantity; } if (parsedPrice) { itemSold.Price = price; } return(itemSold); }).ToList(); return(itemsSold); } } } return(new List <ItemSold>()); }