Esempio n. 1
0
        public IActionResult OnPostProcessMeal(int mealId, [FromRoute] int dayId, [FromRoute] int category, [FromRoute] bool?isremoved = false)
        {
            IsRemoved = isremoved.Value;

            Meal = mealData.GetById(mealId);
            Day  = dayData.GetById(dayId) ?? new Day {
                DayMeals = new List <DayMeal>()
            };

            if (!IsRemoved)
            {
                DayMeal = dayMealData.SetValues(Day, Meal, (MealCategory)category);

                Day.DayMeals.Add(DayMeal);

                if (Day.DayId == 0)
                {
                    Day.OwnerName = User.Identity.Name;
                    Day.Date      = DateTime.Now.Date;
                    dayData.Add(Day);
                }
                else
                {
                    Day = dayData.GetById(dayId);
                    dayData.Update(Day);
                }

                TempData["Message"] = $"{Meal.Name} added to {(MealCategory)category}";
            }
            else
            {
                DayMeal = dayMealData.GetByValues(dayId, mealId, (MealCategory)category);

                Day.DayMeals.Remove(DayMeal);

                TempData["Message"] = $"{Meal.Name} removed from {(MealCategory)category}";
            }

            dayData.Commit();



            return(RedirectToPage("./Today", new { dayId = Day.DayId }));
        }
Esempio n. 2
0
        public IActionResult Put(int id, DayDTO dto)
        {
            try
            {
                Day = dayData.GetById(id);
                if (Day == null)
                {
                    return(NotFound($"Could not find day with id:{id}"));
                }

                ApiRepository.DtoToDay(dto, Day);
                dayData.Update(Day);

                if (dayData.Commit() > 0)
                {
                    return(Ok("Updated!"));
                }
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Database Failure"));
            }
            return(BadRequest());
        }