public IActionResult Create(int ID, int ChapterId) { var vm = new CourseLessonVM(); if (ID != 0) { var model = _courseLessonService.GetById(ID); if (model == null) { return(NotFound()); } vm = _mapper.Map <CourseLessonVM>(model); if (vm.VideoPath != null) { var fileVideo = Path.Combine(Directory.GetCurrentDirectory(), vm.VideoPath); if (!CheckFile(fileVideo)) { vm.VideoDisplay = Path.GetFileName(fileVideo); } } if (vm.SlidePath != null) { var fileSlide = Path.Combine(Directory.GetCurrentDirectory(), vm.SlidePath); if (!CheckFile(fileSlide)) { vm.SlideDisplay = Path.GetFileName(fileSlide); } } } else { var numLesson = _courseLessonService.CountCondition(m => m.ChapterId == ChapterId); vm.ChapterId = ChapterId; vm.SortOrder = ++numLesson; } return(View(vm)); }
public async Task <IActionResult> Detail(CourseLessonVM vm) { var idCourse = _chapterService.GetCondition(m => m.Id == vm.ChapterId).Select(m => m.CourseId).FirstOrDefault(); if (ModelState.IsValid) { try { if (vm.Video != null) { string nameVideo = Path.GetFileName(vm.Video.FileName); string pathUpload = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\resource\videos\" + idCourse); string savePath = Path.Combine(pathUpload, nameVideo); string saveDBPath = Path.Combine(@"\resource\videos\" + idCourse, nameVideo); if (!Directory.Exists(pathUpload)) { Directory.CreateDirectory(pathUpload); } using (var stream = new FileStream(savePath, FileMode.Create)) { await vm.Video.CopyToAsync(stream); } vm.VideoPath = saveDBPath; } if (vm.Slide != null) { string nameVideo = Path.GetFileName(vm.Slide.FileName); string pathUpload = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\resource\slides\" + idCourse); string savePath = Path.Combine(pathUpload, nameVideo); string saveDBPath = Path.Combine(@"\resource\slides\" + idCourse, nameVideo); if (!Directory.Exists(pathUpload)) { Directory.CreateDirectory(pathUpload); } using (var stream = new FileStream(savePath, FileMode.Create)) { await vm.Slide.CopyToAsync(stream); } vm.SlidePath = saveDBPath; } if (vm.Id == 0) { vm.Status = false; var model = _mapper.Map <CourseLessons>(vm); await _courseLessonService.CreateAsync(model); return(RedirectToAction("Detail", new { ID = idCourse })); } else { vm.Status = false; var model = _mapper.Map <CourseLessons>(vm); await _courseLessonService.UpdateAsync(model); return(RedirectToAction("Detail", new { ID = idCourse })); } } catch (Exception) { return(RedirectToAction("Detail", new { ID = idCourse })); } } return(RedirectToAction("Detail", new { ID = idCourse })); }
public async Task <IActionResult> Create(CourseLessonVM vm) { if (ModelState.IsValid) { string pathVideo = vm.VideoPath; string pathSlide = vm.SlidePath; try { var idCourse = _chapterService.GetCondition(m => m.Id == vm.ChapterId).Select(m => m.CourseId).FirstOrDefault(); if (vm.Video != null) { string nameVideo = Path.GetFileName(vm.Video.FileName); string pathUpload = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\resource\videos\" + idCourse); if (CheckFile(Path.Combine(pathUpload, nameVideo))) { string nameTempVideo = Path.GetFileNameWithoutExtension(nameVideo); nameVideo = nameTempVideo + DateTime.Now.ToString("yyyyMMddhhmmssfff") + Path.GetExtension(nameVideo); } string savePath = Path.Combine(pathUpload, nameVideo); string saveDBPath = Path.Combine(@"\resource\videos\" + idCourse, nameVideo); if (!Directory.Exists(pathUpload)) { Directory.CreateDirectory(pathUpload); } using (var stream = new FileStream(savePath, FileMode.CreateNew)) { await vm.Video.CopyToAsync(stream); } vm.VideoPath = saveDBPath; } if (vm.Slide != null) { string nameVideo = Path.GetFileName(vm.Slide.FileName); string pathUpload = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\resource\slides\" + idCourse); string savePath = Path.Combine(pathUpload, nameVideo); string saveDBPath = Path.Combine(@"\resource\slides\" + idCourse, nameVideo); if (!Directory.Exists(pathUpload)) { Directory.CreateDirectory(pathUpload); } using (var stream = new FileStream(savePath, FileMode.Create)) { await vm.Slide.CopyToAsync(stream); } vm.SlidePath = saveDBPath; } if (vm.Id == 0) { vm.Status = false; var model = _mapper.Map <CourseLessons>(vm); await _courseLessonService.CreateAsync(model); return(Json(new { status = true })); } else { vm.Status = false; string name = Directory.GetCurrentDirectory(); if (!string.IsNullOrEmpty(pathVideo) && !string.IsNullOrEmpty(vm.VideoPath)) { string urlVideo = name + @"\wwwroot\" + pathVideo; if (CheckFile(urlVideo)) { System.IO.File.Delete(urlVideo); } } if (!string.IsNullOrEmpty(pathSlide) && !string.IsNullOrEmpty(vm.SlidePath)) { string urlSlide = name + @"\wwwroot\" + pathSlide; if (CheckFile(urlSlide)) { System.IO.File.Delete(urlSlide); } } var model = _mapper.Map <CourseLessons>(vm); await _courseLessonService.UpdateAsync(model); return(Json(new { status = true })); } } catch (Exception ex) { return(Json(new { status = false })); } } return(Json(new { status = false })); }