/*
         * author:邓帅
         * QQ:540043604
         */

        /// <summary>
        /// 按courseId删除Seminar.
        /// 先根据CourseId获得所有的seminar的信息,然后根据seminar信息删除相关topic的记录,然后再根据SeminarId删除SeminarGroup表记录,最后再将seminar的信息删除
        /// </summary>
        /// <param name="courseId">课程Id</param>
        /// <seealso cref="M:Xmu.Crms.Shared.Service.ISeminarService.ListSeminarByCourseId(System.Int64)"/>
        /// <seealso cref="M:Xmu.Crms.Shared.Service.ITopicService.DeleteTopicBySeminarId(System.Int64)"/>
        /// <seealso cref="M:Xmu.Crms.Shared.Service.ISeminarGroupService.DeleteSeminarGroupBySeminarId(System.Int64)"/>
        /// <exception cref="T:System.ArgumentException">格式错误时抛出</exception>
        /// <exception cref="T:Xmu.Crms.Shared.Exceptions.CourseNotFoundException">该课程不存在时抛出</exception>
        public void DeleteSeminarByCourseId(long courseId)
        {
            if (courseId < 0)
            {
                throw new ArgumentException();
            }
            if (_db.Course.Find(courseId) == null)
            {
                throw new CourseNotFoundException();
            }

            var            course = _db.Course.Find(courseId);
            List <Seminar> seminars;

            seminars = ListSeminarByCourseId(courseId).ToList();

            foreach (Seminar seminar in seminars)
            {
                _seminarGroupService.DeleteSeminarGroupBySeminarId(seminar.Id);
                _topicService.DeleteTopicBySeminarId(seminar.Id);
            }
            _db.Seminar.RemoveRange(seminars);

            _db.SaveChanges();
        }