initial commit
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.TodoManager.Commands;
|
||||
|
||||
public class CreateTodoResult
|
||||
{
|
||||
public Todo? Data { get; set; }
|
||||
}
|
||||
|
||||
public class CreateTodoRequest : IRequest<CreateTodoResult>
|
||||
{
|
||||
public string? Name { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public string? CreatedById { get; init; }
|
||||
}
|
||||
|
||||
public class CreateTodoValidator : AbstractValidator<CreateTodoRequest>
|
||||
{
|
||||
public CreateTodoValidator()
|
||||
{
|
||||
RuleFor(x => x.Name).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class CreateTodoHandler : IRequestHandler<CreateTodoRequest, CreateTodoResult>
|
||||
{
|
||||
private readonly ICommandRepository<Todo> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public CreateTodoHandler(
|
||||
ICommandRepository<Todo> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<CreateTodoResult> Handle(CreateTodoRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = new Todo();
|
||||
entity.CreatedById = request.CreatedById;
|
||||
|
||||
entity.Name = request.Name;
|
||||
entity.Description = request.Description;
|
||||
|
||||
await _repository.CreateAsync(entity, cancellationToken);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new CreateTodoResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.TodoManager.Commands;
|
||||
|
||||
public class DeleteTodoResult
|
||||
{
|
||||
public Todo? Data { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteTodoRequest : IRequest<DeleteTodoResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? DeletedById { get; init; }
|
||||
}
|
||||
|
||||
public class DeleteTodoValidator : AbstractValidator<DeleteTodoRequest>
|
||||
{
|
||||
public DeleteTodoValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class DeleteTodoHandler : IRequestHandler<DeleteTodoRequest, DeleteTodoResult>
|
||||
{
|
||||
private readonly ICommandRepository<Todo> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public DeleteTodoHandler(
|
||||
ICommandRepository<Todo> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<DeleteTodoResult> Handle(DeleteTodoRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
var entity = await _repository.GetAsync(request.Id ?? string.Empty, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
throw new Exception($"Entity not found: {request.Id}");
|
||||
}
|
||||
|
||||
entity.UpdatedById = request.DeletedById;
|
||||
|
||||
_repository.Delete(entity);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new DeleteTodoResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.TodoManager.Commands;
|
||||
|
||||
public class UpdateTodoResult
|
||||
{
|
||||
public Todo? Data { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateTodoRequest : IRequest<UpdateTodoResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public string? UpdatedById { get; init; }
|
||||
}
|
||||
|
||||
public class UpdateTodoValidator : AbstractValidator<UpdateTodoRequest>
|
||||
{
|
||||
public UpdateTodoValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
RuleFor(x => x.Name).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class UpdateTodoHandler : IRequestHandler<UpdateTodoRequest, UpdateTodoResult>
|
||||
{
|
||||
private readonly ICommandRepository<Todo> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public UpdateTodoHandler(
|
||||
ICommandRepository<Todo> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<UpdateTodoResult> Handle(UpdateTodoRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
var entity = await _repository.GetAsync(request.Id ?? string.Empty, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
throw new Exception($"Entity not found: {request.Id}");
|
||||
}
|
||||
|
||||
entity.UpdatedById = request.UpdatedById;
|
||||
|
||||
entity.Name = request.Name;
|
||||
entity.Description = request.Description;
|
||||
|
||||
_repository.Update(entity);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new UpdateTodoResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user