first commit
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
using MediatR;
|
||||
|
||||
namespace MyNewProjectName.Application.Features.Sample.Commands.CreateSample;
|
||||
|
||||
/// <summary>
|
||||
/// Command to create a new sample
|
||||
/// </summary>
|
||||
public record CreateSampleCommand(string Name, string? Description) : IRequest<Guid>;
|
||||
@@ -0,0 +1,37 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace MyNewProjectName.Application.Features.Sample.Commands.CreateSample;
|
||||
|
||||
/// <summary>
|
||||
/// Validator for CreateSampleCommand
|
||||
/// </summary>
|
||||
public class CreateSampleCommandValidator : AbstractValidator<CreateSampleCommand>
|
||||
{
|
||||
public CreateSampleCommandValidator()
|
||||
{
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Name is required.")
|
||||
.MaximumLength(200).WithMessage("Name must not exceed 200 characters.");
|
||||
|
||||
RuleFor(x => x.Description)
|
||||
.MaximumLength(1000).WithMessage("Description must not exceed 1000 characters.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using MediatR;
|
||||
|
||||
namespace MyNewProjectName.Application.Features.Sample.Queries.GetSamples;
|
||||
|
||||
/// <summary>
|
||||
/// Query to get all samples
|
||||
/// </summary>
|
||||
public record GetSamplesQuery : IRequest<List<SampleDto>>;
|
||||
@@ -0,0 +1,27 @@
|
||||
using AutoMapper;
|
||||
using MyNewProjectName.Domain.Entities;
|
||||
using MyNewProjectName.Domain.Interfaces;
|
||||
using MediatR;
|
||||
|
||||
namespace MyNewProjectName.Application.Features.Sample.Queries.GetSamples;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for GetSamplesQuery
|
||||
/// </summary>
|
||||
public class GetSamplesQueryHandler : IRequestHandler<GetSamplesQuery, List<SampleDto>>
|
||||
{
|
||||
private readonly IRepository<SampleEntity> _repository;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public GetSamplesQueryHandler(IRepository<SampleEntity> repository, IMapper mapper)
|
||||
{
|
||||
_repository = repository;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<List<SampleDto>> Handle(GetSamplesQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entities = await _repository.GetAllAsync(cancellationToken);
|
||||
return _mapper.Map<List<SampleDto>>(entities);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using AutoMapper;
|
||||
using MyNewProjectName.Application.Mappings;
|
||||
using MyNewProjectName.Domain.Entities;
|
||||
|
||||
namespace MyNewProjectName.Application.Features.Sample.Queries.GetSamples;
|
||||
|
||||
/// <summary>
|
||||
/// Sample DTO
|
||||
/// </summary>
|
||||
public class SampleDto : IMapFrom<SampleEntity>
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string? Description { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
|
||||
public void Mapping(Profile profile)
|
||||
{
|
||||
profile.CreateMap<SampleEntity, SampleDto>();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user