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); }
public ActionResult PostUserFoodLogEntry( int id, [FromBody] FoodLogEntryDTO foodLogEntry) { using (var db = new SqliteContext()) { var myFood = new FoodLogEntry { Timestamp = DateTime.Parse(foodLogEntry.Timestamp), FoodId = foodLogEntry.FoodId, ServingAmount = foodLogEntry.ServingAmount, UserId = id, }; db.FoodLogEntries.Add(myFood); db.SaveChanges(); } return(StatusCode(201)); }
public ActionResult PostUserFoodLogEntry( string id, [FromBody] FoodLogEntryDTO foodLogEntry) { // TODO Authorize user // TODO Check for timestamp validity if (string.IsNullOrWhiteSpace(foodLogEntry.Timestamp)) { return(StatusCode(400, new ErrorDTO("A timestamp must be provided."))); } if (foodLogEntry.FoodId <= 0) { return(StatusCode(400, new ErrorDTO("A food ID must be provided."))); } if (foodLogEntry.ServingAmount <= 0) { return(StatusCode(400, new ErrorDTO("The serving amount cannot be zero or negative."))); } // TODO Perform input validation var myFood = new FoodLogEntry { Timestamp = DateTime.Parse(foodLogEntry.Timestamp), FoodId = foodLogEntry.FoodId, ServingAmount = foodLogEntry.ServingAmount, UserId = id, }; this.Database.FoodLogEntries.Add(myFood); this.Database.SaveChanges(); // TODO Return created object or location header (or both) return(StatusCode(201)); }