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.1 KB
Raw Blame
using Grand.Business.Core.Interfaces.Customers;
using Grand.Data;
using Grand.Domain.Customers;
using Grand.Infrastructure.Extensions;
using Grand.Mediator;

namespace Grand.Business.Customers.Services;

public class CustomerHistoryPasswordService : ICustomerHistoryPasswordService
{
    private readonly IRepository<CustomerHistoryPassword> _customerHistoryPasswordProductRepository;
    private readonly IMediator _mediator;

    public CustomerHistoryPasswordService(IRepository<CustomerHistoryPassword> customerHistoryPasswordProductRepository,
        IMediator mediator)
    {
        _customerHistoryPasswordProductRepository = customerHistoryPasswordProductRepository;
        _mediator = mediator;
    }

    /// <summary>
    ///     Insert a customer history password
    /// </summary>
    /// <param name="customer">Customer</param>
    public virtual async Task InsertCustomerPassword(Customer customer)
    {
        ArgumentNullException.ThrowIfNull(customer);

        var chp = new CustomerHistoryPassword {
            Password = customer.Password,
            PasswordFormatId = customer.PasswordFormatId,
            PasswordSalt = customer.PasswordSalt,
            CustomerId = customer.Id
        };

        await _customerHistoryPasswordProductRepository.InsertAsync(chp);

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

    /// <summary>
    ///     Gets customer passwords
    /// </summary>
    /// <param name="customerId">Customer identifier; pass null to load all records</param>
    /// <param name="passwordsToReturn">Number of returning passwords; pass null to load all records</param>
    /// <returns>List of customer passwords</returns>
    public virtual async Task<IList<CustomerHistoryPassword>> GetPasswords(string customerId, int passwordsToReturn)
    {
        return await _customerHistoryPasswordProductRepository.ToListAsync(
            _customerHistoryPasswordProductRepository
                .Table
                .Where(x => x.CustomerId == customerId)
                .OrderByDescending(password => password.CreatedOnUtc)
                .Take(passwordsToReturn));
    }
}