public async Task <MessageServiceModel> SendMessageAsync(MessageServiceModel model) { if (!this.IsEntityStateValid(model)) { return(null); } var author = await this.usersRepository.All() .SingleOrDefaultAsync(u => u.UserName == model.Author.UserName); var conversationFound = await this.conversationsRepository.All() .AnyAsync(c => c.Id == model.ConversationId); if (author == null || !conversationFound) { return(null); } var message = Mapper.Map <Message>(model); message.Author = author; await this.messagesRepository.AddAsync(message); await this.messagesRepository.SaveChangesAsync(); return(Mapper.Map <MessageServiceModel>(message)); }
public async Task SendMessage(string content, string convId) { if (convId == null || content == null || content.Length < 1) { return; } var conversation = await this.conversationsService.GetAsync(convId); if (conversation == null || !this.ridesService.IsUserParticipant(conversation.Ride, this.Context.User.Identity.Name) && !this.Context.User.IsInRole(GlobalConstants.AdminRoleName)) { return; } var serviceModel = new MessageServiceModel { Author = new PoolItUserServiceModel { UserName = this.Context.User.Identity.Name }, Content = content, ConversationId = convId, SentOn = DateTime.UtcNow }; var resultMsg = await this.conversationsService.SendMessageAsync(serviceModel); if (resultMsg == null) { return; } var viewModel = Mapper.Map <MessageViewModel>(resultMsg); viewModel.AuthorName = HttpUtility.HtmlEncode(viewModel.AuthorName); viewModel.Content = HttpUtility.HtmlEncode(viewModel.Content); await this.Clients.Groups(convId).SendAsync("MessageReceived", viewModel); }
public List <MessageServiceModel> GetMessagesByCommunicationId(int communicationId) { List <MessageServiceModel> list = new List <MessageServiceModel>(); using (SqlConnection conn = new SqlConnection(connString)) { SqlCommand cmd = new SqlCommand("tdb_msg_bid", conn); cmd.CommandType = CommandType.StoredProcedure; conn.Open(); cmd.Parameters.AddWithValue("@com_id", communicationId); using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { if (reader.HasRows) { MessageServiceModel communication = new MessageServiceModel { Id = int.Parse(reader["msg_id"].ToString()), CommunicationId = int.Parse(reader["communication_id"].ToString()), Date = reader["date"].ToString(), Content = reader["content_text"].ToString(), AuthorId = int.Parse(reader["user_author_id"].ToString()), ReceiverId = int.Parse(reader["receiver_id"].ToString()) }; list.Add(communication); } } } conn.Close(); } return(list); }