Пример #1
0
 public void UpdateItems(ICollection <ItemDto> items)
 {
     foreach (var item in items)
     {
         var itemEntity = _mapper.Map <Item>(item);
         _ctx.Entry(itemEntity).State = System.Data.Entity.EntityState.Modified;
     }
     _ctx.SaveChanges();
 }
Пример #2
0
        public void RecordTransaction(TransactionDto transaction)
        {
            // get the transaction entity
            var transac          = _mapper.Map <Transaction>(transaction);
            var uniqueDictionary = new Dictionary <int, ItemDto>();
            var itemsList        = transaction.Items.ToList();

            foreach (var itemDto in itemsList)
            {
                if (itemDto.ItemId < 0)
                {
                    var thisItem = _ctx.StockItems.FirstOrDefault(i => i.ItemName == itemDto.ItemName);
                    if (thisItem == null)
                    {
                        throw new Exception("This is a real problem here");
                    }
                    itemDto.ItemId = thisItem.ItemId;
                }

                if (uniqueDictionary.ContainsKey(itemDto.ItemId))
                {
                    uniqueDictionary[itemDto.ItemId].Quantity += itemDto.Quantity;
                    continue;
                }
                uniqueDictionary.Add(itemDto.ItemId, itemDto);
            }

            Console.Write('s');
            foreach (var item in uniqueDictionary.Values.ToList())
            {
                var transactionItem = new TransactionItem {
                    Transaction = transac
                };
                // attempt to look for item in db
                var itemToAdd = _ctx.StockItems.FirstOrDefault(i => i.ItemName == item.ItemName);
                // if it doesn't exist
                if (itemToAdd == null)
                {
                    var newItem = _mapper.Map <Item>(item);
                    switch (transaction.TransactionType)
                    {
                    // if sale, set custom quantity
                    case 1:
                        newItem.Quantity     = 0;
                        transactionItem.Item = newItem;
                        break;

                    // if purchase, add as is
                    case 0:
                        transactionItem.Item = newItem;
                        break;

                    default:
                        throw new KeyNotFoundException("invalid transaction type");
                    }

                    _ctx.TransactionItems.Add(transactionItem);

                    // if it does exist, update its quantity
                }
                else if (transaction.TransactionType == 1)
                {
                    // if sale, subtract quantity from items table,leave all others intact
                    // TO-DO: find a better way to do this, maybe a partial model that updates
                    // only the properties i want updated
                    itemToAdd.Quantity         -= item.Quantity;
                    _ctx.Entry(itemToAdd).State = EntityState.Modified;
                    // do i need these?
                    _ctx.Entry(itemToAdd).Property(x => x.Unit).IsModified         = false;
                    _ctx.Entry(itemToAdd).Property(x => x.SellingPrice).IsModified = false;
                    _ctx.Entry(itemToAdd).Property(x => x.PurchaseCost).IsModified = false;

                    transactionItem.Item = itemToAdd;

                    // update transasctionItem's other fields: quantity and amount
                    transactionItem.Quantity = item.Quantity;
                    transactionItem.Amount   = item.Quantity * item.SellingPrice;

                    _ctx.TransactionItems.Add(transactionItem);
                }
                else
                {
                    // if purchase, add quantity, update purchase field, leave all others intact
                    itemToAdd.Quantity         += item.Quantity;
                    itemToAdd.PurchaseCost     += item.PurchaseCost;
                    _ctx.Entry(itemToAdd).State = EntityState.Modified;
                    _ctx.Entry(itemToAdd).Property(x => x.Unit).IsModified         = false;
                    _ctx.Entry(itemToAdd).Property(x => x.SellingPrice).IsModified = false;
                    _ctx.Entry(itemToAdd).Property(x => x.PurchaseCost).IsModified = false;

                    transactionItem.Item = itemToAdd;

                    // update transasctionItem's other fields: quantity and amount
                    transactionItem.Quantity = item.Quantity;
                    transactionItem.Amount   = item.Quantity * item.PurchaseCost;

                    _ctx.TransactionItems.Add(transactionItem);
                }
            }

            _ctx.Transactions.Add(transac);
            _ctx.SaveChanges();
        }
Пример #3
0
 public void UpdateUser(UserDto user)
 {
     _ctx.Users.Find(user.UserId);
     _ctx.Entry(_mapper.Map <User>(user)).State = System.Data.Entity.EntityState.Modified;
     _ctx.SaveChanges();
 }