56 lines
2.1 KiB
C#
56 lines
2.1 KiB
C#
using MediatR;
|
|
using Microsoft.Extensions.Logging;
|
|
using TakeoutSaaS.Application.App.Merchants.Commands;
|
|
using TakeoutSaaS.Application.App.Merchants.Dto;
|
|
using TakeoutSaaS.Domain.Merchants.Entities;
|
|
using TakeoutSaaS.Domain.Merchants.Repositories;
|
|
|
|
namespace TakeoutSaaS.Application.App.Merchants.Handlers;
|
|
|
|
/// <summary>
|
|
/// 创建商户命令处理器。
|
|
/// </summary>
|
|
public sealed class CreateMerchantCommandHandler(IMerchantRepository merchantRepository, ILogger<CreateMerchantCommandHandler> logger)
|
|
: IRequestHandler<CreateMerchantCommand, MerchantDto>
|
|
{
|
|
private readonly IMerchantRepository _merchantRepository = merchantRepository;
|
|
private readonly ILogger<CreateMerchantCommandHandler> _logger = logger;
|
|
|
|
/// <inheritdoc />
|
|
public async Task<MerchantDto> Handle(CreateMerchantCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var merchant = new Merchant
|
|
{
|
|
BrandName = request.BrandName.Trim(),
|
|
BrandAlias = request.BrandAlias?.Trim(),
|
|
LogoUrl = request.LogoUrl?.Trim(),
|
|
Category = request.Category?.Trim(),
|
|
ContactPhone = request.ContactPhone.Trim(),
|
|
ContactEmail = request.ContactEmail?.Trim(),
|
|
Status = request.Status,
|
|
JoinedAt = DateTime.UtcNow
|
|
};
|
|
|
|
await _merchantRepository.AddMerchantAsync(merchant, cancellationToken);
|
|
await _merchantRepository.SaveChangesAsync(cancellationToken);
|
|
|
|
_logger.LogInformation("创建商户 {MerchantId} - {BrandName}", merchant.Id, merchant.BrandName);
|
|
return MapToDto(merchant);
|
|
}
|
|
|
|
private static MerchantDto MapToDto(Merchant merchant) => new()
|
|
{
|
|
Id = merchant.Id,
|
|
TenantId = merchant.TenantId,
|
|
BrandName = merchant.BrandName,
|
|
BrandAlias = merchant.BrandAlias,
|
|
LogoUrl = merchant.LogoUrl,
|
|
Category = merchant.Category,
|
|
ContactPhone = merchant.ContactPhone,
|
|
ContactEmail = merchant.ContactEmail,
|
|
Status = merchant.Status,
|
|
JoinedAt = merchant.JoinedAt,
|
|
CreatedAt = merchant.CreatedAt
|
|
};
|
|
}
|