using Diligent.WebAPI.Business.Interfaces;
using Diligent.WebAPI.Host.Mediator.Notifications.Commands;
using MediatR;

namespace Diligent.WebAPI.Host.Mediator.Notifications.Handlers
{
    public class AddNotificationHandler : IRequestHandler<AddNotificationCommand, Unit>
    {
        private readonly ICustomerService _customerService;

        public AddNotificationHandler(ICustomerService customerService)
        {
            _customerService = customerService;
        }

        public async Task<Unit> Handle(AddNotificationCommand request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new BadHttpRequestException("Object cannot be null");
            }

            var result = await _customerService.AddNotification(request.Notification.ReceiverId, request.Notification.RoomId);

            if (!result)
            {
                throw new Exception("Problem with saving notification in database");
            }

            return new Unit();
        }
    }
}
