api/NetinaCMS.Repository/Handlers/Blogs/UpdateBlogCommandHandler.cs

34 lines
1.2 KiB
C#

using NetinaCMS.Repository.Repositories.Base.Contracts;
namespace NetinaCMS.Repository.Handlers.Blogs;
public class UpdateBlogCommandHandler : IRequestHandler<UpdateBlogCommand,bool>
{
private readonly IRepositoryWrapper _repositoryWrapper;
public UpdateBlogCommandHandler(IRepositoryWrapper repositoryWrapper)
{
_repositoryWrapper = repositoryWrapper;
}
public async Task<bool> Handle(UpdateBlogCommand request, CancellationToken cancellationToken)
{
var ent = await _repositoryWrapper.SetRepository<Blog>()
.TableNoTracking
.FirstOrDefaultAsync(b => b.Id == request.Id, cancellationToken);
if (ent == null)
throw new AppException("Blog not found", ApiResultStatusCode.NotFound);
var newEnt = Blog.Create(request.Title, request.Content, request.Tags, request.ReadingTime, request.Summery,
request.IsSuggested, request.CategoryId);
newEnt.CreatedAt = ent.CreatedAt;
newEnt.CreatedBy = ent.CreatedBy;
newEnt.Id = ent.Id;
_repositoryWrapper.SetRepository<Blog>()
.Update(newEnt);
await _repositoryWrapper.SaveChangesAsync(cancellationToken);
return true;
}
}