public Vendor Execute(IEditVendor changedVendorData)
        {
            var vendor = _context.Vendors.SingleOrDefault(v => v.VendorId == changedVendorData.VendorId);

            if (vendor == null)
            {
                throw new NotFoundException <int>("vendor", changedVendorData.VendorId);
            }
            if (vendor.IsSystemReservedVendor())
            {
                throw new Exception("This vendor is required for proper system function and may not be modified.");
            }

            vendor.VendorName = changedVendorData.Company;

            if (vendor.VendorNamespacePrefixes.Any())
            {
                foreach (var vendorNamespacePrefix in vendor.VendorNamespacePrefixes.ToList())
                {
                    _context.VendorNamespacePrefixes.Remove(vendorNamespacePrefix);
                }
            }

            if (!string.IsNullOrEmpty(changedVendorData.NamespacePrefixes))
            {
                var namespacePrefixSplits = changedVendorData.NamespacePrefixes.Split(",");

                foreach (var namespacePrefix in namespacePrefixSplits)
                {
                    _context.VendorNamespacePrefixes.Add(new VendorNamespacePrefix
                    {
                        NamespacePrefix = namespacePrefix,
                        Vendor          = vendor
                    });
                }
            }

            if (vendor.Users?.FirstOrDefault() != null)
            {
                vendor.Users.First().FullName = changedVendorData.ContactName;
                vendor.Users.First().Email    = changedVendorData.ContactEmailAddress;
            }

            else
            {
                var vendorContact = new VendorUser
                {
                    Vendor   = vendor,
                    FullName = changedVendorData.ContactName,
                    Email    = changedVendorData.ContactEmailAddress
                };
                vendor.Users = new List <VendorUser> {
                    vendorContact
                };
            }

            _context.SaveChanges();
            return(vendor);
        }
Beispiel #2
0
        public void Execute(IEditVendor changedVendorData)
        {
            var vendor = _context.Vendors.Single(v => v.VendorId == changedVendorData.VendorId);

            vendor.VendorName = changedVendorData.Company;

            // because of no UI support for multiple VendorNamespacePrefixes,
            // we will always have only one VendorNamespacePrefix associated with vendor.
            // So, we'll update/remove the first item available.
            if (!string.IsNullOrEmpty(changedVendorData.NamespacePrefix))
            {
                if (vendor.VendorNamespacePrefixes.Any())
                {
                    vendor.VendorNamespacePrefixes.First().NamespacePrefix = changedVendorData.NamespacePrefix;
                }
                else
                {
                    vendor.VendorNamespacePrefixes.Add(new VendorNamespacePrefix
                    {
                        NamespacePrefix = changedVendorData.NamespacePrefix,
                        Vendor          = vendor
                    });
                }
            }
            else
            {
                if (vendor.VendorNamespacePrefixes.Any())
                {
                    var toRemove = vendor.VendorNamespacePrefixes.First();
                    _context.VendorNamespacePrefixes.Remove(toRemove);
                }
            }

            if (vendor.Users?.FirstOrDefault() != null)
            {
                vendor.Users.First().FullName = changedVendorData.ContactName;
                vendor.Users.First().Email    = changedVendorData.ContactEmailAddress;
            }

            else
            {
                var vendorContact = new VendorUser
                {
                    Vendor   = vendor,
                    FullName = changedVendorData.ContactName,
                    Email    = changedVendorData.ContactEmailAddress
                };
                vendor.Users = new List <VendorUser> {
                    vendorContact
                };
            }

            _context.SaveChanges();
        }