using Nop.Core; using Nop.Core.Domain.Catalog; using Nop.Core.Domain.Customers; using Nop.Core.Domain.Vendors; using Nop.Data; using Nop.Services.Html; namespace Nop.Services.Vendors; /// /// Vendor service /// public partial class VendorService : IVendorService { #region Fields protected readonly IHtmlFormatter _htmlFormatter; protected readonly IRepository _customerRepository; protected readonly IRepository _productRepository; protected readonly IRepository _vendorRepository; protected readonly IRepository _vendorNoteRepository; #endregion #region Ctor public VendorService(IHtmlFormatter htmlFormatter, IRepository customerRepository, IRepository productRepository, IRepository vendorRepository, IRepository vendorNoteRepository) { _htmlFormatter = htmlFormatter; _customerRepository = customerRepository; _productRepository = productRepository; _vendorRepository = vendorRepository; _vendorNoteRepository = vendorNoteRepository; } #endregion #region Methods /// /// Gets a vendor by vendor identifier /// /// Vendor identifier /// /// A task that represents the asynchronous operation /// The task result contains the vendor /// public virtual async Task GetVendorByIdAsync(int vendorId) { return await _vendorRepository.GetByIdAsync(vendorId, cache => default); } /// /// Gets a vendor by product identifier /// /// Product identifier /// /// A task that represents the asynchronous operation /// The task result contains the vendor /// public virtual async Task GetVendorByProductIdAsync(int productId) { if (productId == 0) return null; return await (from v in _vendorRepository.Table join p in _productRepository.Table on v.Id equals p.VendorId where p.Id == productId select v).FirstOrDefaultAsync(); } /// /// Gets vendors by product identifiers /// /// Array of product identifiers /// /// A task that represents the asynchronous operation /// The task result contains the vendors /// public virtual async Task> GetVendorsByProductIdsAsync(int[] productIds) { ArgumentNullException.ThrowIfNull(productIds); return await (from v in _vendorRepository.Table join p in _productRepository.Table on v.Id equals p.VendorId where productIds.Contains(p.Id) && !v.Deleted && v.Active select v).Distinct().ToListAsync(); } /// /// Gets a vendors by customers identifiers /// /// Array of customer identifiers /// /// A task that represents the asynchronous operation /// The task result contains the vendors /// public virtual async Task> GetVendorsByCustomerIdsAsync(int[] customerIds) { ArgumentNullException.ThrowIfNull(customerIds); return await (from v in _vendorRepository.Table join c in _customerRepository.Table on v.Id equals c.VendorId where customerIds.Contains(c.Id) && !v.Deleted && v.Active select v).Distinct().ToListAsync(); } /// /// Delete a vendor /// /// Vendor /// A task that represents the asynchronous operation public virtual async Task DeleteVendorAsync(Vendor vendor) { await _vendorRepository.DeleteAsync(vendor); } /// /// Gets all vendors /// /// Vendor name /// Vendor email /// Page index /// Page size /// A value indicating whether to show hidden records /// /// A task that represents the asynchronous operation /// The task result contains the vendors /// public virtual async Task> GetAllVendorsAsync(string name = "", string email = "", int pageIndex = 0, int pageSize = int.MaxValue, bool showHidden = false) { var vendors = await _vendorRepository.GetAllPagedAsync(query => { if (!string.IsNullOrWhiteSpace(name)) query = query.Where(v => v.Name.Contains(name)); if (!string.IsNullOrWhiteSpace(email)) query = query.Where(v => v.Email.Contains(email)); if (!showHidden) query = query.Where(v => v.Active); query = query.Where(v => !v.Deleted); query = query.OrderBy(v => v.DisplayOrder).ThenBy(v => v.Name).ThenBy(v => v.Email); return query; }, pageIndex, pageSize); return vendors; } /// /// Inserts a vendor /// /// Vendor /// A task that represents the asynchronous operation public virtual async Task InsertVendorAsync(Vendor vendor) { await _vendorRepository.InsertAsync(vendor); } /// /// Updates the vendor /// /// Vendor /// A task that represents the asynchronous operation public virtual async Task UpdateVendorAsync(Vendor vendor) { await _vendorRepository.UpdateAsync(vendor); } /// /// Gets a vendor note /// /// The vendor note identifier /// /// A task that represents the asynchronous operation /// The task result contains the vendor note /// public virtual async Task GetVendorNoteByIdAsync(int vendorNoteId) { return await _vendorNoteRepository.GetByIdAsync(vendorNoteId, cache => default); } /// /// Gets all vendor notes /// /// Vendor identifier /// Page index /// Page size /// /// A task that represents the asynchronous operation /// The task result contains the vendor notes /// public virtual async Task> GetVendorNotesByVendorAsync(int vendorId, int pageIndex = 0, int pageSize = int.MaxValue) { var query = _vendorNoteRepository.Table.Where(vn => vn.VendorId == vendorId); query = query.OrderBy(v => v.CreatedOnUtc).ThenBy(v => v.Id); return await query.ToPagedListAsync(pageIndex, pageSize); } /// /// Deletes a vendor note /// /// The vendor note /// A task that represents the asynchronous operation public virtual async Task DeleteVendorNoteAsync(VendorNote vendorNote) { await _vendorNoteRepository.DeleteAsync(vendorNote); } /// /// Inserts a vendor note /// /// Vendor note /// A task that represents the asynchronous operation public virtual async Task InsertVendorNoteAsync(VendorNote vendorNote) { await _vendorNoteRepository.InsertAsync(vendorNote); } /// /// Formats the vendor note text /// /// Vendor note /// Formatted text public virtual string FormatVendorNoteText(VendorNote vendorNote) { ArgumentNullException.ThrowIfNull(vendorNote); var text = vendorNote.Note; if (string.IsNullOrEmpty(text)) return string.Empty; text = _htmlFormatter.FormatText(text, false, true, false, false, false, false); return text; } #endregion }