Exemplo n.º 1
0
        public IActionResult SendMessage(string body, string receiver)
        {
            if (string.IsNullOrEmpty(body) || string.IsNullOrEmpty(receiver))
            {
                return(BadRequest());
            }

            var receiverUser = m_UserRepository.GetByUserName(receiver);
            var unWantsMe    = m_BlockingRepository.GetUnwantsMe(CurrentUser.UserID);

            //Blocked user can't send messages. I thought user who blocks someone should be invisible for them
            if (receiverUser == null || unWantsMe.Any(u => u.UserID == receiverUser.UserID))
            {
                return(NotFound(ResponseFactory.CreateResponse("User Not Found")));
            }

            var message = m_ModelFactory.CreateMessage(body, CurrentUser.UserID, receiverUser.UserID, m_DateTimseService.Now());

            if (TryValidateModel(message))
            {
                m_messageRepository.Insert(message);
            }
            else
            {
                return(BadRequest(ResponseFactory.CreateResponse(ModelState)));
            }

            m_Logger.LogInformation("Message sent to user {0} \n Message Body: {1} \n from user {0}", receiverUser.UserID, message.Body, CurrentUser.UserID);
            return(Ok(ResponseFactory.CreateResponse(message)));
        }