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
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
using Application.Common.CQS.Queries;
|
||||
using Application.Common.Extensions;
|
||||
using AutoMapper;
|
||||
using Domain.Entities;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Application.Features.TodoManager.Queries;
|
||||
|
||||
public record GetTodoListDto
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public DateTime? CreatedAtUtc { get; init; }
|
||||
}
|
||||
|
||||
public class GetTodoListProfile : Profile
|
||||
{
|
||||
public GetTodoListProfile()
|
||||
{
|
||||
CreateMap<Todo, GetTodoListDto>();
|
||||
}
|
||||
}
|
||||
|
||||
public class GetTodoListResult
|
||||
{
|
||||
public List<GetTodoListDto>? Data { get; init; }
|
||||
}
|
||||
|
||||
public class GetTodoListRequest : IRequest<GetTodoListResult>
|
||||
{
|
||||
public bool IsDeleted { get; init; } = false;
|
||||
}
|
||||
|
||||
|
||||
public class GetTodoListHandler : IRequestHandler<GetTodoListRequest, GetTodoListResult>
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IQueryContext _context;
|
||||
|
||||
public GetTodoListHandler(IMapper mapper, IQueryContext context)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetTodoListResult> Handle(GetTodoListRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context
|
||||
.Todo
|
||||
.AsNoTracking()
|
||||
.ApplyIsDeletedFilter(request.IsDeleted)
|
||||
.AsQueryable();
|
||||
|
||||
var entities = await query.ToListAsync(cancellationToken);
|
||||
|
||||
var dtos = _mapper.Map<List<GetTodoListDto>>(entities);
|
||||
|
||||
return new GetTodoListResult
|
||||
{
|
||||
Data = dtos
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
using Application.Common.CQS.Queries;
|
||||
using AutoMapper;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Application.Features.TodoManager.Queries;
|
||||
|
||||
public record GetTodoSingleDto
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; init; }
|
||||
public string? Description { get; init; }
|
||||
}
|
||||
|
||||
public class GetTodoSingleProfile : Profile
|
||||
{
|
||||
public GetTodoSingleProfile()
|
||||
{
|
||||
CreateMap<Todo, GetTodoSingleDto>();
|
||||
}
|
||||
}
|
||||
|
||||
public class GetTodoSingleResult
|
||||
{
|
||||
public GetTodoSingleDto? Data { get; init; }
|
||||
}
|
||||
|
||||
public class GetTodoSingleRequest : IRequest<GetTodoSingleResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
}
|
||||
|
||||
public class GetTodoSingleValidator : AbstractValidator<GetTodoSingleRequest>
|
||||
{
|
||||
public GetTodoSingleValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class GetTodoSingleHandler : IRequestHandler<GetTodoSingleRequest, GetTodoSingleResult>
|
||||
{
|
||||
private readonly IQueryContext _context;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public GetTodoSingleHandler(
|
||||
IQueryContext context,
|
||||
IMapper mapper
|
||||
)
|
||||
{
|
||||
_context = context;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<GetTodoSingleResult> Handle(GetTodoSingleRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context
|
||||
.Todo
|
||||
.AsNoTracking()
|
||||
.AsQueryable();
|
||||
|
||||
query = query
|
||||
.Where(x => x.Id == request.Id);
|
||||
|
||||
var entity = await query.SingleOrDefaultAsync(cancellationToken);
|
||||
|
||||
var dto = _mapper.Map<GetTodoSingleDto>(entity);
|
||||
|
||||
return new GetTodoSingleResult
|
||||
{
|
||||
Data = dto
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user