Esempio n. 1
0
        public IActionResult Edit(string courseId, CourseViewModel courseModel)
        {
            //Check Model State
            if (!ModelState.IsValid || !courseModel.Schedules.Any())
            {
                return(BadRequest());
            }

            var course  = _courseService.GetCourse(courseId);
            var teacher = _context.Users.Find(courseModel.Course.TeacherId);

            if (course == null || teacher == null)
            {
                return(NotFound());
            }

            //ToDo :: Uncomment this for extra security || Move it up to be in the same method
            //            if(!_access.IsAdmin(User) || !_access.IsCourseTeacher(User, courseId))
            //                return BadRequest();

            _context.RemoveRange(_courseService.GetCourseSchedule(courseId));
            _courseService.CreateSchedule(courseId, courseModel.Schedules);
            _context.SaveChanges();
            //Add New Countries
            var courseCountries = course.Countries;

            if (courseCountries == null)
            {
                courseCountries = new List <Country>();
            }
            foreach (var cvm in courseModel.CountriesViewModel)
            {
                bool isNew = true;
                foreach (var c in courseCountries)
                {
                    if (cvm.Country.IsoCode == c.IsoCode)
                    {
                        isNew = false;
                        if (!cvm.isAvailable)
                        {
                            courseCountries.Remove(c);
                            break;
                        }
                    }
                }
                if (isNew && cvm.isAvailable)
                {
                    Country country = new Country {
                        IsoCode = cvm.Country.IsoCode, Name = cvm.Country.Name
                    };
                    courseCountries.Add(country);
                }
            }
            //Add New Countries
            course.Countries   = courseCountries;
            course.Subject     = courseModel.Course.Subject;
            course.Code        = courseModel.Course.Code;
            course.Language    = courseModel.Course.Language;
            course.StartDate   = courseModel.Course.StartDate;
            course.EndDate     = courseModel.Course.EndDate;
            course.Price       = courseModel.Course.Price;
            course.Teacher     = teacher;
            course.ClassUrl    = courseModel.Course.ClassUrl;
            course.MeetingId   = courseModel.Course.MeetingId;
            course.MeetingPass = courseModel.Course.MeetingPass;
            course.Ended       = courseModel.Course.Ended;
            course.Hidden      = courseModel.Course.Hidden;
            _context.SaveChanges();

            if (courseModel.Picture != null && courseModel.Picture.Length > 0)
            {
                _files.AddCoursePicture(course, courseModel.Picture);
            }

            if (courseModel.Background != null && courseModel.Background.Length > 0)
            {
                _files.AddCourseBackground(course, courseModel.Background);
            }


            return(RedirectToAction("CourseArea", new { courseId }));
        }