Exemple #1
0
        // GET: Admin/SubjectInSchedules/Create
        public IActionResult Create(int scheduleId)
        {
            var vm = new SubjectInScheduleCreateEditViewModel
            {
                SelectList = new SelectList(MapSubjectTypes.GetSubjectTypes()),
                ScheduleId = scheduleId,
                Teachers   = new List <Teacher>()
            };

            return(View(vm));
        }
Exemple #2
0
        // GET: Admin/SubjectInSchedules/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var subjectInSchedule = await _bll.SubjectInSchedules.FindAsync(id);

            if (subjectInSchedule == null)
            {
                return(NotFound());
            }

            var subject = await _bll.Subjects.FindAsync(subjectInSchedule.SubjectId);

            if (subject == null)
            {
                return(NotFound());
            }

            var vm = new SubjectInScheduleCreateEditViewModel
            {
                SelectList = new SelectList(
                    MapSubjectTypes.GetSubjectTypes(),
                    MapSubjectTypes.GetPreviouslySelectedSubjectType((SubjectType)subjectInSchedule.SubjectType)
                    ),
                SubjectInSchedule   = subjectInSchedule,
                Subject             = subject,
                SelectedSubjectType = MapSubjectTypes.GetPreviouslySelectedSubjectType((SubjectType)subjectInSchedule.SubjectType),
                Teachers            = new List <Teacher>()
            };

            var teachersInSubjectEvent =
                await _bll.TeacherInSubjectEvents.GetAllTeachersForSubjectEventWithoutSubjInclude(subjectInSchedule.Id);

            foreach (var teacherInEvent in teachersInSubjectEvent)
            {
                vm.Teachers.Add(teacherInEvent.Teacher);
            }

            return(View(vm));
        }
Exemple #3
0
        public async Task <IActionResult> Create(SubjectInScheduleCreateEditViewModel vm)
        {
            if (vm.ScheduleId == null)
            {
                return(NotFound());
            }
            vm.SelectList = new SelectList(MapSubjectTypes.GetSubjectTypes(), vm.SelectedSubjectType);
            vm.SubjectInSchedule.SubjectType      = (int)MapSubjectTypes.GetResultSubjectTypeByString(vm.SelectedSubjectType);
            vm.SubjectInSchedule.UniqueIdentifier = $"{vm.Subject.SubjectCode}-{vm.SubjectInSchedule.StartDateTime:yyyyMMddHHmmss}-{vm.SubjectInSchedule.EndDateTime:yyyyMMddHHmmss}";
            vm.SubjectInSchedule.ScheduleId       = (int)vm.ScheduleId;
            vm.SubjectInSchedule.Schedule         = await _bll.Schedules.FindAsync(vm.ScheduleId); // used to pass through model state check

            if (vm.Teachers == null)
            {
                vm.Teachers = new List <Teacher>();
            }
            ModelState.Clear();
            if (!string.IsNullOrWhiteSpace(vm.SubjectInSchedule.Groups) && !string.IsNullOrWhiteSpace(vm.SubjectInSchedule.Rooms) &&
                TryValidateModel(vm.Subject) &&
                TryValidateModel(vm.SubjectInSchedule.Groups) &&
                TryValidateModel(vm.SubjectInSchedule.Rooms))
            {
                if (vm.SubjectInSchedule.StartDateTime == DateTime.MinValue ||
                    vm.SubjectInSchedule.EndDateTime == DateTime.MinValue)
                {
                    ModelState.AddModelError(string.Empty, Resources.Domain.SubjectInSchedule.SubjectInSchedule.DatesChooseError);
                    vm.SubjectInSchedule.StartDateTime = vm.SubjectInSchedule.EndDateTime = DateTime.Today;
                    return(View(vm));
                }

                if (vm.SubjectInSchedule.StartDateTime >= vm.SubjectInSchedule.EndDateTime)
                {
                    ModelState.AddModelError(string.Empty, Resources.Domain.SubjectInSchedule.SubjectInSchedule.DatesDifferenceError);
                    return(View(vm));
                }

                // Add subject
                vm.Subject.CreatedAt = DateTime.Now;
                vm.Subject.CreatedBy = _userManager.GetUserId(User);
                var subjectFromDb = await AddSubjectIfDontExistAsync(vm.Subject);

                vm.SubjectInSchedule.SubjectId = subjectFromDb.Id;
                vm.SubjectInSchedule.Subject   = subjectFromDb;
            }
            ModelState.Clear();
            TryValidateModel(vm);
            if (ModelState.IsValid)
            {
                // set schedule to null so there will be no tracking error!
                vm.SubjectInSchedule.Schedule  = null;
                vm.SubjectInSchedule.Subject   = null;
                vm.SubjectInSchedule.CreatedAt = DateTime.Now;
                vm.SubjectInSchedule.CreatedBy = _userManager.GetUserId(User);

                // Add SubjectInSchedule
                var subjectInScheduleGuid = await _bll.SubjectInSchedules.AddAsync(vm.SubjectInSchedule);

                await _bll.SaveChangesAsync();

                // If teacher list is not empty - add teachers to subject
                if (vm.Teachers != null && vm.Teachers.Count > 0)
                {
                    var subjectInScheduleFromDb =
                        _bll.SubjectInSchedules.GetUpdatesAfterUowSaveChanges(subjectInScheduleGuid);
                    foreach (var teacher in vm.Teachers)
                    {
                        var teacherFromDb = await AddTeacherIfDontExistAsync(teacher);

                        await _bll.TeacherInSubjectEvents.AddAsync(new TeacherInSubjectEvent
                        {
                            CreatedAt           = DateTime.Now,
                            CreatedBy           = _userManager.GetUserId(User),
                            SubjectInScheduleId = subjectInScheduleFromDb.Id,
                            TeacherId           = teacherFromDb.Id
                        });
                    }

                    await _bll.SaveChangesAsync();
                }

                return(RedirectToAction("Index", "ScheduleAndEvents"));
            }

            if (vm.SubjectInSchedule.StartDateTime == DateTime.MinValue ||
                vm.SubjectInSchedule.EndDateTime == DateTime.MinValue)
            {
                ModelState.AddModelError(string.Empty, Resources.Domain.SubjectInSchedule.SubjectInSchedule.DatesChooseError);
                vm.SubjectInSchedule.StartDateTime = vm.SubjectInSchedule.EndDateTime = DateTime.Today;
                return(View(vm));
            }

            if (vm.SubjectInSchedule.StartDateTime >= vm.SubjectInSchedule.EndDateTime)
            {
                ModelState.AddModelError(string.Empty, Resources.Domain.SubjectInSchedule.SubjectInSchedule.DatesDifferenceError);
                return(View(vm));
            }

            return(View(vm));
        }
Exemple #4
0
        public async Task <IActionResult> Edit(int id, SubjectInScheduleCreateEditViewModel vm)
        {
            if (id != vm.SubjectInSchedule.Id)
            {
                return(NotFound());
            }

            vm.SelectList = new SelectList(MapSubjectTypes.GetSubjectTypes(), vm.SelectedSubjectType);
            vm.SubjectInSchedule.SubjectType =
                (int)MapSubjectTypes.GetResultSubjectTypeByString(vm.SelectedSubjectType);
            vm.SubjectInSchedule.UniqueIdentifier =
                $"{vm.Subject.SubjectCode}-{vm.SubjectInSchedule.StartDateTime:yyyyMMddHHmmss}-{vm.SubjectInSchedule.EndDateTime:yyyyMMddHHmmss}";
            vm.SubjectInSchedule.Schedule =
                await _bll.Schedules.FindAsync(vm.SubjectInSchedule
                                               .ScheduleId); // used to pass through model state check

            if (vm.Teachers == null)
            {
                vm.Teachers = new List <Teacher>();
            }

            // Remove teacher function creates teachers with empty names. Delete them from the list!
            for (int i = 0; i < vm.Teachers.Count; i++)
            {
                if (string.IsNullOrWhiteSpace(vm.Teachers[i].TeacherName))
                {
                    vm.Teachers.Remove(vm.Teachers[i]);
                }
            }

            ModelState.Clear();

            if (!string.IsNullOrWhiteSpace(vm.SubjectInSchedule.Groups) && !string.IsNullOrWhiteSpace(vm.SubjectInSchedule.Rooms) &&
                TryValidateModel(vm.Subject) &&
                TryValidateModel(vm.SubjectInSchedule.Groups) &&
                TryValidateModel(vm.SubjectInSchedule.Rooms))
            {
                if (vm.SubjectInSchedule.StartDateTime == DateTime.MinValue ||
                    vm.SubjectInSchedule.EndDateTime == DateTime.MinValue)
                {
                    ModelState.AddModelError(string.Empty, Resources.Domain.SubjectInSchedule.SubjectInSchedule.DatesChooseError);
                    vm.SubjectInSchedule.StartDateTime = vm.SubjectInSchedule.EndDateTime = DateTime.Today;
                    return(View(vm));
                }

                if (vm.SubjectInSchedule.StartDateTime >= vm.SubjectInSchedule.EndDateTime)
                {
                    ModelState.AddModelError(string.Empty, Resources.Domain.SubjectInSchedule.SubjectInSchedule.DatesDifferenceError);
                    return(View(vm));
                }

                // Add subject
                vm.Subject.ChangedAt = DateTime.Now;
                vm.Subject.ChangedBy = _userManager.GetUserId(User);
                _bll.Subjects.Update(vm.Subject);
                vm.SubjectInSchedule.Subject = vm.Subject;
            }
            ModelState.Clear();
            TryValidateModel(vm);
            if (ModelState.IsValid)
            {
                try
                {
                    // set schedule to null so there will be no tracking error!
                    vm.SubjectInSchedule.Schedule  = null;
                    vm.SubjectInSchedule.Subject   = null;
                    vm.SubjectInSchedule.ChangedAt = DateTime.Now;
                    vm.SubjectInSchedule.ChangedBy = _userManager.GetUserId(User);

                    // Add SubjectInSchedule
                    _bll.SubjectInSchedules.Update(vm.SubjectInSchedule);
                    await _bll.SaveChangesAsync();

                    // If teacher list is not empty - add teachers to subject
                    if (vm.Teachers != null && vm.Teachers.Count > 0)
                    {
                        var updatedTeachers = new List <Teacher>();
                        foreach (var teacher in vm.Teachers)
                        {
                            var teacherFromDb = await AddTeacherIfDontExistAsync(teacher);

                            updatedTeachers.Add(teacherFromDb);
                        }

                        await AddTeacherInSubjectEventIfDontExistAsync(vm.SubjectInSchedule.Id, updatedTeachers);
                    }
                    else
                    {
                        await RemoveAllTeachersFromSubject(vm.SubjectInSchedule.Id);
                    }

                    return(RedirectToAction("Index", "ScheduleAndEvents"));
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!SubjectInScheduleExists(vm.SubjectInSchedule.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            if (vm.SubjectInSchedule.StartDateTime == DateTime.MinValue ||
                vm.SubjectInSchedule.EndDateTime == DateTime.MinValue)
            {
                ModelState.AddModelError(string.Empty, Resources.Domain.SubjectInSchedule.SubjectInSchedule.DatesChooseError);
                vm.SubjectInSchedule.StartDateTime = vm.SubjectInSchedule.EndDateTime = DateTime.Today;
                return(View(vm));
            }

            if (vm.SubjectInSchedule.StartDateTime >= vm.SubjectInSchedule.EndDateTime)
            {
                ModelState.AddModelError(string.Empty, Resources.Domain.SubjectInSchedule.SubjectInSchedule.DatesDifferenceError);
                return(View(vm));
            }
            return(View(vm));
        }