public bool Create(ItemViewModel model) { try { var item = ViewToEntity(model); _itemDal.Insert(item); } catch { return(false); } return(true); }
/// <summary> /// Add any new product. Automatically determines the type of item and insert accordingly. /// </summary> /// <param name="product"></param> public void AddProduct(Product product) { using (var scope = new TransactionScope()) { using (var connection = Connector.GetConnection()) { var productDal = new ProductDal(connection); productDal.Insert(product is Item ? ((Item)product).Model : product.Name, product.ProductType); var id = productDal.GetLastInsertId(); product.Id = id; if (product.Id == null) { throw new ArgumentNullException(nameof(product.Id), "Product Id is null"); } if (product is Item) { var item = (Item)product; var itemDal = new ItemDal(connection); itemDal.Insert(id, item.StockQty, item.UnitPrice); if (item is Alloywheel) { var alloywheel = (Alloywheel)item; var alloywheelDal = new AlloywheelDal(connection); alloywheelDal.Insert(id, alloywheel.Brand, alloywheel.Dimension); } if (item is Tyre) { var tyre = (Tyre)item; var tyreDal = new TyreDal(connection); tyreDal.Insert(id, tyre.Brand, tyre.Dimension, tyre.Country); } if (item is Battery) { var battery = (Battery)item; var batteryDal = new BatteryDal(connection); batteryDal.Insert(id, battery.Brand, battery.Capacity, battery.Voltage); } } } scope.Complete(); } }
public void ChooseActionForItems() { ItemDal itemDal = new ItemDal(); Console.Clear(); Console.WriteLine("TABLE: ITEMS\n\n"); Console.WriteLine("What action you want to choose?\n" + "1. View all items\n" + "2. Get item by Id\n" + "3. Get item by name of column\n" + "4. Add item\n" + "5. Edit info about item\n" + "6. Delete item by Id\n" + "7. Delete item by name of column\n" + "8. Back to start menu"); Console.Write("\nYour selection: "); string ch = Console.ReadLine(); switch (ch) { case "1": { try { Console.Clear(); Console.WriteLine("TABLE: ITEMS\n\n"); itemDal.PrintListOfItems(itemDal.GetAll()); Console.ReadKey(); break; } finally { Menu menu = new Menu(); menu.ChooseActionForItems(); } } case "2": { try { Console.Clear(); Console.WriteLine("TABLE: ITEMS\n\nId: "); int id = Convert.ToInt32(Console.ReadLine()); itemDal.PrintItem(itemDal.GetById(id)); Console.ReadKey(); break; } finally { Menu menu = new Menu(); menu.ChooseActionForItems(); } } case "3": { try { Console.Clear(); Console.WriteLine("TABLE: ITEMS\n\nName of column: "); string fieldName = Console.ReadLine(); Console.WriteLine("\nValue: "); string text = Console.ReadLine(); itemDal.PrintListOfItems(itemDal.GetByFieldName(fieldName, text)); Console.ReadKey(); break; } finally { Menu menu = new Menu(); menu.ChooseActionForItems(); } } case "4": { try { Console.Clear(); Console.WriteLine("TABLE: ITEMS\n\n"); Console.WriteLine("NAME: "); string name = Console.ReadLine(); Console.WriteLine("DESCRIPTION: "); string desc = Console.ReadLine(); Console.WriteLine("CATEGORY_ID: "); int categId = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("PRICE: "); decimal price = Convert.ToDecimal(Console.ReadLine()); Console.WriteLine("IN_STOCK: "); string inStock = Console.ReadLine(); Item item = new Item(name, desc, categId, price, inStock); itemDal.Insert(item); Console.WriteLine("Item succesfully inserted :)"); Console.ReadKey(); break; } finally { Menu menu = new Menu(); menu.ChooseActionForItems(); } } case "5": { try { Console.Clear(); Console.WriteLine("TABLE: ITEMS\n\n"); Console.WriteLine("Name of column (set): "); string fieldName = Console.ReadLine(); Console.WriteLine("Value (set): "); string text = Console.ReadLine(); Console.WriteLine("Name of column (condition): "); string fieldCondition = Console.ReadLine(); Console.WriteLine("Value (condition): "); string textCondition = Console.ReadLine(); itemDal.UpdateByFieldName(fieldName, text, fieldCondition, textCondition); Console.WriteLine("Item succesfully updated :)"); Console.ReadKey(); break; } finally { Menu menu = new Menu(); menu.ChooseActionForItems(); } } case "6": { try { Console.Clear(); Console.WriteLine("TABLE: ITEMS\n\nId: "); int id = Convert.ToInt32(Console.ReadLine()); itemDal.DeleteById(id); Console.WriteLine("Item succesfully deleted :)"); Console.ReadKey(); break; } finally { Menu menu = new Menu(); menu.ChooseActionForItems(); } } case "7": { try { Console.Clear(); Console.WriteLine("TABLE: ITEMS\n\n "); Console.WriteLine("Name of column (condition): "); string fieldCondition = Console.ReadLine(); Console.WriteLine("Value (condition): "); string textCondition = Console.ReadLine(); itemDal.DeleteByFieldName(fieldCondition, textCondition); Console.WriteLine("Item succesfully deleted :)"); Console.ReadKey(); break; } finally { Menu menu = new Menu(); menu.ChooseActionForItems(); } } case "8": { Menu menu = new Menu(); menu.ChooseTable(); break; } default: Console.WriteLine("Invalid selection. Please select 1, 2, 3, 4, 5, 6, 7 or 8."); break; } }