Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

ShippingMethodsService.cs

using Nop.Core.Domain.Shipping;
using Nop.Data;
using Nop.Services.Catalog;

namespace Nop.Services.Shipping;

/// 
/// Shipping methods service
/// 
public partial class ShippingMethodsService : IShippingMethodsService
{
    #region Fields

    protected readonly IRepository _shippingMethodRepository;
    protected readonly IRepository _shippingMethodCountryMappingRepository;

    #endregion

    #region Ctor

    public ShippingMethodsService(IRepository shippingMethodRepository,
        IRepository shippingMethodCountryMappingRepository)
    {
        _shippingMethodRepository = shippingMethodRepository;
        _shippingMethodCountryMappingRepository = shippingMethodCountryMappingRepository;
    }

    #endregion

    #region Methods

    /// 
    /// Deletes a shipping method
    /// 
    /// The shipping method
    /// A task that represents the asynchronous operation
    public virtual async Task DeleteShippingMethodAsync(ShippingMethod shippingMethod)
    {
        await _shippingMethodRepository.DeleteAsync(shippingMethod);
    }

    /// 
    /// Gets a shipping method
    /// 
    /// The shipping method identifier
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the shipping method
    /// 
    public virtual async Task GetShippingMethodByIdAsync(int shippingMethodId)
    {
        return await _shippingMethodRepository.GetByIdAsync(shippingMethodId, _ => default);
    }

    /// 
    /// Gets all shipping methods
    /// 
    /// The country identifier to filter by
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the shipping methods
    /// 
    public virtual async Task> GetAllShippingMethodsAsync(int? filterByCountryId = null)
    {
        if (filterByCountryId is > 0)
        {
            return await _shippingMethodRepository.GetAllAsync(query =>
            {
                var query1 =
                    from shippingMethod in query
                    join shippingMethodCountryMapping in _shippingMethodCountryMappingRepository.Table on shippingMethod.Id equals shippingMethodCountryMapping.ShippingMethodId
                    where shippingMethodCountryMapping.CountryId == filterByCountryId.Value
                    select shippingMethod.Id;

                query1 = query1.Distinct();

                var query2 =
                    from sm in query
                    where !query1.Contains(sm.Id)
                    orderby sm.DisplayOrder, sm.Id
                    select sm;

                return query2;
            }, cache => cache.PrepareKeyForDefaultCache(NopShippingDefaults.ShippingMethodsAllCacheKey, filterByCountryId));
        }

        return await _shippingMethodRepository.GetAllAsync(query =>
            from sm in query
            orderby sm.DisplayOrder, sm.Id
            select sm, _ => default);
    }

    /// 
    /// Inserts a shipping method
    /// 
    /// Shipping method
    /// A task that represents the asynchronous operation
    public virtual async Task InsertShippingMethodAsync(ShippingMethod shippingMethod)
    {
        await _shippingMethodRepository.InsertAsync(shippingMethod);
    }

    /// 
    /// Updates the shipping method
    /// 
    /// Shipping method
    /// A task that represents the asynchronous operation
    public virtual async Task UpdateShippingMethodAsync(ShippingMethod shippingMethod)
    {
        await _shippingMethodRepository.UpdateAsync(shippingMethod);
    }

    /// 
    /// Does country restriction exist
    /// 
    /// Shipping method
    /// Country identifier
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the result
    /// 
    public virtual async Task CountryRestrictionExistsAsync(ShippingMethod shippingMethod, int countryId)
    {
        ArgumentNullException.ThrowIfNull(shippingMethod);

        var result = await _shippingMethodCountryMappingRepository.Table
            .AnyAsync(shippingMethodCountryMapping => shippingMethodCountryMapping.ShippingMethodId == shippingMethod.Id && shippingMethodCountryMapping.CountryId == countryId);

        return result;
    }

    /// 
    /// Gets shipping country mappings
    /// 
    /// The shipping method identifier
    /// Country identifier
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the shipping country mappings
    /// 
    public virtual async Task> GetShippingMethodCountryMappingAsync(int shippingMethodId,
        int countryId)
    {
        var query = _shippingMethodCountryMappingRepository.Table.Where(shippingMethodCountryMapping =>
            shippingMethodCountryMapping.ShippingMethodId == shippingMethodId && shippingMethodCountryMapping.CountryId == countryId);

        return await query.ToListAsync();
    }

    /// 
    /// Inserts a shipping country mapping
    /// 
    /// Shipping country mapping
    /// A task that represents the asynchronous operation
    public virtual async Task InsertShippingMethodCountryMappingAsync(ShippingMethodCountryMapping shippingMethodCountryMapping)
    {
        await _shippingMethodCountryMappingRepository.InsertAsync(shippingMethodCountryMapping);
    }

    /// 
    /// Delete the shipping country mapping
    /// 
    /// Shipping country mapping
    /// A task that represents the asynchronous operation
    public virtual async Task DeleteShippingMethodCountryMappingAsync(ShippingMethodCountryMapping shippingMethodCountryMapping)
    {
        await _shippingMethodCountryMappingRepository.DeleteAsync(shippingMethodCountryMapping);
    }

    #endregion
}