Exemplo n.º 1
0
        public IActionResult Post(DayDTO dto)
        {
            try
            {
                Day = ApiRepository.DtoToDay(dto);

                var dayExists = dayData.GetByDate(DateTime.Now.Date, User.Identity.Name);

                if (dayExists != null)
                {
                    return(BadRequest("Day already exists!"));
                }

                dayData.Add(Day);

                if (dayData.Commit() > 0)
                {
                    return(CreatedAtAction("GetById", new { id = Day.DayId }, Day));
                }
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Database Failure"));
            }

            return(BadRequest());
        }
Exemplo n.º 2
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 }));
        }