Example #1
0
        /// <summary>
        /// Prepare paged customer address list model
        /// </summary>
        /// <param name="searchModel">Customer address search model</param>
        /// <param name="customer">Customer</param>
        /// <returns>Customer address list model</returns>
        public virtual CustomerAddressListModel PrepareCustomerAddressListModel(CustomerAddressSearchModel searchModel, Customer customer)
        {
            if (searchModel == null)
            {
                throw new ArgumentNullException(nameof(searchModel));
            }

            if (customer == null)
            {
                throw new ArgumentNullException(nameof(customer));
            }

            //get customer addresses
            var addresses = customer.Addresses
                            .OrderByDescending(address => address.CreatedOnUtc).ThenByDescending(address => address.Id).ToList();

            //prepare list model
            var model = new CustomerAddressListModel
            {
                Data = addresses.PaginationByRequestModel(searchModel).Select(address =>
                {
                    //fill in model values from the entity
                    var addressModel = address.ToModel <AddressModel>();

                    //fill in additional values (not existing in the entity)
                    PrepareModelAddressHtml(addressModel, address);

                    return(addressModel);
                }),
                Total = addresses.Count
            };

            return(model);
        }
        public virtual async Task <IActionResult> AddressesSelect(CustomerAddressSearchModel searchModel)
        {
            if (!await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManageCustomers))
            {
                return(await AccessDeniedDataTablesJson());
            }

            //try to get a company with the specified id
            var company = await _companyService.GetCompanyByIdAsync(searchModel.CustomerId)
                          ?? throw new ArgumentException("No company found with the specified id");

            //prepare model
            var model = await _companyModelFactory.PrepareCompanyCustomerAddressListModelAsync(searchModel, company);

            return(Json(model));
        }
        public virtual async Task <CustomerAddressListModel> PrepareCompanyCustomerAddressListModelAsync(CustomerAddressSearchModel searchModel, Company company)
        {
            if (searchModel == null)
            {
                throw new ArgumentNullException(nameof(searchModel));
            }

            if (company == null)
            {
                throw new ArgumentNullException(nameof(company));
            }

            var model = new CustomerAddressListModel();
            var getCompanyCustomers = await _companyService.GetCompanyCustomersByCompanyIdAsync(company.Id);

            IPagedList <Address> addressesList = new PagedList <Address>(new List <Address>(), 0, 1);

            foreach (var getCompanyCustomer in getCompanyCustomers)
            {
                var customer = await _customerService.GetCustomerByIdAsync(getCompanyCustomer.CustomerId);

                //get customer addresses
                var addresses = (await _customerService.GetAddressesByCustomerIdAsync(customer.Id))
                                .OrderByDescending(address => address.CreatedOnUtc).ThenByDescending(address => address.Id).ToList()
                                .ToPagedList(searchModel);

                foreach (var address in addresses)
                {
                    if (!addressesList.Where(x => x.Id == address.Id).Any())
                    {
                        addressesList.Add(address);
                    }
                }
                searchModel.CustomerId = customer.Id;
            }

            //prepare list model
            model = await new CustomerAddressListModel().PrepareToGridAsync(searchModel, addressesList, () =>
            {
                return(addressesList.SelectAwait(async address =>
                {
                    //fill in model values from the entity
                    var addressModel = address.ToModel <AddressModel>();

                    var customerAddressMapping = await _customerService.GetCustomerAddressesByAddressIdAsync(address.Id);
                    addressModel.CustomerId = customerAddressMapping.Any() ? customerAddressMapping.FirstOrDefault().CustomerId : 0;
                    addressModel.CountryName = (await _countryService.GetCountryByAddressAsync(address))?.Name;
                    addressModel.StateProvinceName = (await _stateProvinceService.GetStateProvinceByAddressAsync(address))?.Name;

                    //fill in additional values (not existing in the entity)
                    await PrepareModelAddressHtmlAsync(addressModel, address);

                    return addressModel;
                }));
            });
            return(model);
        }
Example #4
0
        /// <summary>
        /// Prepare customer address search model
        /// </summary>
        /// <param name="searchModel">Customer address search model</param>
        /// <param name="customer">Customer</param>
        /// <returns>Customer address search model</returns>
        protected virtual CustomerAddressSearchModel PrepareCustomerAddressSearchModel(CustomerAddressSearchModel searchModel, Customer customer)
        {
            if (searchModel == null)
            {
                throw new ArgumentNullException(nameof(searchModel));
            }

            if (customer == null)
            {
                throw new ArgumentNullException(nameof(customer));
            }

            searchModel.CustomerId = customer.Id;

            //prepare page parameters
            searchModel.SetGridPageSize();

            return(searchModel);
        }