public async Task <ActionResult <List <Product> > > Get([FromQuery] int userId) { using (var connection = new SqlConnection(_configuration.GetConnectionString("FoodDb"))) { await connection.OpenAsync(); // oh yea boil the ocean var products = await connection.QueryAsync <Product>("usp_Get_FoodProductsByCarbs", commandType : CommandType.StoredProcedure); if (userId > 0) { var user = InMemoryUsers.GetUsers().FirstOrDefault(x => x.Id == userId); double consumption = user.CarbConsumption.Where(x => x.ConsumedOn.Date == DateTimeOffset.Now.Date) .Sum(x => x.Amount); double max = user.Preference.MaxCarbsPerDayInGrams - consumption; List <Product> productsThatCanBeConsumed = products.Where(x => x.Carbs <= max).ToList(); return(Ok(productsThatCanBeConsumed)); } return(Ok(products.ToList())); } }
public async Task <ActionResult> Consume([FromBody] ConsumeProductCommand command) { if (command.UserId > 0) { var user = InMemoryUsers.GetUsers().FirstOrDefault(x => x.Id == command.UserId); user.CarbConsumption.Add(new CarbConsumption() { Amount = command.CarbAmount, ConsumedOn = DateTimeOffset.Now }); return(NotModified()); } else { return(NotFound()); } }
public async Task <ActionResult <List <User> > > GetUsers() { List <User> users = InMemoryUsers.GetUsers(); return(Ok(users)); }