public IActionResult AddDuty(string StringDay, IndexViewModel model) { if (String.IsNullOrEmpty(model.NewDutyContent)) { ModelState.AddModelError("", "Twoje zadanie nie zawiera żadnej treści!"); return(RedirectToAction("Index", "Category", new { id = model.CategoryId })); } if (model.NewDutyContent.Length > 20) { ModelState.AddModelError("", "Maksymalna długość treści twojego zadania wynosi 20 znaków!"); return(RedirectToAction("Index", "Category", new { id = model.CategoryId })); } Day DayForCreate = Day.Monday; bool StringDayMatchedFlag = false; foreach (Day day in Enum.GetValues(typeof(Day))) { if (day.ToString() == StringDay) { DayForCreate = day; StringDayMatchedFlag = true; break; } } if (!StringDayMatchedFlag) { throw new Exception("StringDay hasn't matched"); } var category = _categoryRepository.GetCategory(Int32.Parse(model.CategoryId)); if (category == null) { throw new Exception("No such category"); } var duty = new Duty() { Content = model.NewDutyContent, Category = category, Day = DayForCreate }; _dutyRepository.AddDuty(duty); _dbContextService.Commit(); return(RedirectToAction("Index", "Category", new { id = model.CategoryId })); }
public IActionResult AddDuty(string StringDay, DayViewModel model) { var category = _categoryRepository.GetCategory(model.DutyCategoryId); if (category == null) { ModelState.AddModelError("", "Każde zadanie musi przynależeć do kategorii. Stwórz nową kategorię, a następnie dodaj do niej zadanie."); return(RedirectToAction("Index", "Home")); } if (!ModelState.IsValid) { return(RedirectToAction("Index", "Home")); } Day DayForCreate = Day.Monday; bool StringDayMatchedFlag = false; foreach (Day day in Enum.GetValues(typeof(Day))) { if (StringDay == day.ToString()) { DayForCreate = day; StringDayMatchedFlag = true; break; } } if (!StringDayMatchedFlag) { throw new Exception("StringDay hasn't matched"); } var newDuty = new Duty() { Content = model.Content, Day = DayForCreate, Category = category }; _dutyRepository.AddDuty(newDuty); _dbContextService.Commit(); return(RedirectToAction("Index", "Home")); }