Files
yes.blog/Yes.Application/Admins/Pages/GetPageQueryHandler.cs
2025-04-30 09:51:36 +08:00

47 lines
1.2 KiB
C#

namespace Yes.Application.Admins.Pages
{
public record GetPageQuery(int Id) : IRequest<GetPageQueryResponse>;
public record GetPageQueryResponse(
int Id,
string Title,
int CategoryId,
string Content,
string Slug,
string CoverUrl,
string? Summary,
string[] Tags,
int Sort
);
public class GetPageQueryHandler(BlogDbContext db,IMapper mapper) : IRequestHandler<GetPageQuery, GetPageQueryResponse>
{
private readonly BlogDbContext _db = db;
private readonly IMapper _mapper = mapper;
public async Task<GetPageQueryResponse> Handle(GetPageQuery request, CancellationToken cancellationToken)
{
var page = await _db.Articles.FindAsync(request.Id);
if (page == null)
{
throw new PageNotExistsException(request.Id);
}
var response = _mapper.Map<GetPageQueryResponse>(page);
return response;
}
}
internal class CreatePageMappingProfile : IRegister
{
public void Register(TypeAdapterConfig config)
{
config.ForType<ArticleEntity, GetPageQueryResponse>()
.Map(dest => dest.Tags, src => src.Tag.ToTags()); ;
}
}
}