using Nop.Core.Domain.Stores; using Nop.Data; namespace Nop.Services.Stores; /// /// Store service /// public partial class StoreService : IStoreService { #region Fields protected readonly IRepository _storeRepository; private static readonly char[] _separator = [',']; #endregion #region Ctor public StoreService(IRepository storeRepository) { _storeRepository = storeRepository; } #endregion #region Utilities /// /// Parse comma-separated Hosts /// /// Store /// Comma-separated hosts protected virtual string[] ParseHostValues(Store store) { ArgumentNullException.ThrowIfNull(store); var parsedValues = new List(); if (string.IsNullOrEmpty(store.Hosts)) return parsedValues.ToArray(); var hosts = store.Hosts.Split(_separator, StringSplitOptions.RemoveEmptyEntries); foreach (var host in hosts) { var tmp = host.Trim(); if (!string.IsNullOrEmpty(tmp)) parsedValues.Add(tmp); } return parsedValues.ToArray(); } #endregion #region Methods /// /// Deletes a store /// /// Store /// A task that represents the asynchronous operation public virtual async Task DeleteStoreAsync(Store store) { ArgumentNullException.ThrowIfNull(store); var allStores = await GetAllStoresAsync(); if (allStores.Count == 1) throw new Exception("You cannot delete the only configured store"); await _storeRepository.DeleteAsync(store); } /// /// Gets all stores /// /// /// A task that represents the asynchronous operation /// The task result contains the stores /// public virtual async Task> GetAllStoresAsync() { return await _storeRepository.GetAllAsync(query => { return from s in query orderby s.DisplayOrder, s.Id select s; }, _ => default, includeDeleted: false); } /// /// Gets all stores /// /// /// The stores /// public virtual IList GetAllStores() { return _storeRepository.GetAll(query => { return from s in query orderby s.DisplayOrder, s.Id select s; }, _ => default, includeDeleted: false); } /// /// Gets a store /// /// Store identifier /// /// A task that represents the asynchronous operation /// The task result contains the store /// public virtual async Task GetStoreByIdAsync(int storeId) { return await _storeRepository.GetByIdAsync(storeId, cache => default, false); } /// /// Inserts a store /// /// Store /// A task that represents the asynchronous operation public virtual async Task InsertStoreAsync(Store store) { await _storeRepository.InsertAsync(store); } /// /// Updates the store /// /// Store /// A task that represents the asynchronous operation public virtual async Task UpdateStoreAsync(Store store) { await _storeRepository.UpdateAsync(store); } /// /// Updates the store /// /// Store public virtual void UpdateStore(Store store) { _storeRepository.Update(store); } /// /// Indicates whether a store contains a specified host /// /// Store /// Host /// true - contains, false - no public virtual bool ContainsHostValue(Store store, string host) { ArgumentNullException.ThrowIfNull(store); if (string.IsNullOrEmpty(host)) return false; var contains = ParseHostValues(store).Any(x => x.Equals(host, StringComparison.InvariantCultureIgnoreCase)); return contains; } /// /// Returns a list of names of not existing stores /// /// The names and/or IDs of the store to check /// /// A task that represents the asynchronous operation /// The task result contains the list of names and/or IDs not existing stores /// public async Task GetNotExistingStoresAsync(string[] storeIdsNames) { ArgumentNullException.ThrowIfNull(storeIdsNames); var query = _storeRepository.Table; var queryFilter = storeIdsNames.Distinct().ToArray(); //filtering by name var filter = await query.Select(store => store.Name) .Where(store => queryFilter.Contains(store)) .ToListAsync(); queryFilter = queryFilter.Except(filter).ToArray(); //if some names not found if (!queryFilter.Any()) return queryFilter.ToArray(); //filtering by IDs filter = await query.Select(store => store.Id.ToString()) .Where(store => queryFilter.Contains(store)) .ToListAsync(); queryFilter = queryFilter.Except(filter).ToArray(); return queryFilter.ToArray(); } #endregion }