public IActionResult DeleteLecture(int lectureNumber, string lectureId) { var lecture = _lectureService.GetLecture(lectureId); if (lecture == null || lecture.Number != lectureNumber) { return(BadRequest("Error Happened")); } var courseId = lecture.CourseId; _lectureService.RemoveAllVideos(lectureId); var files = _lectureService.GetLectureFiles(lectureId); var homeworkDelivery = _lectureService.GetLectureHomeworkDelivery(lectureId); foreach (var file in files) { try { System.IO.File.Delete(file.RootPath); _context.Remove(file); _context.SaveChanges(); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine($"Lecture: {lecture.Number}, id: {lectureId}, file: {file.Name} not deleted"); } } foreach (var file in homeworkDelivery) { try { System.IO.File.Delete(file.RootPath); _context.Remove(file); _context.SaveChanges(); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine($"Lecture: {lecture.Number}, id: {lectureId}, homework: {file.Name} not deleted"); } } _context.Remove(lecture); _context.SaveChanges(); _courseService.ReorderCourseLectures(courseId); return(Ok("dataSavedSuccessfully")); }