public async Task <IActionResult> Create(CertificateFormViewModel certificateFormViewModel) { if (ModelState.IsValid) { var certificate = _mapper.Map <CertificateFormViewModel, Certificate>(certificateFormViewModel); certificate.FilesPathGuid = Guid.NewGuid(); _unitOfWork.CertificateRepository.Insert(certificate); await _unitOfWork.Complete(); var files = HttpContext.Request.Form.Files; if (files.Count > 0) { await _fileHandler.UploadMedia( files, _webHostEnvironment.WebRootPath, certificate.FilesPathGuid.ToString(), FileHandlerFolder.Certificates); } return(RedirectToAction(nameof(Index))); } return(View(certificateFormViewModel)); }
public async Task <IActionResult> Edit(int id, CertificateFormViewModel certificateFormViewModel) { if (id != certificateFormViewModel.Id) { return(BadRequest()); } if (ModelState.IsValid) { var certificate = _mapper.Map <CertificateFormViewModel, Certificate>(certificateFormViewModel); _unitOfWork.CertificateRepository.Update(certificate); await _unitOfWork.Complete(); var files = HttpContext.Request.Form.Files; if (files.Count > 0) { //Delete old file _fileHandler.DeleteMedia( _webHostEnvironment.WebRootPath, certificate.FilesPathGuid.ToString(), FileHandlerFolder.Certificates ); //Upload new file await _fileHandler.UploadMedia( files, _webHostEnvironment.WebRootPath, certificate.FilesPathGuid.ToString(), FileHandlerFolder.Certificates); } return(RedirectToAction(nameof(Index))); } return(View(certificateFormViewModel)); }