// POST api/chats public ChatModel Post([FromBody]ChatModel chatModel) { var allChats = chatsRepository.All(); foreach (var user in chatModel.Participants) { allChats = allChats.Where(c => c.Participants.Select(p => p.Id).Contains(user.Id)); } var existingChat = allChats.Where(c => c.Participants.Count == chatModel.Participants.Count).FirstOrDefault(); if (existingChat == null) { Chat currentChat = new Chat(); foreach (var user in chatModel.Participants) { var currentUser = this.usersRepository.Get(user.Id); currentChat.Participants.Add(currentUser); } chatsRepository.Add(currentChat); return ChatModel.ParseChat(currentChat); } return ChatModel.ParseChat(existingChat); }
public static new ChatFullModel ParseChat(Chat chat, int messageId) { ICollection<MessageModel> messages; if (messageId > -1) { messages = chat.Messages.Where(m => m.Id == messageId).Select(m => MessageModel.ParseMessage(m)).ToList(); } else { messages = chat.Messages.Skip(Math.Max(chat.Messages.Count() - 10, 0)).Select(m => MessageModel.ParseMessage(m)).ToList(); } return new ChatFullModel( chat.Id, chat.Participants.Select(p => UserModel.ParseUser(p)).ToList(), messages); }
public static ChatModel ParseChat(Chat chat) { return new ChatModel(chat.Id, chat.Participants.Select(p => UserModel.ParseUser(p)).ToList()); }