Ejemplo n.º 1
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));
        }