Ejemplo n.º 1
0
        public IActionResult Send([FromBody] MessageModel messageModel)
        {
            var senderUser   = _userService.FindByUserName(messageModel.Username);
            var receiverUser = _userService.FindByUserName(messageModel.ReceiverUsername);

            if (receiverUser != null)
            {
                if (_blockService.IsBlocked(receiverUser.Id, senderUser.Id))
                {
                    _logger.LogInformation($"Sender user {0} is blocked by {1}", senderUser.Id, receiverUser.Id);
                    return(BadRequest("Sender user is blocked."));
                }
                MessageSaveModel messageSaveModel = new MessageSaveModel(senderUser.Id, receiverUser.Id, messageModel.Context);
                var messageModelMapped            = _mapper.Map <Message>(messageSaveModel);
                _messageService.Save(messageModelMapped);
            }
            else
            {
                _logger.LogError("Receiver user could not be found.");
                return(BadRequest("Receiver user could not be found."));
            }

            return(Ok(new
            {
                messageSent = messageModel.Context,
                from = messageModel.Username,
                to = messageModel.ReceiverUsername
            }));
        }