using Nop.Core; using Nop.Core.Domain.Polls; using Nop.Data; using Nop.Services.Stores; namespace Nop.Services.Polls; /// /// Poll service /// public partial class PollService : IPollService { #region Fields protected readonly IRepository _pollRepository; protected readonly IRepository _pollAnswerRepository; protected readonly IRepository _pollVotingRecordRepository; protected readonly IStoreMappingService _storeMappingService; #endregion #region Ctor public PollService( IRepository pollRepository, IRepository pollAnswerRepository, IRepository pollVotingRecordRepository, IStoreMappingService storeMappingService) { _pollRepository = pollRepository; _pollAnswerRepository = pollAnswerRepository; _pollVotingRecordRepository = pollVotingRecordRepository; _storeMappingService = storeMappingService; } #endregion #region Methods /// /// Gets a poll /// /// The poll identifier /// /// A task that represents the asynchronous operation /// The task result contains the poll /// public virtual async Task GetPollByIdAsync(int pollId) { return await _pollRepository.GetByIdAsync(pollId, cache => default); } /// /// Gets polls /// /// The store identifier; pass 0 to load all records /// Language identifier; pass 0 to load all records /// Whether to show hidden records (not published, not started and expired) /// Retrieve only shown on home page polls /// The poll system keyword; pass null to load all records /// Page index /// Page size /// /// A task that represents the asynchronous operation /// The task result contains the polls /// public virtual async Task> GetPollsAsync(int storeId, int languageId = 0, bool showHidden = false, bool loadShownOnHomepageOnly = false, string systemKeyword = null, int pageIndex = 0, int pageSize = int.MaxValue) { var query = _pollRepository.Table; if (!showHidden || storeId > 0) { //apply store mapping constraints query = await _storeMappingService.ApplyStoreMapping(query, storeId); } //whether to load not published, not started and expired polls if (!showHidden) { var utcNow = DateTime.UtcNow; query = query.Where(poll => poll.Published); query = query.Where(poll => !poll.StartDateUtc.HasValue || poll.StartDateUtc <= utcNow); query = query.Where(poll => !poll.EndDateUtc.HasValue || poll.EndDateUtc >= utcNow); } //load homepage polls only if (loadShownOnHomepageOnly) query = query.Where(poll => poll.ShowOnHomepage); //filter by language if (languageId > 0) query = query.Where(poll => poll.LanguageId == languageId); //filter by system keyword if (!string.IsNullOrEmpty(systemKeyword)) query = query.Where(poll => poll.SystemKeyword == systemKeyword); //order records by display order query = query.OrderBy(poll => poll.DisplayOrder).ThenBy(poll => poll.Id); //return paged list of polls return await query.ToPagedListAsync(pageIndex, pageSize); } /// /// Deletes a poll /// /// The poll /// A task that represents the asynchronous operation public virtual async Task DeletePollAsync(Poll poll) { await _pollRepository.DeleteAsync(poll); } /// /// Inserts a poll /// /// Poll /// A task that represents the asynchronous operation public virtual async Task InsertPollAsync(Poll poll) { await _pollRepository.InsertAsync(poll); } /// /// Updates the poll /// /// Poll /// A task that represents the asynchronous operation public virtual async Task UpdatePollAsync(Poll poll) { await _pollRepository.UpdateAsync(poll); } /// /// Gets a poll answer /// /// Poll answer identifier /// /// A task that represents the asynchronous operation /// The task result contains the poll answer /// public virtual async Task GetPollAnswerByIdAsync(int pollAnswerId) { return await _pollAnswerRepository.GetByIdAsync(pollAnswerId, cache => default); } /// /// Deletes a poll answer /// /// Poll answer /// A task that represents the asynchronous operation public virtual async Task DeletePollAnswerAsync(PollAnswer pollAnswer) { await _pollAnswerRepository.DeleteAsync(pollAnswer); } /// /// Gets a poll answers by parent poll /// /// The poll identifier /// Poll answer /// Page index /// Page size /// A task that represents the asynchronous operation public virtual async Task> GetPollAnswerByPollAsync(int pollId, int pageIndex = 0, int pageSize = int.MaxValue) { var query = _pollAnswerRepository.Table.Where(pa => pa.PollId == pollId); //order records by display order query = query.OrderBy(pa => pa.DisplayOrder).ThenBy(pa => pa.Id); //return paged list of polls return await query.ToPagedListAsync(pageIndex, pageSize); } /// /// Inserts a poll answer /// /// Poll answer /// A task that represents the asynchronous operation public virtual async Task InsertPollAnswerAsync(PollAnswer pollAnswer) { await _pollAnswerRepository.InsertAsync(pollAnswer); } /// /// Updates the poll answer /// /// Poll answer /// A task that represents the asynchronous operation public virtual async Task UpdatePollAnswerAsync(PollAnswer pollAnswer) { await _pollAnswerRepository.UpdateAsync(pollAnswer); } /// /// Gets a value indicating whether customer already voted for this poll /// /// Poll identifier /// Customer identifier /// /// A task that represents the asynchronous operation /// The task result contains the result /// public virtual async Task AlreadyVotedAsync(int pollId, int customerId) { if (pollId == 0 || customerId == 0) return false; var result = await (from pa in _pollAnswerRepository.Table join pvr in _pollVotingRecordRepository.Table on pa.Id equals pvr.PollAnswerId where pa.PollId == pollId && pvr.CustomerId == customerId select pvr) .AnyAsync(); return result; } /// /// Inserts a poll voting record /// /// Voting record /// A task that represents the asynchronous operation public virtual async Task InsertPollVotingRecordAsync(PollVotingRecord pollVotingRecord) { await _pollVotingRecordRepository.InsertAsync(pollVotingRecord); } /// /// Gets a poll voting records by parent answer /// /// Poll answer identifier /// Poll answer /// Page index /// Page size /// A task that represents the asynchronous operation public virtual async Task> GetPollVotingRecordsByPollAnswerAsync(int pollAnswerId, int pageIndex = 0, int pageSize = int.MaxValue) { var query = _pollVotingRecordRepository.Table.Where(pa => pa.PollAnswerId == pollAnswerId); //return paged list of poll voting records return await query.ToPagedListAsync(pageIndex, pageSize); } #endregion }