public IActionResult GetIntroduction(string stepIndex) { _loggerManager.Info($"GetIntroduction({stepIndex}) was requested"); if (!_planRepository.GetStepList().Contains(stepIndex)) { _loggerManager.Warn($"GetIntroduction({stepIndex}) : Step index is wrong"); return(BadRequest()); } var introduction = _planRepository.GetIntroduction(stepIndex); if (introduction == null) { introduction = new IntroductionDTO { Step = stepIndex }; } return(View("Introduction", introduction)); }
public IActionResult UploadIntroduction(IntroductionDTO introduction) { _loggerManager.Info($"UploadIntroduction was requested"); var files = HttpContext.Request.Form.Files; if (files == null || files.Count != 1) { ModelState.AddModelError(string.Empty, sharedResource.manageIntroductionInvalidFile); _loggerManager.Warn($"UploadIntroduction : A file is invalid"); return(View("Introduction", introduction)); } //validates step's index if (!_planRepository.GetStepList().Contains(introduction.Step)) { _loggerManager.Warn($"UploadIntroduction : Step index is wrong"); return(BadRequest()); } var uploadlimit = int.Parse(_settingRepository.Get(Settings.FileUploadLimit)); if (!ValidationHelper.ValidateFileSize(files[0], uploadlimit)) { ModelState.AddModelError(string.Empty, sharedResource.manageIntroductionFileLimitError); _loggerManager.Warn($"UploadIntroduction : A file exceed a limit"); return(View("Introduction", introduction)); } var userId = HttpContext.GetUserId(); //upload a file in wwwroot directory var uploadRelPath = UploadHelper.Upload(files[0]); //creates a new file record var fileDto = _fileRepository.CreateNewFile(Path.GetFileNameWithoutExtension(files[0].FileName), Path.GetExtension(files[0].FileName), uploadRelPath, userId); //boolean that determines a result for updating of an introduction var result = false; if (fileDto != null)//A file record created, updates an introduction record { _loggerManager.Info($"UploadIntroduction : A file was created"); //takes an older video version var oldVideo = _planRepository.GetIntroduction(introduction.Step)?.Video; //updaates an introduction for a step result = _planRepository.UpdateIntroduction(introduction.Step, fileDto.Id, userId); if (result && oldVideo != null)//If a update was succeessful and the introduction had a video before, deletes an older video { //a result boolean bool oldFileDelete; //Deletes corresponding record in a repository and corresponding file in a directory, If any fails a result boolean is false oldFileDelete = UploadHelper.Delete(oldVideo.Path); oldFileDelete = _fileRepository.Delete(oldVideo.Id) && oldFileDelete; if (oldFileDelete) { _loggerManager.Warn($"UploadIntroduction : An old file was deleted"); } else { _loggerManager.Warn($"UploadIntroduction : An old file was not deleted"); } } } if (!result) { ModelState.AddModelError(string.Empty, sharedResource.manageServerError); _loggerManager.Warn($"UploadIntroduction was unable to update"); return(View("Introduction", introduction)); } else { _loggerManager.Info($"UploadIntroduction successfully updated"); } return(RedirectToAction("GetIntroduction", new { stepIndex = introduction.Step })); }