public void ImportArticlesData(List <Article> articles) { if (articles.Count > 0) { _warehouseDbContext.Articles.AddRange(articles); _warehouseDbContext.SaveChanges(); } }
public int AddNewProduct(Product product) { _warehouseDbContext.Products.Add(product); var response = _warehouseDbContext.SaveChanges(); return(product.ProductId); }
private void Save() { WarehouseDbContext ctx = new WarehouseDbContext(); if (!IsNew) { if (SelectedProduct == null) { MessageBox.Show(CurrentWindows, "Please select a user before editting."); return; } var cat = ctx.Categories.SingleOrDefault(x => x.Name == SelectedCategory); if (cat == null) { cat = ctx.Categories.Add(new Category { Name = SelectedCategory }); ctx.SaveChanges(); } var product = ctx.Products.Single(u => u.ID == SelectedProduct.ID); product.Name = SelectedProduct.Name; product.Category = cat; product.Price = SelectedProduct.Price; product.Description = SelectedProduct.Description; product.Image = SelectedProduct.Image; ctx.SaveChanges(); } else { var cat = ctx.Categories.SingleOrDefault(x => x.Name == SelectedCategory); if (cat == null) { cat = ctx.Categories.Add(new Category { Name = SelectedCategory }); ctx.SaveChanges(); } SelectedProduct.Category = cat; ctx.Products.Add(SelectedProduct); ctx.SaveChanges(); } //Reload data from database ProductList = new List <Product>(ctx.Products.ToList()); Categories = new List <Category>(ctx.Categories.ToList()); IsNew = false; MessageBox.Show(CurrentWindows, "Updated successfully !"); }
public async Task Handle(AddStockEvent message, CancellationToken cancellationToken) { var stockForDate = await _mediator.Send(new GetWarehouseStock() { WarehouseId = message.WarehouseId, CheckDate = message.EventDate }); //check if we can export this amount of product if (message.EventType == EventTypes.Export) { var product = stockForDate.CurrentStock.SingleOrDefault(x => x.Product.Id == message.ProductId); if (product == null || product.Amount < message.ProductAmount) { throw new CannotExportStockException(); } } else if (message.EventType == EventTypes.Import) { //check if the warehouse accepts hazardous/non-hazardous products and if we have space for import var product = _dbContext.Products.Single(x => x.Id == message.ProductId); var warehouse = _dbContext.Warehouses.Single(x => x.Id == message.WarehouseId); if (product.IsHazardous != warehouse.HazardousProducts || stockForDate.FreeSpace < product.UnitSize * message.ProductAmount) { throw new CannotImportStockException(); } } var entity = _mapper.Map <DataAccess.Entities.StockEvent>(message); _dbContext.StockEvents.Add(entity); _dbContext.SaveChanges(); //we need to fix or remove snapshots that are no longer valid because of import/export //in the past - this could be done e.g. in a queue - or there could be such a requirement //in the system that imports/exports cannot be added after a snapshot has been made //(meaning that the user can only add them up to some time in the past, and e.g. //cannot add imports/exports to previous weeks, months, etc. but only to the current one) var snapshots = _dbContext.StockSnapshots.Where(x => x.WarehouseId == message.WarehouseId && x.SnapshotDate > message.EventDate) .Include(x => x.Products) .ToList(); if (snapshots.Any()) { //let's remove the invalid snapshots for simplicity of this project _dbContext.StockSnapshots.RemoveRange(snapshots); _dbContext.SaveChanges(); } }
private void Delete() { if (SelectedOrder == null) { MessageBox.Show(CurrentWindows, "Please select on order before deleting!"); return; } MessageBoxResult result = MessageBox.Show(CurrentWindows, "Are you sure you want to delete:\n" , "Confirm delete", MessageBoxButton.OKCancel, MessageBoxImage.Warning); if (result == MessageBoxResult.OK) { try { WarehouseDbContext ctx = new WarehouseDbContext(); var order = ctx.Orders.Single(u => u.ID == SelectedOrder.ID); ctx.Orders.Remove(order); ctx.SaveChanges(); OrderList = new List <Order>(ctx.Orders.ToList()); } catch (SqlException ex) { MessageBox.Show(CurrentWindows, ex.Message, "Database error", MessageBoxButton.OK, MessageBoxImage.Error); } catch (DbUpdateException ex) { MessageBox.Show(CurrentWindows, "Please delete the products before deleting the order."); } } }
public void CreateOrder(Order order) { order.OrderPlaced = DateTime.Now; var shoppingCartItems = _shoppingCart.ShoppingCartItems; order.OrderTotal = (int)_shoppingCart.GetShoppingCartTotal(); order.OrderDetails = new List <OrderDetail>(); //adding the order with its details foreach (var shoppingCartItem in shoppingCartItems) { var orderDetail = new OrderDetail { Amount = shoppingCartItem.Amount, ItemId = shoppingCartItem.Inventory.ItemId, Price = shoppingCartItem.Inventory.Price }; order.OrderDetails.Add(orderDetail); } _warehouseDbContext.Orders.Add(order); _warehouseDbContext.SaveChanges(); }
public ActionResult Create(CreateTankModel model) { if (ModelState.IsValid) { var db = new WarehouseDbContext(); var creatorId = this.User.Identity.GetUserId(); var tank = new Tank(model.Name, model.TankNumber, model.MaxCapacity); tank.TankCreatorId = creatorId; if (model.CurrentFertilizer == null) { tank.CurrentFertilizer = null; } else { tank.CurrentFertilizer = model.CurrentFertilizer.ToLower(); } db.Tanks.Add(tank); db.SaveChanges(); return(RedirectToAction("All", "Tanks")); } return(View(model)); }
public ActionResult Create(CreateSiloModel model) { if (ModelState.IsValid) { var db = new WarehouseDbContext(); var creatorId = this.User.Identity.GetUserId(); var silo = new Silo(model.Name, model.MaxCapacity, model.SiloNumber); silo.SiloCreatorId = creatorId; if (model.CurrentCommodity == null) { silo.CurrentCommodity = null; } else { silo.CurrentCommodity = model.CurrentCommodity; } db.Silos.Add(silo); db.SaveChanges(); return(RedirectToAction("Index", "Home")); } return(View(model)); }
public ActionResult Edit(EditSiloModel model) { if (ModelState.IsValid) { using (var db = new WarehouseDbContext()) { var silo = db.Silos.Find(model.Id); if (silo == null) { return(HttpNotFound()); } silo.Name = model.Name; silo.SiloNumber = model.Number; silo.MaxCapacity = model.MaxCapacity; db.SaveChanges(); } return(RedirectToAction("AllSilos")); } return(View(model)); }
public ActionResult Edit(EditTankModel model) { if (ModelState.IsValid) { using (var db = new WarehouseDbContext()) { var tank = db.Tanks.Find(model.Id); if (tank == null) { return(HttpNotFound()); } tank.Name = model.Name; tank.Number = model.Number; tank.MaxCapacity = model.MaxCapacity; db.SaveChanges(); } return(RedirectToAction("AllTanks")); } return(View(model)); }
private void Delete() { if (SelectedUser == null) { MessageBox.Show(CurrentWindows, "Please select a user before deleting."); return; } MessageBoxResult result = MessageBox.Show(CurrentWindows, "Are you sure you want to delete:\n" + SelectedUser.Name, "Confirm delete", MessageBoxButton.OKCancel, MessageBoxImage.Warning); if (result == MessageBoxResult.OK) { try { WarehouseDbContext ctx = new WarehouseDbContext(); var user = ctx.Users.Single(u => u.ID == SelectedUser.ID); ctx.Users.Remove(user); ctx.SaveChanges(); //Reload data from databse UserList = new List <User>(ctx.Users.ToList()); } catch (SqlException ex) { MessageBox.Show(CurrentWindows, ex.Message, "Database error", MessageBoxButton.OK, MessageBoxImage.Error); } } }
private void Delete() { if (SelectedDetail == null) { MessageBox.Show(CurrentWindows, "Please select a user before deleting."); return; } MessageBoxResult result = MessageBox.Show(CurrentWindows, "Are you sure you want to delete:\n" + SelectedDetail.ProductId, "Confirm delete", MessageBoxButton.OKCancel, MessageBoxImage.Warning); if (result == MessageBoxResult.OK) { try { if (Order.ID != 0) { WarehouseDbContext ctx = new WarehouseDbContext(); var detail = ctx.Details.Single(u => u.ID == SelectedDetail.ID); ctx.Details.Remove(detail); ctx.SaveChanges(); } Details.Remove(SelectedDetail); } catch (SqlException ex) { MessageBox.Show(CurrentWindows, ex.Message, "Database error", MessageBoxButton.OK, MessageBoxImage.Error); } } }
public Task <Product> Handle(CreateProduct request, CancellationToken cancellationToken) { var entity = _mapper.Map <DataAccess.Entities.Product>(request); _dbContext.Products.Add(entity); _dbContext.SaveChanges(); var product = _mapper.Map <Product>(entity); return(Task.FromResult(product)); }
public bool RemoveProduct(int ProductId) { using (WarehouseDbContext _db = new WarehouseDbContext()) { var _warehouseItem = _db.Warehouse.Where(x => x.ProductId == ProductId).FirstOrDefault(); if (_warehouseItem != null) { _db.Warehouse.Remove(_warehouseItem); } return(_db.SaveChanges() > 0); } }
public void AddToCart(Inventory inventory, int amount) { var shoppingCartItem = _warehouseDbContext.ShoppingCartItems.SingleOrDefault( s => s.Inventory.ItemId == inventory.ItemId && s.ShoppingCartId == ShoppingCartId); if (shoppingCartItem == null) { shoppingCartItem = new ShoppingCartItem { ShoppingCartId = ShoppingCartId, Inventory = inventory, Amount = 1 }; _warehouseDbContext.ShoppingCartItems.Add(shoppingCartItem); } else { shoppingCartItem.Amount++; } _warehouseDbContext.SaveChanges(); }
public ActionResult AddFertilizer(TankViewModel model) { if (ModelState.IsValid) { var fertilizer = new Fertilizer { Name = model.Fertilizer.Name, Amount = model.Fertilizer.Amount }; var operatorId = User.Identity.GetUserId(); var db = new WarehouseDbContext(); var operatorData = db.Users.Where(o => o.Id == operatorId).FirstOrDefault(); var tank = db.Tanks.Find(model.Id); try { tank.AddFertilizer(fertilizer, operatorData.FullName); db.SaveChanges(); return(RedirectToAction("All", "Tanks")); } catch (Exception ex) { ModelState.AddModelError("", $"{ex.Message}"); var tankOther = db.Tanks .Where(t => t.Id == model.Id) .Select(t => new TankViewModel { Number = t.Number, Name = t.Name, CurrentLoad = t.CurrentLoad, MaxCapacity = t.MaxCapacity, CapacityLeft = t.MaxCapacity - t.CurrentLoad, Fertilizer = new Fertilizer(), CurrentFertilizer = t.CurrentFertilizer }) .FirstOrDefault(); return(View(tankOther)); } } return(View(model)); }
public ActionResult AddCommodity(SiloViewModel model) { if (ModelState.IsValid) { var commodity = new Commodity { Name = model.Commodity.Name, Amount = model.Commodity.Amount }; var operatorId = User.Identity.GetUserId(); var db = new WarehouseDbContext(); var operatorData = db.Users.Where(o => o.Id == operatorId).FirstOrDefault(); var silo = db.Silos.Find(model.Id); try { silo.AddCommodity(commodity, operatorData.FullName); db.SaveChanges(); return(RedirectToAction("All", "Silos")); } catch (Exception ex) { ModelState.AddModelError("", $"{ex.Message}"); var siloOther = db.Silos .Where(s => s.Id == model.Id) .Select(s => new SiloViewModel { Number = s.SiloNumber, Name = s.Name, CurrentLoad = s.CurrentLoad, MaxCapacity = s.MaxCapacity, CapacityLeft = s.MaxCapacity - s.CurrentLoad, Commodity = new Commodity(), CurrentCommodity = s.CurrentCommodity }) .FirstOrDefault(); return(View(siloOther)); } } return(View(model)); }
public bool RemoveProducts(IEnumerable <int> producIds) { using (WarehouseDbContext _db = new WarehouseDbContext()) { foreach (var id in producIds) { var _removeItem = _db.Warehouse.FirstOrDefault(x => x.ProductId == id); _db.Warehouse.Attach(_removeItem); _db.Entry <Warehouse>(_removeItem).State = EntityState.Deleted; } return(_db.SaveChanges() > 0); } }
private void SeedDb(params Guid[] guids) { _mockDbContext.Orders.AddRange(guids.Select(x => new Domain.Order { Canceled = false, ItemId = 1, OrderUid = x, OrderItemUid = x, })); _mockDbContext.Items.Add(new Domain.Item { Id = 1, AvailableCount = 2, Model = "1", Size = "2" }); _mockDbContext.SaveChanges(); }
public ActionResult ConfirmDelete(int id, DeleteSiloModel model) { if (ModelState.IsValid) { var db = new WarehouseDbContext(); var silo = db.Silos .Where(a => a.Id == id) .FirstOrDefault(); var siloCheck = new DeleteSiloModel { CurrentCommodity = silo.CurrentCommodity, CurrentLoad = silo.CurrentLoad, CapacityLeft = silo.MaxCapacity - silo.CurrentLoad, Name = silo.Name }; if (silo == null) { return(HttpNotFound()); } try { silo.CanDeleteSilo(siloCheck); db.Silos.Remove(silo); var operations = db.Operations.Where(o => o.SiloId == id).ToList(); foreach (var op in operations) { db.Operations.Remove(op); } db.SaveChanges(); return(RedirectToAction("AllSilos")); } catch (Exception ex) { ModelState.AddModelError("", $"{ex.Message}"); return(View(model)); } } return(View(model)); }
public ActionResult ConfirmDelete(int id, DeleteTankModel model) { if (ModelState.IsValid) { var db = new WarehouseDbContext(); var tank = db.Tanks .Where(t => t.Id == id) .FirstOrDefault(); var tankCheck = new DeleteTankModel { CurrentFertilizer = tank.CurrentFertilizer, CurrentLoad = tank.CurrentLoad, CapacityLeft = tank.MaxCapacity - tank.CurrentLoad, Name = tank.Name }; if (tank == null) { return(HttpNotFound()); } try { tank.CanDelete(tankCheck); db.Tanks.Remove(tank); var tankOperations = db.TankOperations.Where(o => o.TankId == id).ToList(); foreach (var to in tankOperations) { db.TankOperations.Remove(to); } db.SaveChanges(); return(RedirectToAction("AllTanks")); } catch (Exception ex) { ModelState.AddModelError("", $"{ex.Message}"); return(View(model)); } } return(View(model)); }
private void Done() { WarehouseDbContext ctx = new WarehouseDbContext(); if (Order.ID == 0) { Order.Products = new List <OrderDetail>(); foreach (var item in Details) { OrderDetail detail = new OrderDetail { ProductId = item.ProductId, Quantity = item.Quantity, }; Order.Products.Add(detail); } ctx.Orders.Add(Order); ctx.SaveChanges(); } else { //foreach (var item in Details) //{ // var isExist = Order.Products.Any(x => x.ProductId == item.ProductId); // if (!isExist) // { // OrderDetail detail = new OrderDetail // { // ProductId = item.ProductId, // Quantity = item.Quantity, // }; // Order.Products.Add(detail); // } //} //ctx.SaveChanges(); } CurrentWindows.DialogResult = true; //OrdersWindow mywindow = new OrdersWindow(); //mywindow.Show(); }
public void TestUserViewModel_AddUser() { UsersViewModel model = new UsersViewModel(new UsersWindow()); model.Add(); model.IsNew = true; model.SelectedUser.Name = "Unit Test"; model.SelectedUser.Email = "*****@*****.**"; model.SelectedUser.Password = "******"; model.Save(); WarehouseDbContext ctx = new WarehouseDbContext(); var user = ctx.Users.SingleOrDefault(u => u.Name == "Unit Test"); Assert.AreNotEqual(null, user, "User added !"); //remove unit test user ctx.Users.Remove(user); ctx.SaveChanges(); }
public void ComplateSale() { using (WarehouseDbContext _db = new WarehouseDbContext()) { //using (var transact = _db.Database.BeginTransaction()) //{ try { foreach (var sale in sales) { var _whProd = _db.Warehouse.Where(x => x.ProductId == sale.ProductId).FirstOrDefault(); if (_whProd == null) { throw new InvalidOperationException(); } else { //need to update product if (_whProd.TotalRemind < sale.Count) { throw new Exception("Count not available"); } _whProd.TotalRemind -= sale.Count; _db.Warehouse.Attach(_whProd); _db.Entry <Warehouse>(_whProd).State = EntityState.Modified; } } _db.SaveChanges(); //transact.Commit(); } catch { //transact.Rollback(); throw; } } }
//private static DataBaseManager _db=DataBaseManager.GetDatabaseInstance(); //public static async Task<decimal> GetProductPriceById(int productId) //{ // using (DataBaseManager _db = new DataBaseManager()) // { // var currentProduct =await _db.Warehouse.FirstOrDefaultAsync(x => x.ProductId == productId); // return currentProduct != null ? currentProduct.RetailPrice : 0; // } //} //public static async Task<List<Warehouse>> GetALLProductsFromWarehouse() //{ // using (DataBaseManager _db = new DataBaseManager()) // { // return await _db.Warehouse.ToListAsync(); // } //} /// <summary> /// Binded to datasource /// </summary> /// <returns></returns> //public static List<WareHouseViewer> GetRemindProductsFromWarehouseSearch(string filter) //{ // using (DataBaseManager _db = new DataBaseManager()) // { // return (from wh in _db.Warehouse // join product in _db.Product on wh.ProductId equals product.Id // where ( // (filter == null || filter == "") || // product.Name.Trim().ToLower().Contains(filter.Trim().ToLower())) // && wh.TotalRemind > 0 // select new WareHouseViewer // { // BarCode = product.BarCode, // ProductName = product.Name, // RemindTotal = wh.TotalRemind, // RetailPrice = wh.RetailPrice // }).ToList(); // } //} /// <summary> /// Update warehouse price and Count /// Or insert a row /// </summary> /// <param name="buys"></param> public void ComplateBuyAction() { using (WarehouseDbContext _db = new WarehouseDbContext()) { foreach (var buy in buys) { var warehouse = _db.Warehouse.Where(x => x.ProductId == buy.ProductId).FirstOrDefault(); if (warehouse == null) { //need to add product _db.Warehouse.Add(new Warehouse() { ProductId = buy.ProductId, TotalRemind = buy.Count, RetailPrice = buy.RetailPrice, Price = buy.Price, WholeSalePrice = buy.WholeSalePrice, }); } else { //need to update product if (buy.RetailPrice != 0 && buy.RetailPrice != warehouse.RetailPrice) { warehouse.RetailPrice = buy.RetailPrice; warehouse.Price = buy.Price; warehouse.WholeSalePrice = buy.WholeSalePrice; } warehouse.TotalRemind += buy.Count; _db.Warehouse.Attach(warehouse); _db.Entry <Warehouse>(warehouse).State = EntityState.Modified; } } _db.SaveChanges(); } }
public void Save() { if (!IsNew) { //Edit existing user if (SelectedUser == null) { MessageBox.Show(CurrentWindows, "Please select a user before editting."); return; } WarehouseDbContext ctx = new WarehouseDbContext(); var user = ctx.Users.Single(u => u.ID == SelectedUser.ID); user.Name = SelectedUser.Name; user.Address = SelectedUser.Address; user.City = SelectedUser.City; user.Tel = SelectedUser.Tel; user.Email = SelectedUser.Email; user.Password = SelectedUser.Password; user.LastLoginDate = SelectedUser.LastLoginDate; ctx.SaveChanges(); MessageBox.Show(CurrentWindows, "Updated successfully !"); } else { //Add new user WarehouseDbContext ctx = new WarehouseDbContext(); ctx.Users.Add(SelectedUser); ctx.SaveChanges(); //Reload data from databse UserList = new List <User>(ctx.Users.ToList()); IsNew = false; } }
private void Save() { if (!IsNew) { if (SelectedCustomer == null) { MessageBox.Show(CurrentWindows, "Please select a user before editting."); return; } WarehouseDbContext ctx = new WarehouseDbContext(); var customer = ctx.Customers.Single(u => u.ID == SelectedCustomer.ID); customer.Name = SelectedCustomer.Name; customer.CompanyName = SelectedCustomer.CompanyName; customer.Address = SelectedCustomer.Address; customer.City = SelectedCustomer.City; customer.Telephone = SelectedCustomer.Telephone; customer.Email = SelectedCustomer.Email; ctx.SaveChanges(); MessageBox.Show(CurrentWindows, "Updated successfully !"); } else { WarehouseDbContext ctx = new WarehouseDbContext(); ctx.Customers.Add(SelectedCustomer); ctx.SaveChanges(); CustomerList = new List <Customer>(ctx.Customers.ToList()); IsNew = false; } }
public override Product Update(int id, Product entity) { var dbEntity = WarehouseDbContext.Product.FirstOrDefault(p => p.Id == id); if (dbEntity == null) { return(entity); } dbEntity.Category = entity.Category; dbEntity.Color = entity.Color; dbEntity.Company = entity.Company; dbEntity.Currency = entity.Currency; dbEntity.Description = entity.Description; dbEntity.Ean13 = entity.Ean13; dbEntity.Mail = entity.Mail; dbEntity.Name = entity.Name; dbEntity.Price = entity.Price; dbEntity.Review = entity.Review; dbEntity.Seller = entity.Seller; WarehouseDbContext.SaveChanges(); return(entity); }
private void Login() { WarehouseDbContext ctx = new WarehouseDbContext(); var user = ctx.Users.SingleOrDefault(u => u.Email == CurrentUser.Email); if (user != null && CurrentUser.Password == user.Password) { user.LastLoginDate = DateTime.Now; ctx.SaveChanges(); HomeWindow myWindow = new HomeWindow(); myWindow.Show(); this.CurrentWindows.Close(); //keep current user id in the application App.Current.Properties["CurrentUserID"] = user.ID; } else { MessageBox.Show(CurrentWindows, "Invalid email or password !"); return; } }
public void AddProduct(int docId, Product product) { if (product == null) { _log.Error("Product object is null"); throw new ArgumentNullException("Product not created"); } var document = _db.DocumentItems.FirstOrDefault(l => l.Id == docId); if (document == null) { _log.ErrorFormat("Document with id {0} not found", docId); throw new WarehouseException(String.Format("Document with id {0} not found", docId)); } product.DocumentItem = document; document.Products.Add(product); _db.SaveChanges(); }