public IActionResult SearchStudent(string genre, string level, string type)
        {
            var students = new List <ClassStudentVM>();
            var classes  = new List <Class>();

            if (genre != null && level != null && type != null)
            {
                classes = _classDataAccess.SearchClass(genre, level, type, GetCurrentStudioId());
            }
            else
            {
                classes = _classDataAccess.GetAllClasses(GetCurrentStudioId());
            }

            foreach (var _class in classes)
            {
                foreach (var id in _classDataAccess.GetStudentsConnectedToClass(_class.Id, GetCurrentStudioId()))
                {
                    var            s       = _studentDataAccess.GetStudentById(id);
                    ClassStudentVM student = new ClassStudentVM();

                    student.Firstname = s.Firstname;
                    student.Lastname  = s.Lastname;
                    student.Email     = s.Email;
                    student.Genre     = _class.Genre;
                    student.Level     = _class.Level;
                    students.Add(student);
                }
            }
            return(Json(students));
        }
        public IActionResult Dashboard(int classId)
        {
            ClassStudentVM dashboardNeeds = new ClassStudentVM();
            var            instructorList = _classDataAccess.GetInstructorsConnectedToClass(classId, GetCurrentStudioId());
            var            studentsIdList = _classDataAccess.GetStudentsConnectedToClass(classId, GetCurrentStudioId());
            var            _class         = _classDataAccess.SearchClass(classId, GetCurrentStudioId());

            foreach (var idsIns in instructorList)
            {
                var instructor = _instructorDataAccess.GetInstructorById(idsIns);
                dashboardNeeds.InstructorsList.Add(instructor);
            }

            foreach (var idsStu in studentsIdList)
            {
                var student = _studentDataAccess.GetStudentById(idsStu);
                dashboardNeeds.StudentsList.Add(student);
            }

            dashboardNeeds.PricePerHour = _class.PricePerHour;
            dashboardNeeds.Level        = _class.Level;
            dashboardNeeds.ClassId      = classId;
            var shedule = _classDataAccess.GetClassShedule(classId);

            foreach (var s in shedule)
            {
                dashboardNeeds.SheduleDays.Add($" {s.Day} - {s.Hour} ");
            }

            return(Json(dashboardNeeds));
        }
        public IActionResult AddNewClass(ClassStudentVM _class)
        {
            var newclass = new Class();

            newclass.Genre        = _class.Genre;
            newclass.Level        = _class.Level;
            newclass.PricePerHour = _class.PricePerHour;
            newclass.ClassType    = _class.ClassType;
            newclass.StartDay     = _class.StartDay;
            newclass.StartDay     = DateTime.ParseExact(_class.StartDayToString, "MM/dd/yyyy", CultureInfo.InvariantCulture);

            int studioId = GetCurrentStudioId();
            int classId  = 0;

            var classes = _classDataAccess.GetAllClasses(GetCurrentStudioId());

            foreach (var c in classes)
            {
                var shedule = _classDataAccess.GetClassShedule(c.Id);

                if ((c.Genre == newclass.Genre) && (c.Level == newclass.Level))
                {
                    string classError = "There is already a class with the same genre and level!";
                    return(RedirectToAction("Classes", new { classError }));
                }

                foreach (var day in _class.SheduleDays)
                {
                    foreach (var s in shedule)
                    {
                        if (day.Equals(s.Day) && _class.Hour == s.Hour)
                        {
                            string classError = "There is already a class on the same day and hour!";
                            return(RedirectToAction("Classes", new { classError }));
                        }
                    }
                }
            }

            _classDataAccess.AddNewClass(newclass, studioId);

            foreach (var day in _class.SheduleDays)
            {
                classId = _classDataAccess.GetClassId(newclass);
                _classDataAccess.AddDayToShedule(day, classId, _class.Hour, studioId);
            }

            foreach (var studentId in _class.StudentsIds)
            {
                _classDataAccess.AddStudentToClass(studentId, classId);
            }

            foreach (var instructorId in _class.InstructorsIds)
            {
                _classDataAccess.AddInstructorToClass(instructorId, classId);
            }
            return(RedirectToAction("Classes"));
        }
        public IActionResult Classes(string classError)
        {
            ViewBag.text       = "Classes";
            ViewBag.StudioName = _studioDataAccess.GetStudioInfo(GetCurrentStudioId()).Name;
            var _class         = new ClassStudentVM();
            var studentsList   = _studentDataAccess.GetAllStudents(GetCurrentStudioId());
            var instructorList = _instructorDataAccess.GetAllInstructors(GetCurrentStudioId());

            foreach (var s in studentsList)
            {
                var sL = new SelectListItem()
                {
                    Value = s.Id.ToString(),
                    Text  = $"{s.Firstname} {s.Lastname}"
                };


                _class.Students.Add(sL);
            }

            foreach (var i in instructorList)
            {
                var iL = new SelectListItem()
                {
                    Value = i.Id.ToString(),
                    Text  = $"{i.Firstname} {i.Lastname}"
                };

                _class.Instructors.Add(iL);
            }

            if (classError != null)
            {
                ViewBag.classError = classError;
            }

            return(View(_class));
        }
        public IActionResult UpdateClass(ClassStudentVM _class)
        {
            var _classToUpdate = new Class();

            ClaimsPrincipal currentUser = User;
            var             claims      = currentUser.Claims;
            var             userEmail   = "";

            foreach (var c in claims)
            {
                userEmail = c.Value;
            }
            var newUser = new User();

            newUser.Email = userEmail;
            var userId = _userDataAccess.GetUserId(newUser);

            _class.Genre = _classDataAccess.SearchClass(_class.ClassId, GetCurrentStudioId()).Genre;

            _classToUpdate.Id = _class.ClassId;

            if (_class.PricePerHour != 0)
            {
                _classToUpdate.PricePerHour = _class.PricePerHour;
            }

            var classes = _classDataAccess.GetAllClasses(GetCurrentStudioId());

            foreach (var c in classes)
            {
                var shedule = _classDataAccess.GetClassShedule(c.Id);

                if ((c.Genre == _class.Genre) && (c.Level == _class.Level))
                {
                    string classError = "There is already a class with the same genre and level!";
                    return(RedirectToAction("Classes", new { classError }));
                }

                foreach (var day in _class.SheduleDays)
                {
                    foreach (var s in shedule)
                    {
                        if (day.Equals(s.Day) && _class.Hour == s.Hour)
                        {
                            string classError = "There is already a class on the same day and hour!";
                            return(RedirectToAction("Classes", new { classError }));
                        }
                    }
                }
            }

            if (_class.Level != null)
            {
                _classToUpdate.Level = _class.Level;
            }

            if (_class.ClassType != null)
            {
                _classToUpdate.ClassType = _class.ClassType;
            }

            _classDataAccess.UpdateClass(_classToUpdate, userId);

            if (_class.SheduleDays != null)
            {
                var classShedule = _classDataAccess.GetClassShedule(_class.ClassId);

                if (classShedule.Count > 1)
                {
                    for (int day = 0; day <= _class.SheduleDays.Count - 1; day++)
                    {
                        _classDataAccess.UpdateShedule(_class.SheduleDays[day], _class.Hour, _class.ClassId, userId, classShedule[day].Id);
                    }
                }
                else
                {
                    for (int day = 0; day < _class.SheduleDays.Count - 1; day++)
                    {
                        _classDataAccess.UpdateShedule(_class.SheduleDays[day], _class.Hour, _class.ClassId, userId, classShedule[0].Id);
                        if (_class.Hour != null)
                        {
                            _classDataAccess.AddDayToShedule(_class.SheduleDays[day + 1], _class.ClassId, _class.Hour, GetCurrentStudioId());
                        }
                        else
                        {
                            _classDataAccess.AddDayToShedule(_class.SheduleDays[day + 1], _class.ClassId, "18:30-19:30", GetCurrentStudioId());
                        }
                    }
                }
            }

            if (_class.InstructorsIds != null)
            {
                foreach (var id in _class.InstructorsIds)
                {
                    _classDataAccess.AddInstructorToClass(id, _class.ClassId);
                }
            }

            if (_class.StudentsIds != null)
            {
                foreach (var id in _class.StudentsIds)
                {
                    _classDataAccess.AddStudentToClass(id, _class.ClassId);
                }
            }

            return(RedirectToAction("Classes"));
        }