public IActionResult Update(SliderUploadVM SliderUploadVM, IFormFile file) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (file != null) { using (Stream stream = file.OpenReadStream()) { using (BinaryReader br = new BinaryReader(stream)) { var fileContent = br.ReadBytes((int)file.Length); SliderUploadVM.Image = SaveFileHelper.SaveFileToDisk(_env, fileContent, file.FileName, file.ContentType); } } } Slider _sliderDb = _sliderRepository.GetSingle(SliderUploadVM.Id); if (_sliderDb == null) { return(NotFound()); } Mapper.Map(SliderUploadVM, _sliderDb); _sliderRepository.SaveChanges(); return(new NoContentResult()); }
public IActionResult Create(SliderUploadVM SliderUploadVM, IFormFile file) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (file == null || file.Length == 0) { return(BadRequest("Your file was not valid!")); } using (Stream stream = file.OpenReadStream()) { using (BinaryReader br = new BinaryReader(stream)) { var fileContent = br.ReadBytes((int)file.Length); SliderUploadVM.Image = SaveFileHelper.SaveFileToDisk(_env, fileContent, file.FileName, file.ContentType); } } Slider _newSlider = Mapper.Map <Slider>(SliderUploadVM); _sliderRepository.Add(_newSlider); _sliderRepository.SaveChanges(); CreatedAtRouteResult result = CreatedAtRoute("GetCategory", new { controller = "Categories", id = SliderUploadVM.Id }, SliderUploadVM); return(result); }