public ActionResult <GetChatResponce> GetMinimumChat(GetMinimumChatRequest req) { GetChatResponce chat = service.GetMinimumChat(req, ClientsUserId); if (chat == null) { return(NotFound()); } return(Ok(chat)); }
public ActionResult <GetChatResponce> GetChat(GetChatRequest req) { GetChatResponce chat = service.GetChatMessagesByIndex(req); if (chat == null) { return(NotFound()); } return(Ok(chat)); }
public GetChatResponce GetChatMessagesByIndex(GetChatRequest req) { var projection = Builders <Chat> .Projection .Expression(c => new GetChatResponce { Id = c.Id, Members = c.Members, UpdatedAt = c.UpdatedAt, Messages = c.MessageLists.ToList()[req.MessageListIndex].Messages, CreatedAt = c.CreatedAt, MessageListIndex = req.MessageListIndex }); GetChatResponce res = null; try { res = collection .Find(c => c.Id == req.ChatId) .Project(projection) .FirstOrDefault(); } catch (Exception) { } return(res); }
public GetChatResponce CreateChat(CreateChatRequest req, string userId) { var firstMessageSentAt = GetCurrentUnixTimestamp(); Message message = new Message { Id = req.FirstMessage.Id, Body = req.FirstMessage.Body, AuthorUserId = userId, SentAt = firstMessageSentAt }; var messageDate = GetDateTimeFromUnixTimeStamp(message.SentAt); bool membersContainsClientsUserId = false; foreach (ChatMember member in req.Members) { if (member.UserId == userId) { membersContainsClientsUserId = true; continue; } } if (!membersContainsClientsUserId) { return(null); } List <ChatMember> members = req.Members.ToList(); int authorsMemberIndex = members .FindIndex(m => m.UserId == userId); members[authorsMemberIndex].LastTimeOnChat = firstMessageSentAt; Chat chat = new Chat { Members = members, UpdatedAt = firstMessageSentAt, MessageLists = new List <MessageList> { new MessageList { Day = messageDate.Day, Month = messageDate.Month, Year = messageDate.Year, Messages = new List <Message> { message } } }, CreatedAt = firstMessageSentAt }; Create(chat); GetChatResponce res = new GetChatResponce { Id = chat.Id, CreatedAt = chat.CreatedAt, Members = chat.Members, Messages = chat.MessageLists.First().Messages, UpdatedAt = chat.UpdatedAt }; return(res); }