public ActionResult <FoodLogEntriesDTO> GetUserFoodHistory( int id, DateTime?startTime, DateTime?endTime) { using (var db = new SqliteContext()) { try { var user = db.Users .Where(x => x.Id == id) .Select(x => new { x.Foods }) .ToList()[0]; var dto = new FoodLogEntriesDTO(); dto.Entries = new List <FoodLogEntryDTO>(); foreach (var foodLogEntry in user.Foods) { var entry = new FoodLogEntryDTO { Timestamp = foodLogEntry.Timestamp.ToString(), FoodId = foodLogEntry.FoodId, ServingAmount = foodLogEntry.ServingAmount }; if (foodLogEntry.Timestamp < startTime) { continue; } else if (foodLogEntry.Timestamp > endTime) { continue; } else { dto.Entries.Add(entry); } } return(dto); } catch (ArgumentOutOfRangeException) { return(StatusCode(404)); } } }
public ActionResult <FoodLogEntriesDTO> GetUserFoodHistory( string id, DateTime?startTime, DateTime?endTime) { // TODO Authorize user // TODO Check for valid timestamps // TODO Check for null case var user = this.Database.Users .Where(x => x.Id == id) .Select(x => new { x.Foods }) .FirstOrDefault(); var dto = new FoodLogEntriesDTO(); dto.Entries = new List <FoodLogEntryDTO>(); foreach (var foodLogEntry in user.Foods) { var entry = new FoodLogEntryDTO { Timestamp = foodLogEntry.Timestamp.ToString(), FoodId = foodLogEntry.FoodId, ServingAmount = foodLogEntry.ServingAmount }; if (foodLogEntry.Timestamp < startTime) { continue; } else if (foodLogEntry.Timestamp > endTime) { continue; } else { dto.Entries.Add(entry); } } return(dto); }