public string GetListOfMessagesJson(string who, string withWhom, int timesRequested) { var folderPath = FindPath.FindOrCreateFolderPath(who, withWhom); var filePaths = FindPath.GetTxtFileNamesOfFolder(folderPath).ToList(); //if count is 0 it means users havent chatted at all and it should return a different message to do if (filePaths.Count == 0 || timesRequested > filePaths.Count - 1) { return("You got all Chat History!"); } //SORT LIST BASED ON NAME OF TXT FILE var orderedFilePaths = filePaths.OrderBy(fp => fp, new FileNamesOrderByDate <string>()).ToList(); //Get Requested History var dayHistoryRequested = orderedFilePaths[orderedFilePaths.Count - 1 - timesRequested]; //Get Messages In File var messages = Message.GetMessages(dayHistoryRequested); //Convert them to JSON var json = JsonConvert.SerializeObject(messages, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); return(json); }
public string GetListOfMessagesJson(string who, string withWhom, int timesRequested) { var folderPath = FindPath.FindOrCreateFolderPath(who, withWhom); var filePath = FindPath.GetTxtFileNamesOfFolder(folderPath).ToList(); if (timesRequested > filePath.Count - 1) { return("You got all Chat History!"); } var dayHistoryRequested = filePath[filePath.Count - 1 - timesRequested]; //var day = dayHistoryRequested.Substring(dayHistoryRequested.Length - 14, dayHistoryRequested.Length - 1); string[] lines = File.ReadAllLines(dayHistoryRequested); List <Message> messages = new List <Message>(); for (int i = 0; i < lines.Length - 1;) { Message a = new Message { FromUserName = lines[i], MessageContent = lines[i + 1], TimeSent = DateTime.Parse(lines[i + 2]) }; messages.Add(a); i += 3; } var json = JsonConvert.SerializeObject(messages, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); //var json2 = JsonConvert.SerializeObject(day, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); return(json); }