using Nop.Core.Caching; using Nop.Core.Domain.Attributes; using Nop.Data; namespace Nop.Services.Attributes; /// /// Attribute service /// /// Type of the attribute (see ) /// Type of the attribute value (see ) public partial class AttributeService : IAttributeService where TAttribute : BaseAttribute where TAttributeValue : BaseAttributeValue { #region Fields protected readonly IRepository _attributeRepository; protected readonly IRepository _attributeValueRepository; protected readonly IStaticCacheManager _staticCacheManager; #endregion #region Ctor public AttributeService(IRepository attributeRepository, IRepository attributeValueRepository, IStaticCacheManager staticCacheManager) { _attributeRepository = attributeRepository; _attributeValueRepository = attributeValueRepository; _staticCacheManager = staticCacheManager; } #endregion #region Methods #region Attributes /// /// Gets all attributes /// /// /// A task that represents the asynchronous operation /// The task result contains the attributes /// public async Task> GetAllAttributesAsync() { return await _attributeRepository.GetAllAsync( query => query.OrderBy(attribute => attribute.DisplayOrder) .ThenBy(attribute => attribute.Id), _ => default); } /// /// Gets a attribute /// /// attribute identifier /// /// A task that represents the asynchronous operation /// The task result contains the attribute /// public async Task GetAttributeByIdAsync(int attributeId) { return await _attributeRepository.GetByIdAsync(attributeId, _ => default); } /// /// Inserts a attribute /// /// attribute /// A task that represents the asynchronous operation public async Task InsertAttributeAsync(TAttribute attribute) { await _attributeRepository.InsertAsync(attribute); } /// /// Updates a attribute /// /// attribute /// A task that represents the asynchronous operation public async Task UpdateAttributeAsync(TAttribute attribute) { await _attributeRepository.UpdateAsync(attribute); } /// /// Deletes a attribute /// /// attribute /// A task that represents the asynchronous operation public async Task DeleteAttributeAsync(TAttribute attribute) { await _attributeRepository.DeleteAsync(attribute); } /// /// Gets attributes /// /// Attribute identifiers /// /// A task that represents the asynchronous operation /// The task result contains the attributes /// public virtual async Task> GetAttributeByIdsAsync(int[] attributeIds) { return await _attributeRepository.GetByIdsAsync(attributeIds); } /// /// Deletes attributes /// /// Attributes /// A task that represents the asynchronous operation public virtual async Task DeleteAttributesAsync(IList attributes) { ArgumentNullException.ThrowIfNull(attributes); await _attributeRepository.DeleteAsync(attributes); } #endregion #region Attribute values /// /// Gets attribute values by attribute identifier /// /// The attribute identifier /// /// A task that represents the asynchronous operation /// The task result contains the attribute values /// public async Task> GetAttributeValuesAsync(int attributeId) { var key = _staticCacheManager.PrepareKeyForDefaultCache( NopAttributeDefaults.AttributeValuesByAttributeCacheKey, typeof(TAttribute).Name, attributeId); var query = _attributeValueRepository.Table .Where(attributeValue => attributeValue.AttributeId == attributeId) .OrderBy(attributeValue => attributeValue.DisplayOrder) .ThenBy(attributeValue => attributeValue.Id); return await _staticCacheManager.GetAsync(key, async () => await query.ToListAsync()); } /// /// Gets a attribute value /// /// attribute value identifier /// /// A task that represents the asynchronous operation /// The task result contains the attribute value /// public async Task GetAttributeValueByIdAsync(int attributeValueId) { return await _attributeValueRepository.GetByIdAsync(attributeValueId, _ => default); } /// /// Inserts a attribute value /// /// attribute value /// A task that represents the asynchronous operation public async Task InsertAttributeValueAsync(TAttributeValue attributeValue) { await _attributeValueRepository.InsertAsync(attributeValue); } /// /// Updates a attribute value /// /// attribute value /// A task that represents the asynchronous operation public async Task UpdateAttributeValueAsync(TAttributeValue attributeValue) { await _attributeValueRepository.UpdateAsync(attributeValue); } /// /// Deletes a attribute value /// /// attribute value /// A task that represents the asynchronous operation public async Task DeleteAttributeValueAsync(TAttributeValue attributeValue) { await _attributeValueRepository.DeleteAsync(attributeValue); } #endregion #endregion }