initial commit
This commit is contained in:
@@ -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()
|
||||
.IsDeletedEqualTo(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