Esempio n. 1
0
        public async Task <IActionResult> AddMessageForUser(int userId, MessageFormDto messageDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            User currentUser = await _repository.GetUser(userId);

            messageDto.SenderId = userId;

            User recipient = await _repository.GetUser(messageDto.RecipientId);

            if (recipient == null)
            {
                return(BadRequest("Recipient does not exist"));
            }

            Message message = _mapper.Map <Message>(messageDto);

            _repository.Add(message);

            if (await _repository.SaveAll())
            {
                MessageForDetailDto messageToReturnDto = _mapper.Map <MessageForDetailDto>(message);

                return(CreatedAtRoute("GetMessage", new { messageId = message.Id }, messageToReturnDto));
            }

            return(BadRequest("Message could not be saved"));
        }
Esempio n. 2
0
        public async Task SendMessage([FromBody] MessageFormDto message)
        {
            Message newMessage = Message.Convert(message);

            Message messageCreated = this._messageService.Create(newMessage);

            this._threadService.UpdateNewMessage(messageCreated.Thread, messageCreated.Id);

            await context.Clients.All.SendAsync("ReceiveMessage", messageCreated);
        }
Esempio n. 3
0
 public static Message Convert(MessageFormDto messageForm)
 {
     return(new Message(null, messageForm.Sender, messageForm.Thread, messageForm.Content, messageForm.CreeAt));
 }