public ActionResult <CustomDay> Add(CustomDayDTO model)
        {
            Day templateDayChosen = _dayRepository.GetByWeekAndDay(model.TemplateName, model.WeekNr, model.DayNr);

            if (templateDayChosen == null)
            {
                return(NotFound());
            }
            else
            {
                try
                {
                    CustomDay customDayToCreate = new CustomDay(templateDayChosen.TemplateName, templateDayChosen.WeekNr, templateDayChosen.DayNr, model.Date, model.PreDish, model.MainDish, model.Dessert);
                    if (_customDayRepository.GetByDate(customDayToCreate.Date) != null)
                    {
                        return(BadRequest("A day for this date already exists"));
                    }
                    foreach (DayActivity dayActivity in templateDayChosen.DayActivities)
                    {
                        DayActivity dayActivityToAdd = new DayActivity(customDayToCreate, dayActivity.Activity, dayActivity.TimeOfDay);
                        customDayToCreate.AddDayActivity(dayActivityToAdd);
                    }
                    foreach (Helper helper in templateDayChosen.Helpers)
                    {
                        Helper helperToAdd = new Helper(customDayToCreate, helper.User);
                        customDayToCreate.AddHelper(helperToAdd);
                    }
                    _customDayRepository.Add(customDayToCreate);
                    _customDayRepository.SaveChanges();
                    return(Ok(customDayToCreate));
                }
                catch (Exception ex)
                {
                    return(BadRequest(ex.Message));
                }
            }
        }
Esempio n. 2
0
        public ActionResult <Day> GetByWeekAndDay(string templateName, int weekNr, int dayNr)
        {
            Day day = _dayRepository.GetByWeekAndDay(templateName, weekNr, dayNr);

            if (day == null)
            {
                return(NotFound());
            }
            else
            {
                return(Ok(day));
            }
        }