Exemple #1
0
        public Task <Consumable> CreateAsync(Consumable consumable)
        {
            _context.Consumables.Add(consumable);

            try
            {
                _context.SaveChanges();
            }
            catch (Exception e)
            {
                if (!(e.InnerException is MySqlException exception))
                {
                    throw;
                }

                if (exception.Number == (int)MySqlErrorCode.DuplicateKeyEntry)
                {
                    throw new ExecutionError(consumable.Name + " exists already in the database.");
                }

                throw;
            }

            return(Task.FromResult(consumable));
        }
        public IActionResult NewMeal(Meal meal)
        {
            if (!string.IsNullOrEmpty(meal.Name))
            {
                _ctx.Meals.Add(meal);
                _ctx.SaveChanges();

                return(RedirectToAction("Index"));
            }
            else
            {
                ViewData["ErrorMessage"] = "Value cannot be null or empty";
            }

            return(View());
        }
 public void DeleteRecord(int id)
 {
     using var caloriesContext = new CaloriesContext();
     caloriesContext.CalorieDiaries.Remove(new CalorieDiary {
         Id = id
     });
     caloriesContext.SaveChanges();
 }
Exemple #4
0
        public Task <Consumption> CreateAsync(int consumableId, int mealId)
        {
            var meal = _context.Meals
                       .Single(m => m.Id == mealId);
            var consumable = _context.Consumables
                             .Single(x => x.Id == consumableId);
            var consumption = new Consumption
            {
                MealId    = mealId, Meal = meal, ConsumableId = consumableId, Consumable = consumable,
                CreatedAt = DateTime.Now
            };

            _context.Consumptions
            .Add(consumption);
            _context.SaveChanges();

            return(Task.FromResult(consumption));
        }
 public void AddExercise(int exerciseId, DateTime date)
 {
     using var caloriesContext = new CaloriesContext();
     caloriesContext.CalorieDiaries.Add(new CalorieDiary
     {
         AddedAt    = date,
         ExerciseId = exerciseId
     });
     caloriesContext.SaveChanges();
 }
 public void AddFood(int foodId, DateTime date)
 {
     using var caloriesContext = new CaloriesContext();
     caloriesContext.CalorieDiaries.Add(new CalorieDiary()
     {
         AddedAt = date,
         FoodId  = foodId
     });
     caloriesContext.SaveChanges();
 }
Exemple #7
0
        public Task <Meal> CreateAsync(Meal meal)
        {
            _context.Meals.Add(meal);
            try
            {
                _context.SaveChanges();
            }
            catch (DbUpdateException e)
            {
                if (!(e.InnerException is MySqlException exception))
                {
                    throw;
                }

                if (exception.Number == (int)MySqlErrorCode.DuplicateKeyEntry)
                {
                    throw new ExecutionError(meal.Name + " exists already in the database.");
                }
                throw;
            }

            return(Task.FromResult(meal));
        }