public ActionResult SaveLesson(TimetableViewModel model) { Dictionary<int, string> ages = new Dictionary<int, string>(); foreach (Group group in db.Groups.ToList()) { ages.Add(group.Id, group.Age); } ViewBag.AgeCategory = ages; if (ModelState.IsValid) { if (model.IsNew) { ModelState.AddModelError("", ""); string serviceName = db.Services.Find(model.ServiceId).Name; string groupName = db.Groups.Find(model.GroupId).Name; string workerName = db.Workers.Find(model.WorkerId).FullName; string duration = db.Services.Find(model.ServiceId).DurationTime; int capacity = db.Groups.Find(model.GroupId).Capacity; int occupied = db.Groups.Find(model.GroupId).Childs.Count; int free = capacity - occupied; Lesson newLesson = new Lesson() { DayOfWeek = model.DayOfWeek, Time = model.Time, Duration = duration, GroupId = model.GroupId, GroupName = groupName, WorkerId = model.WorkerId, WorkerName = workerName, ServiceId = model.ServiceId, ServName = serviceName, Place = model.Place.Trim(), FreePlaces = free, AllPlaces = capacity }; foreach (Lesson l in db.Lessons) { int startTime = Convert.ToInt32(l.Time.Split(':')[0]) * 60 + Convert.ToInt32(l.Time.Split(':')[1]); int endTime = Convert.ToInt32(l.Duration.Split(':')[0]) * 60 + Convert.ToInt32(l.Duration.Split(':')[1]) + startTime; int newLessTime = Convert.ToInt32(newLesson.Time.Split(':')[0]) * 60 + Convert.ToInt32(newLesson.Time.Split(':')[1]); int newLessTimeEnd = Convert.ToInt32(newLesson.Time.Split(':')[0]) * 60 + Convert.ToInt32(newLesson.Time.Split(':')[1]) + Convert.ToInt32(newLesson.Duration.Split(':')[0]) * 60 + Convert.ToInt32(newLesson.Duration.Split(':')[1]); if (((startTime <= newLessTime && newLessTime <= endTime) || (startTime <= newLessTimeEnd && newLessTimeEnd <= endTime)) && l.WorkerName == newLesson.WorkerName && l.DayOfWeek == newLesson.DayOfWeek) { ModelState.AddModelError("", "Teacher " + newLesson.WorkerName + " has a lesson at time: " + newLesson.Time + " - " + (endTime / 60).ToString("00") + ":" + (endTime % 60).ToString("00") + " on " + newLesson.DayOfWeek + "."); ViewBag.Lessons = db.Lessons; return PartialView("TimetablePartial"); } if (((startTime <= newLessTime && newLessTime <= endTime) || (startTime <= newLessTimeEnd && newLessTimeEnd <= endTime)) && l.DayOfWeek == newLesson.DayOfWeek && l.Place == newLesson.Place) { ModelState.AddModelError("", "Place " + newLesson.Place + " is ocupied at time: " + newLesson.Time + " - " + (endTime / 60).ToString("00") + ":" + (endTime % 60).ToString("00") + " on " + newLesson.DayOfWeek + "."); ViewBag.Lessons = db.Lessons; return PartialView("TimetablePartial"); } if (((startTime <= newLessTime && newLessTime <= endTime) || (startTime <= newLessTimeEnd && newLessTimeEnd <= endTime)) && l.GroupName == newLesson.GroupName && l.DayOfWeek == newLesson.DayOfWeek) { ModelState.AddModelError("", "Group " + newLesson.GroupName + " has a lesson at time: " + newLesson.Time + " - " + (endTime / 60).ToString("00") + ":" + (endTime % 60).ToString("00") + " on " + newLesson.DayOfWeek + "."); ViewBag.Lessons = db.Lessons; return PartialView("TimetablePartial"); } } db.Lessons.Add(newLesson); db.SaveChanges(); ViewBag.Lessons = db.Lessons; return PartialView("TimetablePartial"); } else { string serviceName = db.Services.Find(model.ServiceId).Name; string groupName = db.Groups.Find(model.GroupId).Name; string workerName = db.Workers.Find(model.WorkerId).FullName; string duration = db.Services.Find(model.ServiceId).DurationTime; int capacity = db.Groups.Find(model.GroupId).Capacity; int occupied = db.Groups.Find(model.GroupId).Childs.Count; int free = capacity - occupied; Lesson lesson = db.Lessons.Find(model.LessonId); lesson.DayOfWeek = model.DayOfWeek; lesson.Time = model.Time; lesson.Duration = duration; lesson.GroupId = model.GroupId; lesson.GroupName = groupName; lesson.WorkerId = model.WorkerId; lesson.WorkerName = workerName; lesson.ServiceId = model.ServiceId; lesson.ServName = serviceName; lesson.Place = model.Place.Trim(); lesson.AllPlaces = capacity; lesson.FreePlaces = free; db.SaveChanges(); ViewBag.Lessons = db.Lessons; return PartialView("TimetablePartial"); } } return PartialView("TimetablePartial"); }
public ActionResult SaveLesson(TimetableViewModel model) { if (ModelState.IsValid) { if (model.IsNew) { ModelState.AddModelError("", ""); string serviceName = db.Services.Find(model.ServiceId).Name; string groupName = db.Groups.Find(model.GroupId).Name; string workerName = db.Workers.Find(model.WorkerId).FullName; string duration = db.Services.Find(model.ServiceId).DurationTime; Lesson newLesson = new Lesson() { DayOfWeek = model.DayOfWeek, Time = model.Time, Duration = duration, GroupId = model.GroupId, GroupName = groupName, WorkerId = model.WorkerId, WorkerName = workerName, ServiceId = model.ServiceId, ServName = serviceName, Place = model.Place.Trim() }; foreach(Lesson l in db.Lessons) { int startTime = Convert.ToInt32(l.Time.Split(':')[0]) * 60 + Convert.ToInt32(l.Time.Split(':')[1]); int endTime = Convert.ToInt32(l.Duration.Split(':')[0]) * 60 + Convert.ToInt32(l.Duration.Split(':')[1]) + startTime; int newLessTime = Convert.ToInt32(newLesson.Time.Split(':')[0]) * 60 + Convert.ToInt32(newLesson.Time.Split(':')[1]); if(startTime <= newLessTime && newLessTime <= endTime && l.WorkerName == newLesson.WorkerName && l.DayOfWeek == newLesson.DayOfWeek) { ModelState.AddModelError("", "Teacher " + newLesson.WorkerName + " has a lesson at time: " + newLesson.Time + " - " + (endTime / 60).ToString("00") + ":" + (endTime % 60).ToString("00") + " on " + newLesson.DayOfWeek + "."); ViewBag.Lessons = db.Lessons; return PartialView("TimetablePartial"); } if (startTime <= newLessTime && newLessTime <= endTime && l.DayOfWeek == newLesson.DayOfWeek && l.Place == newLesson.Place) { ModelState.AddModelError("", "Place " + newLesson.Place + " is ocupied at time: " + newLesson.Time + " - " + (endTime / 60).ToString("00") + ":" + (endTime % 60).ToString("00") + " on " + newLesson.DayOfWeek + "."); ViewBag.Lessons = db.Lessons; return PartialView("TimetablePartial"); } if (startTime <= newLessTime && newLessTime <= endTime && l.GroupName == newLesson.GroupName && l.DayOfWeek == newLesson.DayOfWeek) { ModelState.AddModelError("", "Group " + newLesson.GroupName + " has a lesson at time: " + newLesson.Time + " - " + (endTime / 60).ToString("00") + ":" + (endTime % 60).ToString("00") + " on " + newLesson.DayOfWeek + "."); ViewBag.Lessons = db.Lessons; return PartialView("TimetablePartial"); } } db.Lessons.Add(newLesson); db.SaveChanges(); ViewBag.Lessons = db.Lessons; return PartialView("TimetablePartial"); } else { string serviceName = db.Services.Find(model.ServiceId).Name; string groupName = db.Groups.Find(model.GroupId).Name; string workerName = db.Workers.Find(model.WorkerId).FullName; string duration = db.Services.Find(model.ServiceId).DurationTime; Lesson lesson = db.Lessons.Find(model.LessonId); lesson.DayOfWeek = model.DayOfWeek; lesson.Time = model.Time; lesson.Duration = duration; lesson.GroupId = model.GroupId; lesson.GroupName = groupName; lesson.WorkerId = model.WorkerId; lesson.WorkerName = workerName; lesson.ServiceId = model.ServiceId; lesson.ServName = serviceName; lesson.Place = model.Place.Trim(); db.SaveChanges(); ViewBag.Lessons = db.Lessons; return PartialView("TimetablePartial"); } } return PartialView("TimetablePartial"); }