Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
C# aspnet-corecsharpdockerdotnetecommerceecommerce-platformmongodbmulti-storemulti-vendornosqlonline-shopshoppingcart
using Grand.Business.Core.Interfaces.Cms;
using Grand.Business.Core.Interfaces.Common.Security;
using Grand.Data;
using Grand.Domain.Customers;
using Grand.Domain.Pages;
using Grand.Infrastructure;
using Grand.Infrastructure.Caching;
using Grand.Infrastructure.Caching.Constants;
using Grand.Infrastructure.Configuration;
using Grand.Infrastructure.Extensions;
using Grand.Mediator;
namespace Grand.Business.Cms.Services;
/// <summary>
/// Page service
/// </summary>
public class PageService : IPageService
{
#region Ctor
public PageService(IRepository<Page> pageRepository,
IContextAccessor contextAccessor,
IAclService aclService,
IMediator mediator,
ICacheBase cacheBase, AccessControlConfig accessControlConfig)
{
_pageRepository = pageRepository;
_contextAccessor = contextAccessor;
_aclService = aclService;
_mediator = mediator;
_cacheBase = cacheBase;
_accessControlConfig = accessControlConfig;
}
#endregion
#region Fields
private readonly IRepository<Page> _pageRepository;
private readonly IContextAccessor _contextAccessor;
private readonly IAclService _aclService;
private readonly IMediator _mediator;
private readonly ICacheBase _cacheBase;
private readonly AccessControlConfig _accessControlConfig;
#endregion
#region Methods
/// <summary>
/// Gets a page
/// </summary>
/// <param name="pageId">The page identifier</param>
/// <returns>Page</returns>
public virtual Task<Page> GetPageById(string pageId)
{
var key = string.Format(CacheKey.PAGES_BY_ID_KEY, pageId);
return _cacheBase.GetAsync(key, () => _pageRepository.GetByIdAsync(pageId));
}
/// <summary>
/// Gets a page
/// </summary>
/// <param name="systemName">The page system name</param>
/// <param name="storeId">Store identifier; pass "" to ignore filtering by store and load the first one</param>
/// <returns>
/// The page a store overrode this system name with, if it has one; otherwise the page shared by every store
/// </returns>
public virtual async Task<Page> GetPageBySystemName(string systemName, string storeId)
{
if (string.IsNullOrEmpty(systemName))
return null;
var key = string.Format(CacheKey.PAGES_BY_SYSTEMNAME, systemName, storeId);
return await _cacheBase.GetAsync(key, async () =>
{
var query = from p in _pageRepository.Table
select p;
query = query.Where(t => t.SystemName.ToLower() == systemName.ToLower());
query = query.OrderBy(t => t.Id);
IEnumerable<Page> pages = await _pageRepository.ToListAsync(query);
if (!string.IsNullOrEmpty(storeId))
//a page this store was given for itself is the one it means, even though the shared page is older
pages = pages.Where(x => _aclService.Authorize(x, storeId))
.OrderByDescending(x => x.LimitedToStores && x.Stores.Contains(storeId));
return pages.FirstOrDefault();
});
}
/// <summary>
/// Gets all pages
/// </summary>
/// <param name="storeId">Store identifier; pass "" to load all records</param>
/// <param name="ignoreAcl"></param>
/// <returns>Pages</returns>
public virtual async Task<IList<Page>> GetAllPages(string storeId, bool ignoreAcl = false)
{
var key = string.Format(CacheKey.PAGES_ALL_KEY, storeId, ignoreAcl);
return await _cacheBase.GetAsync(key, async () =>
{
var query = from p in _pageRepository.Table
select p;
query = query.OrderBy(t => t.DisplayOrder).ThenBy(t => t.SystemName);
if ((string.IsNullOrEmpty(storeId) || _accessControlConfig.IgnoreStoreLimitations) &&
(ignoreAcl || _accessControlConfig.IgnoreAcl)) return await _pageRepository.ToListAsync(query);
{
if (!ignoreAcl && !_accessControlConfig.IgnoreAcl)
{
var allowedCustomerGroupsIds = _contextAccessor.WorkContext.CurrentCustomer.GetCustomerGroupIds();
query = from p in query
where !p.LimitedToGroups || allowedCustomerGroupsIds.Any(x => p.CustomerGroups.Contains(x))
select p;
}
//Store acl
if (string.IsNullOrEmpty(storeId) || _accessControlConfig.IgnoreStoreLimitations)
return await _pageRepository.ToListAsync(query);
query = from p in query
where !p.LimitedToStores || p.Stores.Contains(storeId)
select p;
query = query.OrderBy(t => t.SystemName);
}
return await _pageRepository.ToListAsync(query);
});
}
/// <summary>
/// Inserts a page
/// </summary>
/// <param name="page">Page</param>
public virtual async Task InsertPage(Page page)
{
ArgumentNullException.ThrowIfNull(page);
await _pageRepository.InsertAsync(page);
//cache
await _cacheBase.RemoveByPrefix(CacheKey.PAGES_PATTERN_KEY);
//event notification
await _mediator.EntityInserted(page);
}
/// <summary>
/// Updates the page
/// </summary>
/// <param name="page">Page</param>
public virtual async Task UpdatePage(Page page)
{
ArgumentNullException.ThrowIfNull(page);
await _pageRepository.UpdateAsync(page);
//cache
await _cacheBase.RemoveByPrefix(CacheKey.PAGES_PATTERN_KEY);
//event notification
await _mediator.EntityUpdated(page);
}
/// <summary>
/// Deletes a page
/// </summary>
/// <param name="page">Page</param>
public virtual async Task DeletePage(Page page)
{
ArgumentNullException.ThrowIfNull(page);
await _pageRepository.DeleteAsync(page);
//cache
await _cacheBase.RemoveByPrefix(CacheKey.PAGES_PATTERN_KEY);
//event notification
await _mediator.EntityDeleted(page);
}
#endregion
}