public ActionResult <Helper> AddHelper(DateTime date, HelperDTO model) { CustomDay customDayToEdit = _customDayRepository.GetByDate(date); if (customDayToEdit == null) { return(NotFound()); } else { User user = _userRepository.GetById(model.UserId); if (user == null) { return(NotFound()); } else { try { Helper helperToAdd = new Helper(customDayToEdit, user); customDayToEdit.AddHelper(helperToAdd); _customDayRepository.SaveChanges(); return(Ok(helperToAdd)); } catch (Exception ex) { return(BadRequest(ex.Message)); } } } }
public ActionResult <CustomDay> Edit(DateTime date, CustomDayDTO model) { CustomDay dayToEdit = _customDayRepository.GetByDate(date); if (dayToEdit == null) { return(NotFound()); } else { if (!dayToEdit.WeekNr.Equals(model.TemplateName) || (dayToEdit.WeekNr != model.WeekNr) || (dayToEdit.DayNr != model.DayNr)) { try { Day templateDayChosen = _dayRepository.GetByWeekAndDay(model.TemplateName, model.WeekNr, model.DayNr); dayToEdit.TemplateName = templateDayChosen.TemplateName; dayToEdit.WeekNr = templateDayChosen.WeekNr; dayToEdit.DayNr = templateDayChosen.DayNr; dayToEdit.DayActivities = new List <DayActivity>(); dayToEdit.Helpers = new List <Helper>(); foreach (DayActivity dayActivity in templateDayChosen.DayActivities) { DayActivity dayActivityToAdd = new DayActivity(dayToEdit, dayActivity.Activity, dayActivity.TimeOfDay); dayToEdit.AddDayActivity(dayActivityToAdd); } foreach (Helper helper in templateDayChosen.Helpers) { Helper helperToAdd = new Helper(dayToEdit, helper.User); dayToEdit.AddHelper(helperToAdd); } } catch (Exception ex) { return(BadRequest(ex.Message)); } } try { dayToEdit.Date = model.Date; dayToEdit.PreDish = model.PreDish; dayToEdit.MainDish = model.MainDish; dayToEdit.Dessert = model.Dessert; _customDayRepository.SaveChanges(); return(Ok(dayToEdit)); } catch (Exception ex) { return(BadRequest(ex.Message)); }; } }
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)); } } }