using Diligent.WebAPI.Business.Services;
using Diligent.WebAPI.Data.Entities;
using Diligent.WebAPI.Host.Exceptions;
using Diligent.WebAPI.Host.Mediator.Messages.Commands;
using MediatR;

namespace Diligent.WebAPI.Host.Mediator.Messages.Handlers
{
    public class AddMessageHandler : IRequestHandler<AddMessageCommand, Unit>
    {
        private readonly RoomService _roomService;

        public AddMessageHandler(RoomService roomService)
        {
            _roomService = roomService;
        }
        public async Task<Unit> Handle(AddMessageCommand request, CancellationToken cancellationToken)
        {
            var result = await _roomService.AddMessage(request.roomId,request.message);

            if (!result)
                throw new NotFoundException("Room id doesn't exist");

            return new Unit();
        }
    }
}
