1
This commit is contained in:
45
Yes.Application/Admins/Articles/GetArticleQueryHandler.cs
Normal file
45
Yes.Application/Admins/Articles/GetArticleQueryHandler.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
namespace Yes.Application.Admins.Articles
|
||||
{
|
||||
|
||||
public record GetArticleQuery(int Id) : IRequest<GetArticleQueryResponse>;
|
||||
|
||||
public record GetArticleQueryResponse(
|
||||
int Id,
|
||||
string Title,
|
||||
int CategoryId,
|
||||
string Content,
|
||||
string Slug,
|
||||
string CoverUrl,
|
||||
string? Summary,
|
||||
string[] Tags
|
||||
);
|
||||
|
||||
public class GetArticleQueryHandler(BlogDbContext db,IMapper mapper) : IRequestHandler<GetArticleQuery, GetArticleQueryResponse>
|
||||
{
|
||||
private readonly BlogDbContext _db = db;
|
||||
private readonly IMapper _mapper = mapper;
|
||||
public async Task<GetArticleQueryResponse> Handle(GetArticleQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var article = await _db.Articles.FindAsync(request.Id);
|
||||
if (article == null)
|
||||
{
|
||||
throw new ArticleNotExistsException(request.Id);
|
||||
}
|
||||
|
||||
var response = _mapper.Map<GetArticleQueryResponse>(article);
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
internal class GetArticleMappingProfile : IRegister
|
||||
{
|
||||
public void Register(TypeAdapterConfig config)
|
||||
{
|
||||
config.ForType<ArticleEntity, GetArticleQueryResponse>()
|
||||
.Map(dest => dest.Tags, src => src.Tag.ToTags()); ;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user