public IActionResult Edit(int id, EditSupplierModel model)
        {
            if (!ModelState.IsValid)
            {
                return(this.View(model));
            }

            var success = this.supplierService.Edit(id, model.Name, model.IsImporter);

            if (!success)
            {
                TempData["error"] = "The supplier you are trying to edit does not exist!";
            }
            else
            {
                this.logService.Create(User.Identity.Name, "Edit", "Supplier");
            }

            return(RedirectToAction(nameof(AllSuppliers)));
        }
Example #2
0
        public IObjectIdentifier Save(EditSupplierModel input)
        {
            var supplier = input.IsNew ? new Supplier() : _repo.GetOrThrow <Supplier>(input.Id);

            supplier.Name = input.Name;
            var existing         = supplier.Products.ToList();
            var productsToAdd    = input.Products.Where(p => p.Checked && existing.All(sp => sp.Variant.Id != p.Id));
            var productsToRemove = existing.Where(sp => input.Products.Any(p => !p.Checked && p.Id == sp.Variant.Id));

            foreach (var toRemove in productsToRemove)
            {
                supplier.Remove(toRemove);
                _repo.Delete(toRemove);
            }
            foreach (var toAdd in productsToAdd)
            {
                supplier.Add(new SupplierProduct(_repo.Get <ProductVariant>(toAdd.Id)));
            }

            _repo.Save(supplier);
            _repo.Commit();
            return(new ObjectIdentifier(supplier.Name));
        }