using AyCode.Core.Extensions; using Nop.Core; using Nop.Core.Caching; using Nop.Core.Domain.Customers; using Nop.Data; using Nop.Plugin.Misc.AuctionPlugin.Domains; using Nop.Plugin.Misc.AuctionPlugin.Domains.DataLayer; using Nop.Plugin.Misc.AuctionPlugin.Domains.Dtos; using Nop.Plugin.Misc.AuctionPlugin.Domains.Entities; using Nop.Services.Logging; 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 IRepository _productToAuctionRepository; protected readonly IShortTermCacheManager _shortTermCacheManager; protected readonly IStaticCacheManager _staticCacheManager; protected readonly IWorkContext _workContext; protected readonly ILogger _logger; #endregion #region Ctor /// /// Ctor /// /// /// Short term cache manager /// Cache manager public AuctionService( AuctionDbContext ctx, //IRepository auctionRepository, //IRepository productToAuctionRepository, IShortTermCacheManager shortTermCacheManager, IStaticCacheManager staticCacheManager, IWorkContext workContext, ILogger logger) { _ctx = ctx; //_customerBidRepository = customerBidRepository; //_auctionRepository = auctionRepository; //_productToAuctionRepository = productToAuctionRepository; _shortTermCacheManager = shortTermCacheManager; _staticCacheManager = staticCacheManager; _workContext = workContext; _logger = logger; } #endregion #region Methods private async Task ValidateAuctionBid(AuctionBid auctionBid) { //auctionBid.CustomerId = (await _workContext.GetCurrentCustomerAsync()).Id; //elvileg megnézi cache-ben, csak utána DB-zik! - J. //etc... return true; } /// /// 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) { if (await ValidateAuctionBid(auctionBid)) { await _ctx.AuctionBids.InsertAsync(auctionBid, false); await _staticCacheManager.RemoveByPrefixAsync(AUCTION_PATTERN_KEY); } } public virtual async Task UpdateBidAsync(AuctionBid auctionBid) { if (await ValidateAuctionBid(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.GetAllAuctionsAsync(); } public async Task> GetProductToAuctionsByAuctionIdAsync(int auctionId, bool onlyActiveItems = false) { //return (await _ctx.Auctions.GetAllAsync(auctions => auctions.OrderByDescending(x => x.StartDateUtc), _ => default)).ToList(); return []; } #endregion #endregion #region Dtots public async Task GetAuctionDtoByIdAsync(int auctionId) { var auction = await _ctx.Auctions.GetByIdAsync(auctionId); return auction == null ? null : new AuctionDto(auction); } public async Task GetAuctionDtoWithProductByIdAsync(int auctionId, int productId) { var auction = await _ctx.Auctions.GetByIdAsync(auctionId); if (auction == null) return null; var auctionDto = new AuctionDto(auction); auctionDto.ProductToAuctionDtos.AddRange(await _ctx.ProductToAuctions.GetByAuctionAndProductId(auctionId, productId).Select(x => new ProductToAuctionDto(x)).ToListAsync()); return auctionDto; } public async Task GetProductToAuctionDtoByIdAsync(int productToAuctionId) { var productToAuction = await _ctx.ProductToAuctions.GetByIdAsync(productToAuctionId); return productToAuction == null ? null : new ProductToAuctionDto(productToAuction); } public async Task GetAuctionDtoByProductToAuctionIdAsync(int productToAuctionId, bool includeProductToAuctionDto = true) { var productTouctionDto = await GetProductToAuctionDtoByIdAsync(productToAuctionId); if (productTouctionDto == null) return null; var auctionDto = await GetAuctionDtoByIdAsync(productTouctionDto.AuctionId); //Ez sosem lehet null! - J. if (includeProductToAuctionDto) auctionDto.ProductToAuctionDtos.Add(productTouctionDto); return auctionDto; } public async Task GetAuctionBidDtoByIdAsync(int auctionBidId) { var auctionBid = await _ctx.AuctionBids.GetByIdAsync(auctionBidId); return auctionBid == null ? null : new AuctionBidDto(auctionBid); } public async Task> GetProductToAuctionsByProductIdAsync(int productId) { return new List(await _ctx.ProductToAuctions.GetByProductId(productId).ToListAsync()); } public async Task> GetProductToAuctionByAuctionIdAndProductIdAsync(int auctionId, int productId) { return new List(await _ctx.ProductToAuctions.GetByAuctionAndProductId(auctionId, productId).ToListAsync()); } public async Task AssignProductToAuctionAsync(int productId, decimal startingPrice, decimal bidPrice, int auctionId) { var auction = await GetAuctionDtoByIdAsync(auctionId); if (auction == null) return null; var existedProductToAuction = (await GetProductToAuctionByAuctionIdAndProductIdAsync(auctionId, productId)).FirstOrDefault(); if (existedProductToAuction != null) return existedProductToAuction; var mapping = new ProductToAuctionMapping { ProductId = productId, StartingPrice = startingPrice, BidPrice = bidPrice, ProductAmount = 0, AuctionStatus = Domains.Enums.AuctionStatus.Active, //Ez miért Active alapból? - J. AuctionId = auctionId }; try { await _ctx.ProductToAuctions.InsertAsync(mapping); } catch (Exception ex) { await _logger.InformationAsync(ex.ToString()); } return mapping; } #endregion Dtos }