38 lines
1.1 KiB
C#
38 lines
1.1 KiB
C#
using MyNewProjectName.Domain.Entities;
|
|
using MyNewProjectName.Domain.Interfaces;
|
|
using MediatR;
|
|
|
|
namespace MyNewProjectName.Application.Features.Sample.Commands.CreateSample;
|
|
|
|
/// <summary>
|
|
/// Handler for CreateSampleCommand
|
|
/// </summary>
|
|
public class CreateSampleCommandHandler : IRequestHandler<CreateSampleCommand, Guid>
|
|
{
|
|
private readonly IRepository<SampleEntity> _repository;
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
|
|
public CreateSampleCommandHandler(IRepository<SampleEntity> repository, IUnitOfWork unitOfWork)
|
|
{
|
|
_repository = repository;
|
|
_unitOfWork = unitOfWork;
|
|
}
|
|
|
|
public async Task<Guid> Handle(CreateSampleCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var entity = new SampleEntity
|
|
{
|
|
Id = Guid.NewGuid(),
|
|
Name = request.Name,
|
|
Description = request.Description,
|
|
IsActive = true,
|
|
CreatedAt = DateTime.UtcNow
|
|
};
|
|
|
|
await _repository.AddAsync(entity, cancellationToken);
|
|
await _unitOfWork.SaveChangesAsync(cancellationToken);
|
|
|
|
return entity.Id;
|
|
}
|
|
}
|