public ActionResult Create([Bind(Include = "AnimalGroupId,EmployeeId,FoodProductsId,TimeForFeeding,FeedingDate,Quantity")] Feedings feedings) { ViewBag.Exception = null; string msg = null; FeedingReminderAccess feedingReminderAccess = new FeedingReminderAccess(); if (ModelState.IsValid) { db.Feedings.Add(feedings); try { FoodProducts foodProducts = db.FoodProducts.Single(c => c.FoodProductsId == feedings.FoodProductsId); foodProducts.Quantity = foodProducts.Quantity - feedings.Quantity ?? default(int); feedingReminderAccess.Create(new FeedingReminder(feedings.FeedingId, feedings.FeedingDate.ToString(), 0)); db.SaveChanges(); } catch (Exception e) { msg = "Unable to create such feeding, we have no more such food products"; ViewBag.Exception = msg; ViewBag.AnimalGroupId = new SelectList(db.AnimalGroups, "AnimalGroupId", "Name"); ViewBag.EmployeeId = new SelectList(db.Employees, "EmployeeId", "FirstName"); ViewBag.FoodProductsId = new SelectList(db.FoodProducts, "FoodProductsId", "Name"); return(View(feedings)); } return(RedirectToAction("Index")); } ViewBag.AnimalGroupId = new SelectList(db.AnimalGroups, "AnimalGroupId", "Name", feedings.AnimalGroupId); ViewBag.EmployeeId = new SelectList(db.Employees, "EmployeeId", "FirstName", feedings.EmployeeId); ViewBag.FoodProductsId = new SelectList(db.FoodProducts, "FoodProductsId", "Name", feedings.FoodProductsId); return(View(feedings)); }
public ActionResult Index() { FeedingReminderAccess feedingReminderAccess = new FeedingReminderAccess(); IEnumerable <FeedingReminder> allReminders = feedingReminderAccess.GetFeedingReminders(); FeedingReminder[] reminders = allReminders.Cast <FeedingReminder>().ToArray(); List <int> ids = filterIds(reminders); var feedings = db.Feedings.Include(f => f.AnimalGroups).Include(f => f.Employees).Include(f => f.FoodProducts).Where(f => ids.Contains(f.FeedingId)); return(View(feedings.ToList())); }
public ActionResult MarkAsShown(int?id) { FeedingReminderAccess feedingReminderAccess = new FeedingReminderAccess(); IEnumerable <FeedingReminder> allReminders = feedingReminderAccess.GetFeedingReminders(); FeedingReminder[] reminders = allReminders.Cast <FeedingReminder>().ToArray(); for (int i = 0; i < reminders.Length; i++) { if (reminders[i].FeedingId == id) { reminders[i].WasShown = 1; feedingReminderAccess.Update(reminders[i].Id, reminders[i]); } } List <int> ids = filterIds(reminders); var feedings = db.Feedings.Include(f => f.AnimalGroups).Include(f => f.Employees).Include(f => f.FoodProducts).Where(f => ids.Contains(f.FeedingId)); return(View(feedings.ToList())); }