52 lines
1.7 KiB
C#
52 lines
1.7 KiB
C#
using GozareshgirProgramManager.Application._Common.Interfaces;
|
|
using GozareshgirProgramManager.Application._Common.Models;
|
|
using GozareshgirProgramManager.Domain._Common.Exceptions;
|
|
using GozareshgirProgramManager.Domain.TaskChatAgg.Repositories;
|
|
using MediatR;
|
|
|
|
namespace GozareshgirProgramManager.Application.Modules.TaskChat.Commands.EditMessage;
|
|
|
|
public record EditMessageCommand(
|
|
Guid MessageId,
|
|
string NewTextContent
|
|
) : IBaseCommand;
|
|
|
|
public class EditMessageCommandHandler : IBaseCommandHandler<EditMessageCommand>
|
|
{
|
|
private readonly ITaskChatMessageRepository _repository;
|
|
private readonly IAuthHelper _authHelper;
|
|
|
|
public EditMessageCommandHandler(ITaskChatMessageRepository repository, IAuthHelper authHelper)
|
|
{
|
|
_repository = repository;
|
|
_authHelper = authHelper;
|
|
}
|
|
|
|
public async Task<OperationResult> Handle(EditMessageCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var currentUserId = _authHelper.GetCurrentUserId()??
|
|
throw new UnAuthorizedException("کاربر احراز هویت نشده است");
|
|
|
|
var message = await _repository.GetByIdAsync(request.MessageId);
|
|
if (message == null)
|
|
{
|
|
return OperationResult.NotFound("پیام یافت نشد");
|
|
}
|
|
|
|
try
|
|
{
|
|
message.EditMessage(request.NewTextContent, currentUserId);
|
|
await _repository.UpdateAsync(message);
|
|
await _repository.SaveChangesAsync();
|
|
|
|
// TODO: SignalR notification
|
|
|
|
return OperationResult.Success();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return OperationResult.ValidationError(ex.Message);
|
|
}
|
|
}
|
|
}
|