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

246 lines
8.8 KiB
C#

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;
/// <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 IRepository<ProductToAuctionMapping> _productToAuctionRepository;
protected readonly IShortTermCacheManager _shortTermCacheManager;
protected readonly IStaticCacheManager _staticCacheManager;
protected readonly IWorkContext _workContext;
protected readonly ILogger _logger;
#endregion
#region Ctor
/// <summary>
/// Ctor
/// </summary>
/// <param name="ctx"></param>
/// <param name="shortTermCacheManager">Short term cache manager</param>
/// <param name="staticCacheManager">Cache manager</param>
public AuctionService(
AuctionDbContext ctx,
//IRepository<Auction> auctionRepository,
//IRepository<ProductToAuctionMapping> 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<bool> ValidateAuctionBid(AuctionBid auctionBid)
{
auctionBid.CustomerId = (await _workContext.GetCurrentCustomerAsync()).Id; //elvileg megnézi cache-ben, csak utána DB-zik! - J.
//etc...
return true;
}
/// <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)
{
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);
}
}
/// <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<IList<Auction>> GetAllAuctionsAsync()
{
return await _ctx.Auctions.GetAllAuctionsAsync();
}
public async Task<List<ProductToAuctionMapping>> 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<AuctionDto> GetAuctionDtoByIdAsync(int auctionId)
{
var auction = await _ctx.Auctions.GetByIdAsync(auctionId);
return auction == null ? null : new AuctionDto(auction);
}
public async Task<AuctionDto> 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<ProductToAuctionDto> GetProductToAuctionDtoByIdAsync(int productToAuctionId)
{
var productToAuction = await _ctx.ProductToAuctions.GetByIdAsync(productToAuctionId);
return productToAuction == null ? null : new ProductToAuctionDto(productToAuction);
}
public async Task<AuctionDto> 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<AuctionBidDto> GetAuctionBidDtoByIdAsync(int auctionBidId)
{
var auctionBid = await _ctx.AuctionBids.GetByIdAsync(auctionBidId);
return auctionBid == null ? null : new AuctionBidDto(auctionBid);
}
public async Task<List<ProductToAuctionMapping>> GetProductToAuctionsByProductIdAsync(int productId)
{
return new List<ProductToAuctionMapping>(await _ctx.ProductToAuctions.GetByProductId(productId).ToListAsync());
}
public async Task<List<ProductToAuctionMapping>> GetProductToAuctionByAuctionIdAndProductIdAsync(int auctionId, int productId)
{
return new List<ProductToAuctionMapping>(await _ctx.ProductToAuctions.GetByAuctionAndProductId(auctionId, productId).ToListAsync());
}
public async Task<ProductToAuctionMapping> 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
}