Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

4.8 KB
Raw Blame
using Grand.Business.Core.Extensions;
using Grand.Business.Core.Interfaces.Customers;
using Grand.Domain.Catalog;
using Grand.Domain.Customers;
using Grand.Infrastructure;
using Grand.Web.Features.Models.Customers;
using Grand.Web.Models.Customer;
using Grand.Mediator;

namespace Grand.Web.Features.Handlers.Customers;

public class GetCustomAttributesHandler : IRequestHandler<GetCustomAttributes, IList<CustomerAttributeModel>>
{
    private readonly ICustomerAttributeParser _customerAttributeParser;
    private readonly ICustomerAttributeService _customerAttributeService;
    private readonly IContextAccessor _contextAccessor;

    public GetCustomAttributesHandler(ICustomerAttributeService customerAttributeService,
        ICustomerAttributeParser customerAttributeParser,
        IContextAccessor contextAccessor)
    {
        _customerAttributeService = customerAttributeService;
        _customerAttributeParser = customerAttributeParser;
        _contextAccessor = contextAccessor;
    }

    public async Task<IList<CustomerAttributeModel>> Handle(GetCustomAttributes request,
        CancellationToken cancellationToken)
    {
        var result = new List<CustomerAttributeModel>();

        var customerAttributes =
            await _customerAttributeService.GetAllCustomerAttributes(_contextAccessor.StoreContext.CurrentStore.Id);
        foreach (var attribute in customerAttributes)
        {
            var attributeModel = new CustomerAttributeModel {
                Id = attribute.Id,
                Name = attribute.GetTranslation(x => x.Name, request.Language.Id),
                IsRequired = attribute.IsRequired,
                IsReadOnly = attribute.IsReadOnly,
                AttributeControlType = attribute.AttributeControlTypeId
            };

            if (attribute.ShouldHaveValues())
            {
                //values
                var attributeValues = attribute.CustomerAttributeValues;
                foreach (var attributeValue in attributeValues)
                {
                    var valueModel = new CustomerAttributeValueModel {
                        Id = attributeValue.Id,
                        Name = attributeValue.GetTranslation(x => x.Name, request.Language.Id),
                        IsPreSelected = attributeValue.IsPreSelected
                    };
                    attributeModel.Values.Add(valueModel);
                }
            }

            //set already selected attributes
            var selectedAttributes = request.OverrideAttributes ?? request.Customer.Attributes;

            switch (attribute.AttributeControlTypeId)
            {
                case AttributeControlType.DropdownList:
                case AttributeControlType.RadioList:
                case AttributeControlType.Checkboxes:
                {
                    if (selectedAttributes.Any())
                    {
                        //clear default selection
                        foreach (var item in attributeModel.Values)
                            item.IsPreSelected = false;

                        //select new values
                        var selectedValues =
                            await _customerAttributeParser.ParseCustomerAttributeValues(selectedAttributes);
                        foreach (var attributeValue in selectedValues)
                            if (attributeModel.Id == attributeValue.CustomerAttributeId)
                                foreach (var item in attributeModel.Values)
                                    if (attributeValue.Id == item.Id)
                                        item.IsPreSelected = true;
                    }
                }
                    break;
                case AttributeControlType.ReadonlyCheckboxes:
                {
                    //do nothing
                    //values are already pre-set
                }
                    break;
                case AttributeControlType.TextBox:
                case AttributeControlType.MultilineTextbox:
                {
                    if (selectedAttributes.Any())
                    {
                        var enteredText = selectedAttributes.Where(x => x.Key == attribute.Id).Select(x => x.Value)
                            .ToList();
                        if (enteredText.Any())
                            attributeModel.DefaultValue = enteredText[0];
                    }
                }
                    break;
                case AttributeControlType.ColorSquares:
                case AttributeControlType.ImageSquares:
                case AttributeControlType.Datepicker:
                case AttributeControlType.FileUpload:
                default:
                    //not supported attribute control types
                    break;
            }

            result.Add(attributeModel);
        }


        return result;
    }
}