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.

3.9 KB
Raw Blame
using Grand.Business.Core.Interfaces.Catalog.Collections;
using Grand.Data;
using Grand.Domain.Catalog;
using Grand.Infrastructure.Caching;
using Grand.Infrastructure.Caching.Constants;
using Grand.Infrastructure.Extensions;
using Grand.Mediator;

namespace Grand.Business.Catalog.Services.Collections;

/// <summary>
///     Collection layout service
/// </summary>
public class CollectionLayoutService : ICollectionLayoutService
{
    #region Ctor

    /// <summary>
    ///     Ctor
    /// </summary>
    /// <param name="collectionLayoutRepository">Collection layout repository</param>
    /// <param name="cacheBase">Cache base</param>
    /// <param name="mediator">Mediator</param>
    public CollectionLayoutService(IRepository<CollectionLayout> collectionLayoutRepository,
        ICacheBase cacheBase,
        IMediator mediator)
    {
        _collectionLayoutRepository = collectionLayoutRepository;
        _cacheBase = cacheBase;
        _mediator = mediator;
    }

    #endregion

    #region Fields

    private readonly IRepository<CollectionLayout> _collectionLayoutRepository;
    private readonly ICacheBase _cacheBase;
    private readonly IMediator _mediator;

    #endregion

    #region Methods

    /// <summary>
    ///     Gets all collection layouts
    /// </summary>
    /// <returns>Collection layouts</returns>
    public virtual async Task<IList<CollectionLayout>> GetAllCollectionLayouts()
    {
        return await _cacheBase.GetAsync(CacheKey.COLLECTION_LAYOUT_ALL, async () =>
        {
            var query = from pt in _collectionLayoutRepository.Table
                orderby pt.DisplayOrder
                select pt;
            return await _collectionLayoutRepository.ToListAsync(query);
        });
    }

    /// <summary>
    ///     Gets a collection layout
    /// </summary>
    /// <param name="collectionLayoutId">Collection layout id</param>
    /// <returns>Collection layout</returns>
    public virtual Task<CollectionLayout> GetCollectionLayoutById(string collectionLayoutId)
    {
        var key = string.Format(CacheKey.COLLECTION_LAYOUT_BY_ID_KEY, collectionLayoutId);
        return _cacheBase.GetAsync(key, () => _collectionLayoutRepository.GetByIdAsync(collectionLayoutId));
    }

    /// <summary>
    ///     Inserts collection layout
    /// </summary>
    /// <param name="collectionLayout">Collection layout</param>
    public virtual async Task InsertCollectionLayout(CollectionLayout collectionLayout)
    {
        ArgumentNullException.ThrowIfNull(collectionLayout);

        await _collectionLayoutRepository.InsertAsync(collectionLayout);

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

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

    /// <summary>
    ///     Updates the collection layout
    /// </summary>
    /// <param name="collectionLayout">Collection layout</param>
    public virtual async Task UpdateCollectionLayout(CollectionLayout collectionLayout)
    {
        ArgumentNullException.ThrowIfNull(collectionLayout);

        await _collectionLayoutRepository.UpdateAsync(collectionLayout);

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

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

    /// <summary>
    ///     Delete collection layout
    /// </summary>
    /// <param name="collectionLayout">Collection layout</param>
    public virtual async Task DeleteCollectionLayout(CollectionLayout collectionLayout)
    {
        ArgumentNullException.ThrowIfNull(collectionLayout);

        await _collectionLayoutRepository.DeleteAsync(collectionLayout);

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

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

    #endregion
}