public async Task <IList <MessageData> > LoadMessagesAsync(int limit = 0, CancellationToken token = default) { var messages = await _messageRepository.GetAsync(limit, token); if (messages == null || messages.Count == 0) { return(null); } List <MessageData> res = new List <MessageData>(); for (int i = 0; i < messages.Count; i++) { MessageDataEntity m = messages[i]; string text = m.Text; token.ThrowIfCancellationRequested(); if (i + 1 < messages.Count && messages[i + 1].Id == m.Id) { StringBuilder textBuilder = new StringBuilder(m.Text); while (i + 1 < messages.Count && messages[i + 1].Id == m.Id) { textBuilder.Append("\n" + messages[++i].Text); } text = textBuilder.ToString(); } res.Add(new MessageData(m.Id, m.DateTime, m.UserName, text, m.UserName == CurrentUserContext.CurrentUserName, m.ReplyToMessageId)); } return(res); }
public async Task GetLast2StringFrom5_Return2() { File.AppendAllLines(_filename, new string[] { (new MessageDataEntity("user11", "hello1")).ToString(), (new MessageDataEntity("user21", "hello2")).ToString(), (new MessageDataEntity("user31", "hello3")).ToString(), (new MessageDataEntity("user41", "hello4")).ToString(), (new MessageDataEntity("user51", "hello5")).ToString(), }); var res = await _repo.GetAsync(2); Assert.AreEqual(2, res.Count); Assert.AreEqual("USER41", res[0].UserName); Assert.AreEqual("USER51", res[1].UserName); }