Api/Netina.Repository/Handlers/Comments/CreateCommentCommandHandler.cs

58 lines
2.2 KiB
C#

using AppException = Netina.Common.Models.Exception.AppException;
namespace Netina.Repository.Handlers.Comments;
public class CreateCommentCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService)
: IRequestHandler<CreateCommentCommand, Guid>
{
public async Task<Guid> Handle(CreateCommentCommand request, CancellationToken cancellationToken)
{
Guid userId;
if (request.UserId == null)
{
if (!Guid.TryParse(currentUserService.UserId, out userId))
throw new AppException("User id is wrong", ApiResultStatusCode.BadRequest);
}
else
userId = request.UserId.Value;
if (request.BlogId != null)
{
var entBlog = BlogComment.Create(request.Title, request.Content, request.Rate, request.IsAdmin, userId, request.BlogId.Value);
if (request.IsAdmin)
entBlog.ConfirmReview();
if (request.ParentId != null)
entBlog.SetParent(request.ParentId.Value);
repositoryWrapper.SetRepository<BlogComment>().Add(entBlog);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
return entBlog.Id;
}
if (request.ProductId != null)
{
var entBlog = ProductComment.Create(request.Title, request.Content, request.Rate, request.IsBuyer,
request.IsAdmin, userId, request.ProductId.Value);
if (request.IsAdmin)
entBlog.ConfirmReview();
if (request.ParentId != null)
entBlog.SetParent(request.ParentId.Value);
repositoryWrapper.SetRepository<ProductComment>().Add(entBlog);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
return entBlog.Id;
}
var ent = Comment.Create(request.Title, request.Content, request.Rate, request.IsAdmin, userId);
if (request.IsAdmin)
ent.ConfirmReview();
if (request.ParentId != null)
ent.SetParent(request.ParentId.Value);
repositoryWrapper.SetRepository<Comment>().Add(ent);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
return ent.Id;
}
}