<commit_before>﻿using System.Collections.Generic;
using Nop.Core.Domain.Vendors;
using Nop.Core.Infrastructure;
using Nop.Services.Vendors;

namespace Nop.Plugin.Api.Attributes
{
    public class ValidateVendor : BaseValidationAttribute
    {
        private Dictionary<string, string> _errors;

        private IVendorService _vendorService;

        private IVendorService VendorService
        {
            get
            {
                if (_vendorService == null)
                {
                    _vendorService = EngineContext.Current.Resolve<IVendorService>();
                }

                return _vendorService;
            }
        }

        public ValidateVendor()
        {
            _errors = new Dictionary<string, string>();
        }

        public override void Validate(object instance)
        {
            int vendorId = 0;

            if (instance != null && int.TryParse(instance.ToString(), out vendorId))
            {
                Vendor vendor = VendorService.GetVendorById(vendorId);

                if (vendor == null)
                {
                    _errors.Add("Invalid vendor id", "Non existing vendor");
                }
                // TODO: validate that the vendorId is an existing vendor.
            }
            // The type validation happens in the model binder automatically so we don't need to set an error here.
        }

        public override Dictionary<string, string> GetErrors()
        {
            return _errors;
        }
    }
}<commit_msg>ValidateVendor should handle the case when we want to remove the Vendor by setting it to 0 rather than throwing an error that such a vendor does not exist.<commit_after>﻿using System.Collections.Generic;
using Nop.Core.Domain.Vendors;
using Nop.Core.Infrastructure;
using Nop.Services.Vendors;

namespace Nop.Plugin.Api.Attributes
{
    public class ValidateVendor : BaseValidationAttribute
    {
        private Dictionary<string, string> _errors;

        private IVendorService _vendorService;

        private IVendorService VendorService
        {
            get
            {
                if (_vendorService == null)
                {
                    _vendorService = EngineContext.Current.Resolve<IVendorService>();
                }

                return _vendorService;
            }
        }

        public ValidateVendor()
        {
            _errors = new Dictionary<string, string>();
        }

        public override void Validate(object instance)
        {
            int vendorId = 0;

            if (instance != null && int.TryParse(instance.ToString(), out vendorId))
            {
                if (vendorId > 0)
                {
                    Vendor vendor = VendorService.GetVendorById(vendorId);

                    if (vendor == null)
                    {
                        _errors.Add("Invalid vendor id", "Non existing vendor");
                    }
                }
            }
        }

        public override Dictionary<string, string> GetErrors()
        {
            return _errors;
        }
    }
}
{'fraction_non_alphanumeric': 0.07476635514018691, 'fraction_numerical': 0.0012890750886239124, 'mean_word_length': 1.546349466776046, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5515413', 'n_tokens_mistral': 763, 'n_tokens_neox': 724, 'n_words': 200}