initial commit
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.TodoItemManager.Commands;
|
||||
|
||||
public class CreateTodoItemResult
|
||||
{
|
||||
public TodoItem? Data { get; set; }
|
||||
}
|
||||
|
||||
public class CreateTodoItemRequest : IRequest<CreateTodoItemResult>
|
||||
{
|
||||
public string? Name { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public string? TodoId { get; init; }
|
||||
public string? CreatedById { get; init; }
|
||||
}
|
||||
|
||||
public class CreateTodoItemValidator : AbstractValidator<CreateTodoItemRequest>
|
||||
{
|
||||
public CreateTodoItemValidator()
|
||||
{
|
||||
RuleFor(x => x.Name).NotEmpty();
|
||||
RuleFor(x => x.TodoId).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class CreateTodoItemHandler : IRequestHandler<CreateTodoItemRequest, CreateTodoItemResult>
|
||||
{
|
||||
private readonly ICommandRepository<TodoItem> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public CreateTodoItemHandler(
|
||||
ICommandRepository<TodoItem> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<CreateTodoItemResult> Handle(CreateTodoItemRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var entity = new TodoItem();
|
||||
entity.CreatedById = request.CreatedById;
|
||||
|
||||
entity.Name = request.Name;
|
||||
entity.Description = request.Description;
|
||||
entity.TodoId = request.TodoId;
|
||||
|
||||
await _repository.CreateAsync(entity, cancellationToken);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new CreateTodoItemResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.TodoItemManager.Commands;
|
||||
|
||||
public class DeleteTodoItemResult
|
||||
{
|
||||
public TodoItem? Data { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteTodoItemRequest : IRequest<DeleteTodoItemResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? DeletedById { get; init; }
|
||||
}
|
||||
|
||||
public class DeleteTodoItemValidator : AbstractValidator<DeleteTodoItemRequest>
|
||||
{
|
||||
public DeleteTodoItemValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class DeleteTodoItemHandler : IRequestHandler<DeleteTodoItemRequest, DeleteTodoItemResult>
|
||||
{
|
||||
private readonly ICommandRepository<TodoItem> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public DeleteTodoItemHandler(
|
||||
ICommandRepository<TodoItem> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<DeleteTodoItemResult> Handle(DeleteTodoItemRequest 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 DeleteTodoItemResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
using Application.Common.Repositories;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
|
||||
namespace Application.Features.TodoItemManager.Commands;
|
||||
|
||||
public class UpdateTodoItemResult
|
||||
{
|
||||
public TodoItem? Data { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateTodoItemRequest : IRequest<UpdateTodoItemResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public string? TodoId { get; init; }
|
||||
public string? UpdatedById { get; init; }
|
||||
}
|
||||
|
||||
public class UpdateTodoItemValidator : AbstractValidator<UpdateTodoItemRequest>
|
||||
{
|
||||
public UpdateTodoItemValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
RuleFor(x => x.Name).NotEmpty();
|
||||
RuleFor(x => x.TodoId).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class UpdateTodoItemHandler : IRequestHandler<UpdateTodoItemRequest, UpdateTodoItemResult>
|
||||
{
|
||||
private readonly ICommandRepository<TodoItem> _repository;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
|
||||
public UpdateTodoItemHandler(
|
||||
ICommandRepository<TodoItem> repository,
|
||||
IUnitOfWork unitOfWork
|
||||
)
|
||||
{
|
||||
_repository = repository;
|
||||
_unitOfWork = unitOfWork;
|
||||
}
|
||||
|
||||
public async Task<UpdateTodoItemResult> Handle(UpdateTodoItemRequest 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;
|
||||
entity.TodoId = request.TodoId;
|
||||
|
||||
_repository.Update(entity);
|
||||
await _unitOfWork.SaveAsync(cancellationToken);
|
||||
|
||||
return new UpdateTodoItemResult
|
||||
{
|
||||
Data = entity
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
using Application.Common.CQS.Queries;
|
||||
using Application.Common.Extensions;
|
||||
using AutoMapper;
|
||||
using Domain.Entities;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Application.Features.TodoItemManager.Queries;
|
||||
|
||||
public record GetTodoItemListDto
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public string? TodoId { get; init; }
|
||||
public string? TodoName { get; init; }
|
||||
public DateTime? CreatedAtUtc { get; init; }
|
||||
}
|
||||
|
||||
public class GetTodoItemListProfile : Profile
|
||||
{
|
||||
public GetTodoItemListProfile()
|
||||
{
|
||||
CreateMap<TodoItem, GetTodoItemListDto>()
|
||||
.ForMember(
|
||||
dest => dest.TodoName,
|
||||
opt => opt.MapFrom(src => src.Todo != null ? src.Todo.Name : string.Empty)
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public class GetTodoItemListResult
|
||||
{
|
||||
public List<GetTodoItemListDto>? Data { get; init; }
|
||||
}
|
||||
|
||||
public class GetTodoItemListRequest : IRequest<GetTodoItemListResult>
|
||||
{
|
||||
public bool IsDeleted { get; init; } = false;
|
||||
}
|
||||
|
||||
|
||||
public class GetTodoItemListHandler : IRequestHandler<GetTodoItemListRequest, GetTodoItemListResult>
|
||||
{
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IQueryContext _context;
|
||||
|
||||
public GetTodoItemListHandler(IMapper mapper, IQueryContext context)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetTodoItemListResult> Handle(GetTodoItemListRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context
|
||||
.TodoItem
|
||||
.AsNoTracking()
|
||||
.ApplyIsDeletedFilter(request.IsDeleted)
|
||||
.Include(x => x.Todo)
|
||||
.AsQueryable();
|
||||
|
||||
var entities = await query.ToListAsync(cancellationToken);
|
||||
|
||||
var dtos = _mapper.Map<List<GetTodoItemListDto>>(entities);
|
||||
|
||||
return new GetTodoItemListResult
|
||||
{
|
||||
Data = dtos
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
using Application.Common.CQS.Queries;
|
||||
using AutoMapper;
|
||||
using Domain.Entities;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Application.Features.TodoItemManager.Queries;
|
||||
|
||||
public record GetTodoItemSingleDto
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
public string? Name { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public string? TodoName { get; init; }
|
||||
}
|
||||
|
||||
public class GetTodoItemSingleProfile : Profile
|
||||
{
|
||||
public GetTodoItemSingleProfile()
|
||||
{
|
||||
CreateMap<TodoItem, GetTodoItemSingleDto>()
|
||||
.ForMember(
|
||||
dest => dest.TodoName,
|
||||
opt => opt.MapFrom(src => src.Todo != null ? src.Todo.Name : string.Empty)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public class GetTodoItemSingleResult
|
||||
{
|
||||
public GetTodoItemSingleDto? Data { get; init; }
|
||||
}
|
||||
|
||||
public class GetTodoItemSingleRequest : IRequest<GetTodoItemSingleResult>
|
||||
{
|
||||
public string? Id { get; init; }
|
||||
}
|
||||
|
||||
public class GetTodoItemSingleValidator : AbstractValidator<GetTodoItemSingleRequest>
|
||||
{
|
||||
public GetTodoItemSingleValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class GetTodoItemSingleHandler : IRequestHandler<GetTodoItemSingleRequest, GetTodoItemSingleResult>
|
||||
{
|
||||
private readonly IQueryContext _context;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public GetTodoItemSingleHandler(
|
||||
IQueryContext context,
|
||||
IMapper mapper
|
||||
)
|
||||
{
|
||||
_context = context;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<GetTodoItemSingleResult> Handle(GetTodoItemSingleRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _context
|
||||
.TodoItem
|
||||
.AsNoTracking()
|
||||
.Include(x => x.Todo)
|
||||
.AsQueryable();
|
||||
|
||||
query = query
|
||||
.Where(x => x.Id == request.Id);
|
||||
|
||||
var entity = await query.SingleOrDefaultAsync(cancellationToken);
|
||||
|
||||
var dto = _mapper.Map<GetTodoItemSingleDto>(entity);
|
||||
|
||||
return new GetTodoItemSingleResult
|
||||
{
|
||||
Data = dto
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user