public IActionResult GetTextAnalysisForUser([FromHeader] string userId) { List <TextAnalysis> infoTextAnalyses = new List <TextAnalysis>(); UserTextAnalysis userTextAnalysisJson = new UserTextAnalysis(); using (var database = new LiteDatabase(@"TextAnalysis1.db")) { var usersTextAnalysis = database.GetCollection <TextAnalysis>("UserTextAnalysis"); var allUsers = database.GetCollection <User>("User"); var user = allUsers.FindOne(x => x.userId == userId); if (user == null) { return(NotFound("The user with id " + userId + " could not be found!")); } else { var textAnalysisToBeFound = usersTextAnalysis.Find(x => x.userId == userId); if (textAnalysisToBeFound.Count() == 0) { return(NotFound("The user with id " + userId + " has no text analysis saved.")); } else { infoTextAnalyses = textAnalysisToBeFound.ToList(); userTextAnalysisJson = helperMethods.getUserTextAnalysis(infoTextAnalyses, userId); } } } return(Ok(userTextAnalysisJson)); }
public IActionResult GetNegativeTextAnalysisForUser([FromHeader] string userId, [FromHeader] string date) { List <TextAnalysis> infoTextAnalyses = new List <TextAnalysis>(); List <TextAnalysisResult> textAnalysisResults = new List <TextAnalysisResult>(); UserTextAnalysis userTextAnalysisJson = new UserTextAnalysis(); using (var database = new LiteDatabase(@"TextAnalysis1.db")) { var usersTextAnalysis = database.GetCollection <TextAnalysis>("UserTextAnalysis"); var allUsers = database.GetCollection <User>("User"); var user = allUsers.FindOne(x => x.userId == userId); if (user == null) { return(NotFound("The user with id " + userId + " could not be found!")); } else { if (date != null) { var allTextAnalysisForUser = usersTextAnalysis.Find(x => x.userId == userId); var allTextAnalysisForUserWithDate = allTextAnalysisForUser.Where(x => x.date == date).ToList(); foreach (var item in allTextAnalysisForUserWithDate) { var textAnalysisResultJson = JsonConvert.DeserializeObject <TextAnalysisResult>(item.textAnalysisResult); textAnalysisResults.Add(textAnalysisResultJson); } var negativeTextAnalysisResults = textAnalysisResults.Where(x => x.Context == "Negative").ToList(); foreach (var item in negativeTextAnalysisResults) { var negativeList = allTextAnalysisForUserWithDate.Where(x => x.textAnalysisResult == JsonConvert.SerializeObject(item)).ToList(); foreach (var negative in negativeList) { infoTextAnalyses.Add(negative); } break; } if (allTextAnalysisForUser.Count() == 0) { return(NotFound("The user with id " + userId + " has no text analysis saved.")); } else if (allTextAnalysisForUserWithDate.Count() == 0) { return(NotFound("The user with id " + userId + " has no text analysis saved for this date.")); } else { userTextAnalysisJson = helperMethods.getUserTextAnalysis(infoTextAnalyses, userId); } } else { var allTextAnalysisForUser = usersTextAnalysis.Find(x => x.userId == userId); foreach (var item in allTextAnalysisForUser) { var textAnalysisResultJson = JsonConvert.DeserializeObject <TextAnalysisResult>(item.textAnalysisResult); textAnalysisResults.Add(textAnalysisResultJson); } var negativeTextAnalysisResults = textAnalysisResults.Where(x => x.Context == "Negative").ToList(); foreach (var item in negativeTextAnalysisResults) { var negativeList = allTextAnalysisForUser.Where(x => x.textAnalysisResult == JsonConvert.SerializeObject(item)).ToList(); foreach (var negative in negativeList) { infoTextAnalyses.Add(negative); } break; } if (allTextAnalysisForUser.Count() == 0) { return(NotFound("The user with id " + userId + " has no text analysis saved.")); } else { userTextAnalysisJson = helperMethods.getUserTextAnalysis(infoTextAnalyses, userId); } } } } return(Ok(userTextAnalysisJson)); }
public IActionResult GetTextAnalysisForUserBetweenDates([FromHeader] string userId, [FromHeader] string startingDate, [FromHeader] string endingDate, [FromHeader] string negative) { List <TextAnalysis> infoTextAnalyses = new List <TextAnalysis>(); UserTextAnalysis userTextAnalysisJson = new UserTextAnalysis(); List <TextAnalysisResult> textAnalysisResults = new List <TextAnalysisResult>(); List <string> textAnalysesTimestampsWithinRange = new List <string>(); List <string> parsedDatesBetweenRange = new List <string>(); List <string> datesBetweenRange = new List <string>(); List <TextAnalysis> allTextAnalysesForDateRange = new List <TextAnalysis>(); if (negative != null) { if (startingDate != null && endingDate != null) { var changedStartingDate = startingDate.Replace("-", "/"); var changedEndingDate = endingDate.Replace("-", "/"); var parsedStartingDate = DateTime.ParseExact(changedStartingDate, "MM/dd/yyyy", null); var parsedEndingDate = DateTime.ParseExact(changedEndingDate, "MM/dd/yyyy", null); if ((helperMethods.GetDateRange(parsedStartingDate, parsedEndingDate)).Count() == 0) { return(BadRequest("Start date is bigger than end date!")); } foreach (DateTime date in helperMethods.GetDateRange(parsedStartingDate, parsedEndingDate)) { parsedDatesBetweenRange.Add(date.ToShortDateString()); } foreach (var date in parsedDatesBetweenRange) { var changedDate = date.Replace("/", "-"); datesBetweenRange.Add(changedDate); } } else if (startingDate == null || endingDate == null) { return(BadRequest("Start or end date not specified!")); } using (var database = new LiteDatabase(@"TextAnalysis1.db")) { var usersTextAnalysis = database.GetCollection <TextAnalysis>("UserTextAnalysis"); var allUsers = database.GetCollection <User>("User"); var user = allUsers.FindOne(x => x.userId == userId); if (user == null) { return(NotFound("The user with id " + userId + " could not be found!")); } else { var allTextAnalysisForUser = usersTextAnalysis.Find(x => x.userId == userId); foreach (var date in datesBetweenRange) { var allTextAnalysisForUserWithDate = allTextAnalysisForUser.Where(x => x.date == date); foreach (var item in allTextAnalysisForUserWithDate) { allTextAnalysesForDateRange.Add(item); } } if (allTextAnalysisForUser.Count() == 0) { return(NotFound("The user with id " + userId + " has no text analysis saved.")); } else if (allTextAnalysesForDateRange.Count() == 0) { return(NotFound("The user with id " + userId + " has no text analysis saved for this date range.")); } else { foreach (var textAnalysis in allTextAnalysesForDateRange) { var textAnalysisResultJson = JsonConvert.DeserializeObject <TextAnalysisResult>(textAnalysis.textAnalysisResult); textAnalysisResults.Add(textAnalysisResultJson); } var negativeTextAnalysisResults = textAnalysisResults.Where(x => x.Context == "Negative").ToList(); foreach (var item in negativeTextAnalysisResults) { var negativeList = allTextAnalysesForDateRange.Where(x => x.textAnalysisResult == JsonConvert.SerializeObject(item)).ToList(); foreach (var negativeItem in negativeList) { infoTextAnalyses.Add(negativeItem); } break; } userTextAnalysisJson = helperMethods.getUserTextAnalysis(infoTextAnalyses, userId); return(Ok(userTextAnalysisJson)); } } } } else { if (startingDate != null && endingDate != null) { var changedStartingDate = startingDate.Replace("-", "/"); var changedEndingDate = endingDate.Replace("-", "/"); var parsedStartingDate = DateTime.ParseExact(changedStartingDate, "MM/dd/yyyy", null); var parsedEndingDate = DateTime.ParseExact(changedEndingDate, "MM/dd/yyyy", null); if ((helperMethods.GetDateRange(parsedStartingDate, parsedEndingDate)).Count() == 0) { return(BadRequest("Start date is bigger than end date!")); } foreach (DateTime date in helperMethods.GetDateRange(parsedStartingDate, parsedEndingDate)) { parsedDatesBetweenRange.Add(date.ToShortDateString()); } foreach (var date in parsedDatesBetweenRange) { var changedDate = date.Replace("/", "-"); datesBetweenRange.Add(changedDate); } } else if (startingDate == null || endingDate == null) { return(BadRequest("Start or end date not specified!")); } using (var database = new LiteDatabase(@"TextAnalysis1.db")) { var usersTextAnalysis = database.GetCollection <TextAnalysis>("UserTextAnalysis"); var allUsers = database.GetCollection <User>("User"); var user = allUsers.FindOne(x => x.userId == userId); if (user == null) { return(NotFound("The user with id " + userId + " could not be found!")); } else { var allTextAnalysisForUser = usersTextAnalysis.Find(x => x.userId == userId); foreach (var date in datesBetweenRange) { var allTextAnalysisForUserWithDate = allTextAnalysisForUser.Where(x => x.date == date); foreach (var item in allTextAnalysisForUserWithDate) { allTextAnalysesForDateRange.Add(item); } } if (allTextAnalysisForUser.Count() == 0) { return(NotFound("The user with id " + userId + " has no text analysis saved.")); } else if (allTextAnalysesForDateRange.Count() == 0) { return(NotFound("The user with id " + userId + " has no text analysis saved for this date range.")); } else { userTextAnalysisJson = helperMethods.getUserTextAnalysis(allTextAnalysesForDateRange, userId); return(Ok(userTextAnalysisJson)); } } } } }
public IActionResult GetTextAnalysisForUserWithDate([FromHeader] string userId, [FromHeader] string date, [FromHeader] string startingTime, [FromHeader] string endingTime) { List <TextAnalysis> infoTextAnalyses = new List <TextAnalysis>(); UserTextAnalysis userTextAnalysisJson = new UserTextAnalysis(); List <string> textAnalysesTimestampsWithinRange = new List <string>(); using (var database = new LiteDatabase(@"TextAnalysis1.db")) { var usersTextAnalysis = database.GetCollection <TextAnalysis>("UserTextAnalysis"); var allUsers = database.GetCollection <User>("User"); var user = allUsers.FindOne(x => x.userId == userId); if (user == null) { return(NotFound("The user with id " + userId + " could not be found!")); } else { if (startingTime != null && endingTime != null) { var dateAndStartTime = date + " " + startingTime; var startTime = DateTime.ParseExact(dateAndStartTime, "MM-dd-yyyy HH:mm:ss", CultureInfo.InvariantCulture); var dateAndEndTime = date + " " + endingTime; var endTime = DateTime.ParseExact(dateAndEndTime, "MM-dd-yyyy HH:mm:ss", CultureInfo.InvariantCulture); var startTimeToParse = startTime.Hour.ToString() + ":" + startTime.Minute + ":" + startTime.Second; var endTimeToParse = endTime.Hour.ToString() + ":" + endTime.Minute + ":" + endTime.Second; TimeSpan startingTimeSpan = TimeSpan.Parse(startTimeToParse); TimeSpan endingTimeSpan = TimeSpan.Parse(endTimeToParse); var allTextAnalysisForUser = usersTextAnalysis.Find(x => x.userId == userId); var allTextAnalysisForUserWithDate = allTextAnalysisForUser.Where(x => x.date == date); if (allTextAnalysisForUser.Count() == 0) { return(NotFound("The user with id " + userId + " has no text analysis saved.")); } else if (allTextAnalysisForUserWithDate.Count() == 0) { return(NotFound("The user with id " + userId + " has no text analysis saved for this date.")); } else { foreach (var item in allTextAnalysisForUserWithDate) { var userTextAnalysisTimestamp = item.timestamp; DateTime userTextAnalysisDateTime = DateTime.Parse(userTextAnalysisTimestamp); var userTextAnalysisDateTimeToParse = userTextAnalysisDateTime.Hour.ToString() + ":" + userTextAnalysisDateTime.Minute + ":" + userTextAnalysisDateTime.Second; TimeSpan userTextAnalysisTimeSpan = TimeSpan.Parse(userTextAnalysisDateTimeToParse); if (startingTimeSpan <= endingTimeSpan) { // start and stop times are in the same day if (userTextAnalysisTimeSpan >= startingTimeSpan && userTextAnalysisTimeSpan <= endingTimeSpan) { textAnalysesTimestampsWithinRange.Add(userTextAnalysisTimeSpan.ToString()); } } else { // start and stop times are in different days if (userTextAnalysisTimeSpan >= startingTimeSpan || userTextAnalysisTimeSpan <= endingTimeSpan) { textAnalysesTimestampsWithinRange.Add(userTextAnalysisTimeSpan.ToString()); } } } foreach (var item in textAnalysesTimestampsWithinRange) { var userTextAnalysesWithinTimeRange = allTextAnalysisForUserWithDate.Where(x => x.timestamp == item).ToList(); foreach (var timeRange in userTextAnalysesWithinTimeRange) { infoTextAnalyses.Add(timeRange); } break; } userTextAnalysisJson = helperMethods.getUserTextAnalysis(infoTextAnalyses, userId); return(Ok(userTextAnalysisJson)); } } else { var allTextAnalysisForUser = usersTextAnalysis.Find(x => x.userId == userId); var allTextAnalysisForUserWithDate = allTextAnalysisForUser.Where(x => x.date == date); if (allTextAnalysisForUser.Count() == 0) { return(NotFound("The user with id " + userId + " has no text analysis saved.")); } else if (allTextAnalysisForUserWithDate.Count() == 0) { return(NotFound("The user with id " + userId + " has no text analysis saved for this date.")); } else { infoTextAnalyses = allTextAnalysisForUserWithDate.ToList(); userTextAnalysisJson = helperMethods.getUserTextAnalysis(infoTextAnalyses, userId); } } } return(Ok(userTextAnalysisJson)); } }