public ReMessageList GetReMessageList(string userId, int messageId) { if (db.ApplicationUserMessages.Any(aum => aum.MessageId == messageId)) { Message message = db.Messages.Where(m => m.Id == messageId).First(); if (message.ApplicationUserId == userId || db.ApplicationUserMessages.Where(aum => aum.MessageId == messageId).First().ApplicationUserId == userId) { ReMessageList model = new ReMessageList { Theme = message.Theme }; MessageUserIdPair tempMessageUserIdPair = GetFromUserMessage(messageId); model.ReMessageId = messageId; if (message.ApplicationUserId == userId) { model.ReceiverId = tempMessageUserIdPair.UserId; model.IsUser = tempMessageUserIdPair.IsUser; } else { model.ReceiverId = message.ApplicationUserId; model.IsUser = IsUser(message.ApplicationUserId); } model.ReMessages = new List <ReMessageItem> { }; Message tempMessage = message; for (; ;) { model.ReMessages.Add(new ReMessageItem { Sender = tempMessageUserIdPair.User, SenderId = tempMessageUserIdPair.UserId, IsUser = tempMessageUserIdPair.IsUser, Text = tempMessage.Text, DateTime = tempMessage.DateTime }); tempMessage = GetReMessage(tempMessage.Id); if (tempMessage != null) { tempMessageUserIdPair = GetFromUserMessage(tempMessage.Id); } else { break; } } return(model); } } return(null); }
private MessageUserIdPair GetFromUserMessage(int messageId) { MessageUserIdPair result = new MessageUserIdPair(); ApplicationUserMessage tempApplicationUserMessage = db.ApplicationUserMessages.Where(aum => aum.MessageId == messageId).First(); if (db.Kindergartens.Any(k => k.Id == tempApplicationUserMessage.ApplicationUserId)) { Kindergarten tempKindergarten = db.Kindergartens.Where(su => su.Id == tempApplicationUserMessage.ApplicationUserId).First(); result.UserId = tempKindergarten.Id; result.User = tempKindergarten.Name; result.IsUser = false; } if (db.SiteUsers.Any(su => su.Id == tempApplicationUserMessage.ApplicationUserId)) { SiteUser tempSiteUser = db.SiteUsers.Where(su => su.Id == tempApplicationUserMessage.ApplicationUserId).First(); result.UserId = tempSiteUser.Id; result.User = tempSiteUser.FullName; result.IsUser = true; } return(result); }