47 lines
1.6 KiB
C#
47 lines
1.6 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.PinMessage;
|
|
|
|
public record PinMessageCommand(Guid MessageId) : IBaseCommand;
|
|
|
|
public class PinMessageCommandHandler : IBaseCommandHandler<PinMessageCommand>
|
|
{
|
|
private readonly ITaskChatMessageRepository _repository;
|
|
private readonly IAuthHelper _authHelper;
|
|
|
|
public PinMessageCommandHandler(ITaskChatMessageRepository repository, IAuthHelper authHelper)
|
|
{
|
|
_repository = repository;
|
|
_authHelper = authHelper;
|
|
}
|
|
|
|
public async Task<OperationResult> Handle(PinMessageCommand 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.PinMessage(currentUserId);
|
|
await _repository.UpdateAsync(message);
|
|
await _repository.SaveChangesAsync();
|
|
|
|
return OperationResult.Success();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return OperationResult.ValidationError(ex.Message);
|
|
}
|
|
}
|
|
}
|