using Nop.Core; using Nop.Core.Caching; using Nop.Data; using Nop.Plugin.Misc.AuctionPlugin.Domains; using Nop.Plugin.Misc.AuctionPlugin.Domains.DataLayer; using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities; namespace Nop.Plugin.Misc.AuctionPlugin.Services; /// /// Store pickup point service /// public class AuctionService : IAuctionService { #region Constants /// /// Cache key for pickup points /// /// /// {0} : current store ID /// protected readonly CacheKey _auctionAllKey = new("Nop.auction.all-{0}", AUCTION_PATTERN_KEY); protected const string AUCTION_PATTERN_KEY = "Nop.auction."; #endregion #region Fields protected readonly AuctionDbContext _ctx; //protected readonly IRepository _customerBidRepository; //protected readonly IRepository _auctionRepository; protected readonly IShortTermCacheManager _shortTermCacheManager; protected readonly IStaticCacheManager _staticCacheManager; #endregion #region Ctor /// /// Ctor /// /// Store pickup point repository /// Short term cache manager /// Cache manager public AuctionService(AuctionDbContext ctx, IShortTermCacheManager shortTermCacheManager, IStaticCacheManager staticCacheManager) { _ctx = ctx; //_customerBidRepository = customerBidRepository; //_auctionRepository = auctionRepository; _shortTermCacheManager = shortTermCacheManager; _staticCacheManager = staticCacheManager; } #endregion #region Methods /// /// Gets all bids /// /// 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 bids /// public virtual async Task> GetAllBidsAsync(int customerId = 0, int pageIndex = 0, int pageSize = int.MaxValue) { var rez = new List(); //var rez = await _shortTermCacheManager.GetAsync(async () => await _customerBidRepository.GetAllAsync(query => //{ // if (customerId > 0) // query = query.Where(bid => bid.CustomerId == customerId || bid.CustomerId == 0); // query = query.OrderBy(bid => bid.CreateDate).ThenBy(bid => bid.ProductId); // return query; //}), _pickupPointAllKey, customerId); return new PagedList(rez, pageIndex, pageSize); } public virtual async Task GetBidByIdAsync(int bidId) { return await _ctx.AuctionBids.GetByIdAsync(bidId); } public virtual async Task InsertBidAsync(AuctionBid auctionBid) { await _ctx.AuctionBids.InsertAsync(auctionBid, false); await _staticCacheManager.RemoveByPrefixAsync(AUCTION_PATTERN_KEY); } public virtual async Task UpdateBidAsync(AuctionBid auctionBid) { await _ctx.AuctionBids.UpdateAsync(auctionBid, false); await _staticCacheManager.RemoveByPrefixAsync(AUCTION_PATTERN_KEY); } /// /// Deletes a pickup point /// /// Pickup point /// A task that represents the asynchronous operation public virtual async Task DeleteBidAsync(AuctionBid pickupPoint) { await _ctx.AuctionBids.DeleteAsync(pickupPoint, false); await _staticCacheManager.RemoveByPrefixAsync(AUCTION_PATTERN_KEY); } #region auctions public virtual async Task InsertAuctionAsync(Auction auction) { await _ctx.Auctions.InsertAsync(auction, false); await _staticCacheManager.RemoveByPrefixAsync(AUCTION_PATTERN_KEY); } public async Task> GetAllAuctionsAsync() { return (await _ctx.Auctions.GetAllAsync(auctions => auctions.OrderByDescending(x => x.StartDateUtc), _ => default)).ToList(); } #endregion #endregion }