Mango.Nop.Plugins/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionService.cs

128 lines
4.2 KiB
C#

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;
/// <summary>
/// Store pickup point service
/// </summary>
public class AuctionService : IAuctionService
{
#region Constants
/// <summary>
/// Cache key for pickup points
/// </summary>
/// <remarks>
/// {0} : current store ID
/// </remarks>
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<AuctionBid> _customerBidRepository;
//protected readonly IRepository<Auction> _auctionRepository;
protected readonly IShortTermCacheManager _shortTermCacheManager;
protected readonly IStaticCacheManager _staticCacheManager;
#endregion
#region Ctor
/// <summary>
/// Ctor
/// </summary>
/// <param name="customerBidRepository">Store pickup point repository</param>
/// <param name="shortTermCacheManager">Short term cache manager</param>
/// <param name="staticCacheManager">Cache manager</param>
public AuctionService(AuctionDbContext ctx, IShortTermCacheManager shortTermCacheManager, IStaticCacheManager staticCacheManager)
{
_ctx = ctx;
//_customerBidRepository = customerBidRepository;
//_auctionRepository = auctionRepository;
_shortTermCacheManager = shortTermCacheManager;
_staticCacheManager = staticCacheManager;
}
#endregion
#region Methods
/// <summary>
/// Gets all bids
/// </summary>
/// <param name="customerId">The store identifier; pass 0 to load all records</param>
/// <param name="pageIndex">Page index</param>
/// <param name="pageSize">Page size</param>
/// <returns>
/// A task that represents the asynchronous operation
/// The task result contains the bids
/// </returns>
public virtual async Task<IPagedList<AuctionBid>> GetAllBidsAsync(int customerId = 0, int pageIndex = 0, int pageSize = int.MaxValue)
{
var rez = new List<AuctionBid>();
//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<AuctionBid>(rez, pageIndex, pageSize);
}
public virtual async Task<AuctionBid> 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);
}
/// <summary>
/// Deletes a pickup point
/// </summary>
/// <param name="pickupPoint">Pickup point</param>
/// <returns>A task that represents the asynchronous operation</returns>
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<List<Auction>> GetAllAuctionsAsync()
{
return (await _ctx.Auctions.GetAllAsync(auctions => auctions.OrderByDescending(x => x.StartDateUtc), _ => default)).ToList();
}
#endregion
#endregion
}