Try your search with a different keyword or use * as a wildcard.
using Nop.Core;
using Nop.Core.Domain.Directory;
using Nop.Data;
namespace Nop.Services.Directory;
///
/// Measure dimension service
///
public partial class MeasureService : IMeasureService
{
#region Fields
protected readonly IRepository _measureDimensionRepository;
protected readonly IRepository _measureWeightRepository;
protected readonly MeasureSettings _measureSettings;
#endregion
#region Ctor
public MeasureService(IRepository measureDimensionRepository,
IRepository measureWeightRepository,
MeasureSettings measureSettings)
{
_measureDimensionRepository = measureDimensionRepository;
_measureWeightRepository = measureWeightRepository;
_measureSettings = measureSettings;
}
#endregion
#region Methods
#region Dimensions
///
/// Deletes measure dimension
///
/// Measure dimension
/// A task that represents the asynchronous operation
public virtual async Task DeleteMeasureDimensionAsync(MeasureDimension measureDimension)
{
await _measureDimensionRepository.DeleteAsync(measureDimension);
}
///
/// Gets a measure dimension by identifier
///
/// Measure dimension identifier
///
/// A task that represents the asynchronous operation
/// The task result contains the measure dimension
///
public virtual async Task GetMeasureDimensionByIdAsync(int measureDimensionId)
{
return await _measureDimensionRepository.GetByIdAsync(measureDimensionId, cache => default);
}
///
/// Gets a measure dimension by system keyword
///
/// The system keyword
///
/// A task that represents the asynchronous operation
/// The task result contains the measure dimension
///
public virtual async Task GetMeasureDimensionBySystemKeywordAsync(string systemKeyword)
{
if (string.IsNullOrEmpty(systemKeyword))
return null;
var measureDimensions = await GetAllMeasureDimensionsAsync();
foreach (var measureDimension in measureDimensions)
if (measureDimension.SystemKeyword.ToLowerInvariant() == systemKeyword.ToLowerInvariant())
return measureDimension;
return null;
}
///
/// Gets all measure dimensions
///
///
/// A task that represents the asynchronous operation
/// The task result contains the measure dimensions
///
public virtual async Task> GetAllMeasureDimensionsAsync()
{
var measureDimensions = await _measureDimensionRepository.GetAllAsync(query =>
{
return from md in query
orderby md.DisplayOrder, md.Id
select md;
}, cache => default);
return measureDimensions;
}
///
/// Inserts a measure dimension
///
/// Measure dimension
/// A task that represents the asynchronous operation
public virtual async Task InsertMeasureDimensionAsync(MeasureDimension measure)
{
await _measureDimensionRepository.InsertAsync(measure);
}
///
/// Updates the measure dimension
///
/// Measure dimension
/// A task that represents the asynchronous operation
public virtual async Task UpdateMeasureDimensionAsync(MeasureDimension measure)
{
await _measureDimensionRepository.UpdateAsync(measure);
}
///
/// Converts dimension
///
/// Value to convert
/// Source dimension
/// Target dimension
/// A value indicating whether a result should be rounded
///
/// A task that represents the asynchronous operation
/// The task result contains the converted value
///
public virtual async Task ConvertDimensionAsync(decimal value,
MeasureDimension sourceMeasureDimension, MeasureDimension targetMeasureDimension, bool round = true)
{
ArgumentNullException.ThrowIfNull(sourceMeasureDimension);
ArgumentNullException.ThrowIfNull(targetMeasureDimension);
var result = value;
if (result != decimal.Zero && sourceMeasureDimension.Id != targetMeasureDimension.Id)
{
result = await ConvertToPrimaryMeasureDimensionAsync(result, sourceMeasureDimension);
result = await ConvertFromPrimaryMeasureDimensionAsync(result, targetMeasureDimension);
}
if (round)
result = Math.Round(result, 2);
return result;
}
///
/// Converts from primary dimension
///
/// Value to convert
/// Target dimension
///
/// A task that represents the asynchronous operation
/// The task result contains the converted value
///
public virtual async Task ConvertFromPrimaryMeasureDimensionAsync(decimal value,
MeasureDimension targetMeasureDimension)
{
ArgumentNullException.ThrowIfNull(targetMeasureDimension);
var result = value;
var baseDimensionIn = await GetMeasureDimensionByIdAsync(_measureSettings.BaseDimensionId);
if (result == decimal.Zero || targetMeasureDimension.Id == baseDimensionIn.Id)
return result;
var exchangeRatio = targetMeasureDimension.Ratio;
if (exchangeRatio == decimal.Zero)
throw new NopException($"Exchange ratio not set for dimension [{targetMeasureDimension.Name}]");
result *= exchangeRatio;
return result;
}
///
/// Converts to primary measure dimension
///
/// Value to convert
/// Source dimension
///
/// A task that represents the asynchronous operation
/// The task result contains the converted value
///
public virtual async Task ConvertToPrimaryMeasureDimensionAsync(decimal value,
MeasureDimension sourceMeasureDimension)
{
ArgumentNullException.ThrowIfNull(sourceMeasureDimension);
var result = value;
var baseDimensionIn = await GetMeasureDimensionByIdAsync(_measureSettings.BaseDimensionId);
if (result == decimal.Zero || sourceMeasureDimension.Id == baseDimensionIn.Id)
return result;
var exchangeRatio = sourceMeasureDimension.Ratio;
if (exchangeRatio == decimal.Zero)
throw new NopException($"Exchange ratio not set for dimension [{sourceMeasureDimension.Name}]");
result /= exchangeRatio;
return result;
}
#endregion
#region Weights
///
/// Deletes measure weight
///
/// Measure weight
/// A task that represents the asynchronous operation
public virtual async Task DeleteMeasureWeightAsync(MeasureWeight measureWeight)
{
await _measureWeightRepository.DeleteAsync(measureWeight);
}
///
/// Gets a measure weight by identifier
///
/// Measure weight identifier
///
/// A task that represents the asynchronous operation
/// The task result contains the measure weight
///
public virtual async Task GetMeasureWeightByIdAsync(int measureWeightId)
{
return await _measureWeightRepository.GetByIdAsync(measureWeightId, cache => default);
}
///
/// Gets a measure weight by system keyword
///
/// The system keyword
///
/// A task that represents the asynchronous operation
/// The task result contains the measure weight
///
public virtual async Task GetMeasureWeightBySystemKeywordAsync(string systemKeyword)
{
if (string.IsNullOrEmpty(systemKeyword))
return null;
var measureWeights = await GetAllMeasureWeightsAsync();
foreach (var measureWeight in measureWeights)
if (measureWeight.SystemKeyword.ToLowerInvariant() == systemKeyword.ToLowerInvariant())
return measureWeight;
return null;
}
///
/// Gets all measure weights
///
///
/// A task that represents the asynchronous operation
/// The task result contains the measure weights
///
public virtual async Task> GetAllMeasureWeightsAsync()
{
var measureWeights = await _measureWeightRepository.GetAllAsync(query =>
{
return from mw in query
orderby mw.DisplayOrder, mw.Id
select mw;
}, cache => default);
return measureWeights;
}
///
/// Inserts a measure weight
///
/// Measure weight
/// A task that represents the asynchronous operation
public virtual async Task InsertMeasureWeightAsync(MeasureWeight measure)
{
await _measureWeightRepository.InsertAsync(measure);
}
///
/// Updates the measure weight
///
/// Measure weight
/// A task that represents the asynchronous operation
public virtual async Task UpdateMeasureWeightAsync(MeasureWeight measure)
{
await _measureWeightRepository.UpdateAsync(measure);
}
///
/// Converts weight
///
/// Value to convert
/// Source weight
/// Target weight
/// A value indicating whether a result should be rounded
///
/// A task that represents the asynchronous operation
/// The task result contains the converted value
///
public virtual async Task ConvertWeightAsync(decimal value,
MeasureWeight sourceMeasureWeight, MeasureWeight targetMeasureWeight, bool round = true)
{
ArgumentNullException.ThrowIfNull(sourceMeasureWeight);
ArgumentNullException.ThrowIfNull(targetMeasureWeight);
var result = value;
if (result != decimal.Zero && sourceMeasureWeight.Id != targetMeasureWeight.Id)
{
result = await ConvertToPrimaryMeasureWeightAsync(result, sourceMeasureWeight);
result = await ConvertFromPrimaryMeasureWeightAsync(result, targetMeasureWeight);
}
if (round)
result = Math.Round(result, 2);
return result;
}
///
/// Converts from primary weight
///
/// Value to convert
/// Target weight
///
/// A task that represents the asynchronous operation
/// The task result contains the converted value
///
public virtual async Task ConvertFromPrimaryMeasureWeightAsync(decimal value,
MeasureWeight targetMeasureWeight)
{
ArgumentNullException.ThrowIfNull(targetMeasureWeight);
var result = value;
var baseWeightIn = await GetMeasureWeightByIdAsync(_measureSettings.BaseWeightId);
if (result == decimal.Zero || targetMeasureWeight.Id == baseWeightIn.Id)
return result;
var exchangeRatio = targetMeasureWeight.Ratio;
if (exchangeRatio == decimal.Zero)
throw new NopException($"Exchange ratio not set for weight [{targetMeasureWeight.Name}]");
result *= exchangeRatio;
return result;
}
///
/// Converts to primary measure weight
///
/// Value to convert
/// Source weight
///
/// A task that represents the asynchronous operation
/// The task result contains the converted value
///
public virtual async Task ConvertToPrimaryMeasureWeightAsync(decimal value, MeasureWeight sourceMeasureWeight)
{
ArgumentNullException.ThrowIfNull(sourceMeasureWeight);
var result = value;
var baseWeightIn = await GetMeasureWeightByIdAsync(_measureSettings.BaseWeightId);
if (result == decimal.Zero || sourceMeasureWeight.Id == baseWeightIn.Id)
return result;
var exchangeRatio = sourceMeasureWeight.Ratio;
if (exchangeRatio == decimal.Zero)
throw new NopException($"Exchange ratio not set for weight [{sourceMeasureWeight.Name}]");
result /= exchangeRatio;
return result;
}
#endregion
#endregion
}