private void UpdateStoredMessage(Api.Models.Message poMessage) { using (AndressBanenesBotContext loContext = new AndressBanenesBotContext(msConnectionString)) { DAL.Message loStoredMessage = loContext.Message .FirstOrDefault(x => x.Chat.TelegramChatId == poMessage.Chat.Id && x.TelegramMessageId == poMessage.MessageId); loStoredMessage.Text = poMessage.Text; loContext.SaveChanges(); } }
private void StoreMessage(Api.Models.Message poMessage) { using (AndressBanenesBotContext loContext = new AndressBanenesBotContext(msConnectionString)) { DAL.Chat loChat = loContext.Chat.FirstOrDefault(x => x.TelegramChatId == poMessage.Chat.Id); DAL.User loUser = loContext.User.FirstOrDefault(x => x.TelegramUserId == poMessage.From.Id); DAL.Message loReplyToMessage = null; //If either the chat or the user is new if (loChat == null || loUser == null) { //If the chat is new, create it and then add it to the context if (loChat == null) { loChat = new DAL.Chat() { TelegramChatId = poMessage.Chat.Id, Username = poMessage.Chat.Username, Title = poMessage.Chat.Title, Type = Enum.GetName(poMessage.Chat.Type.GetType(), poMessage.Chat.Type) }; loContext.Chat.Add(loChat); } //If the user is new, create it and then add it to the context if (loUser == null) { loUser = new DAL.User() { TelegramUserId = poMessage.From.Id, FirstName = poMessage.From.FirstName, LastName = poMessage.From.LastName, Username = poMessage.From.Username }; loContext.User.Add(loUser); } //Save the changes to the context so the IDs get filled loContext.SaveChanges(); } //If the message is a reply to another message, try getting the original message from the database if it exists in there if (poMessage.ReplyToMessage != null) { loReplyToMessage = loContext.Message.FirstOrDefault(x => x.TelegramMessageId == poMessage.ReplyToMessage.MessageId && x.ChatId == loChat.Id); } //Create the new Message object DAL.Message loMessage = new DAL.Message() { ChatId = loChat.Id, UserId = loUser.Id, ReplyToMessageId = loReplyToMessage?.Id, TelegramMessageId = poMessage.MessageId, Text = poMessage.Text }; loContext.Message.Add(loMessage); loContext.SaveChanges(); } }