public void AddUser(UserDto userDto) { if (userDto == null) { throw new ArgumentNullException(nameof(userDto)); } var user = _mapper.Map <User>(userDto); _ctx.Users.Add(user); _ctx.SaveChanges(); }
// consider removing this public void AddItem(ItemDto item) { var itemToAdd = _mapper.Map <Item>(item); itemToAdd.Deleted = 0; itemToAdd.DeleteDate = null; _ctx.StockItems.Add(itemToAdd); try { _ctx.SaveChanges(); } catch (DbEntityValidationException e) { Console.WriteLine(e); } }
public void AddCompany(CompanyDto company) { if (company == null) { throw new ArgumentNullException(nameof(company)); } _ctx.Companies.Add(_mapper.Map <Company>(company)); _ctx.SaveChanges(); }
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(); }