Skip to content

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.

2.2 KB
Raw Blame
using Grand.Business.Core.Interfaces.System.Admin;
using Grand.Data;
using Grand.Domain.Admin;
using Grand.Infrastructure.Caching;
using Grand.Infrastructure.Caching.Constants;
using Grand.Infrastructure.Extensions;
using Grand.Mediator;

namespace Grand.Web.Common.Menu;

public class AdminSiteMapService : IAdminSiteMapService
{
    private readonly IRepository<AdminSiteMap> _adminSiteMapRepository;
    private readonly ICacheBase _cacheBase;
    private readonly IMediator _mediator;

    public AdminSiteMapService(
        IRepository<AdminSiteMap> adminSiteMapRepository,
        ICacheBase cacheBase,
        IMediator mediator)
    {
        _adminSiteMapRepository = adminSiteMapRepository;
        _cacheBase = cacheBase;
        _mediator = mediator;
    }

    public virtual async Task<IList<AdminSiteMap>> GetSiteMap()
    {
        return await _cacheBase.GetAsync(CacheKey.ADMIN_SITEMAP_KEY, async () =>
        {
            var query = from c in _adminSiteMapRepository.Table
                orderby c.DisplayOrder
                select c;

            return await _adminSiteMapRepository.ToListAsync(query);
        });
    }

    public virtual async Task InsertSiteMap(AdminSiteMap entity)
    {
        ArgumentNullException.ThrowIfNull(entity);

        await _adminSiteMapRepository.InsertAsync(entity);

        //clear cache
        await _cacheBase.RemoveByPrefix(CacheKey.ADMIN_SITEMAP_PATTERN_KEY);

        //event notification
        await _mediator.EntityInserted(entity);
    }

    public virtual async Task UpdateSiteMap(AdminSiteMap entity)
    {
        ArgumentNullException.ThrowIfNull(entity);

        await _adminSiteMapRepository.UpdateAsync(entity);

        //clear cache
        await _cacheBase.RemoveByPrefix(CacheKey.ADMIN_SITEMAP_PATTERN_KEY);

        //event notification
        await _mediator.EntityUpdated(entity);
    }

    public virtual async Task DeleteSiteMap(AdminSiteMap entity)
    {
        ArgumentNullException.ThrowIfNull(entity);

        await _adminSiteMapRepository.DeleteAsync(entity);

        //clear cache
        await _cacheBase.RemoveByPrefix(CacheKey.ADMIN_SITEMAP_PATTERN_KEY);

        //event notification
        await _mediator.EntityDeleted(entity);
    }
}