using Nop.Core; using Nop.Core.Caching; using Nop.Data; using Nop.Plugin.Pickup.PickupInStore.Domain; namespace Nop.Plugin.Pickup.PickupInStore.Services; /// /// Store pickup point service /// public class StorePickupPointService : IStorePickupPointService { #region Constants /// /// Cache key for pickup points /// /// /// {0} : current store ID /// protected readonly CacheKey _pickupPointAllKey = new("Nop.pickuppoint.all-{0}", PICKUP_POINT_PATTERN_KEY); protected const string PICKUP_POINT_PATTERN_KEY = "Nop.pickuppoint."; #endregion #region Fields protected readonly IRepository _storePickupPointRepository; protected readonly IShortTermCacheManager _shortTermCacheManager; protected readonly IStaticCacheManager _staticCacheManager; #endregion #region Ctor /// /// Ctor /// /// Store pickup point repository /// Short term cache manager /// Cache manager public StorePickupPointService(IRepository storePickupPointRepository, IShortTermCacheManager shortTermCacheManager, IStaticCacheManager staticCacheManager) { _storePickupPointRepository = storePickupPointRepository; _shortTermCacheManager = shortTermCacheManager; _staticCacheManager = staticCacheManager; } #endregion #region Methods /// /// Gets all pickup points /// /// The store identifier; pass 0 to load all records /// Page index /// Page size /// /// A task that represents the asynchronous operation /// The task result contains the pickup points /// public virtual async Task> GetAllStorePickupPointsAsync(int storeId = 0, int pageIndex = 0, int pageSize = int.MaxValue) { var rez = await _shortTermCacheManager.GetAsync(async () => await _storePickupPointRepository.GetAllAsync(query => { if (storeId > 0) query = query.Where(point => point.StoreId == storeId || point.StoreId == 0); query = query.OrderBy(point => point.DisplayOrder).ThenBy(point => point.Name); return query; }), _pickupPointAllKey, storeId); return new PagedList(rez, pageIndex, pageSize); } /// /// Gets a pickup point /// /// Pickup point identifier /// /// A task that represents the asynchronous operation /// The task result contains the pickup point /// public virtual async Task GetStorePickupPointByIdAsync(int pickupPointId) { return await _storePickupPointRepository.GetByIdAsync(pickupPointId); } /// /// Inserts a pickup point /// /// Pickup point /// A task that represents the asynchronous operation public virtual async Task InsertStorePickupPointAsync(StorePickupPoint pickupPoint) { await _storePickupPointRepository.InsertAsync(pickupPoint, false); await _staticCacheManager.RemoveByPrefixAsync(PICKUP_POINT_PATTERN_KEY); } /// /// Updates the pickup point /// /// Pickup point /// A task that represents the asynchronous operation public virtual async Task UpdateStorePickupPointAsync(StorePickupPoint pickupPoint) { await _storePickupPointRepository.UpdateAsync(pickupPoint, false); await _staticCacheManager.RemoveByPrefixAsync(PICKUP_POINT_PATTERN_KEY); } /// /// Deletes a pickup point /// /// Pickup point /// A task that represents the asynchronous operation public virtual async Task DeleteStorePickupPointAsync(StorePickupPoint pickupPoint) { await _storePickupPointRepository.DeleteAsync(pickupPoint, false); await _staticCacheManager.RemoveByPrefixAsync(PICKUP_POINT_PATTERN_KEY); } #endregion }