public async Task<IActionResult> Create(News news, IFormFile uploadFile) { if (ModelState.IsValid) { string fileName = Configurations.DefaultFileName; if (uploadFile != null) { //Image uploading string uploads = Path.Combine(_environment.WebRootPath, Configurations.NewsImageStockagePath); fileName = Guid.NewGuid().ToString() + "_" + ContentDispositionHeaderValue.Parse(uploadFile.ContentDisposition).FileName.Trim('"'); await uploadFile.SaveImageAsAsync(Path.Combine(uploads, fileName)); } //Setting value for creation news.Id = Guid.NewGuid(); news.DateOfPublication = DateTime.Now; news.ImageLink = Path.Combine(Configurations.NewsImageStockagePath,fileName); //TODO Get logged in User and add it to the news StolonsUser user = await GetCurrentStolonsUserAsync(); news.User = user; _context.News.Add(news); _context.SaveChanges(); return RedirectToAction("Index"); } return View(news); }
public async Task<IActionResult> Edit(News news,IFormFile uploadFile) { if (ModelState.IsValid) { if (uploadFile != null) { string uploads = Path.Combine(_environment.WebRootPath, Configurations.NewsImageStockagePath); //Deleting old image DeleteImage(news.ImageLink); //Image uploading string fileName = Guid.NewGuid().ToString() + "_" + ContentDispositionHeaderValue.Parse(uploadFile.ContentDisposition).FileName.Trim('"'); await uploadFile.SaveImageAsAsync(Path.Combine(uploads, fileName)); //Setting new value, saving news.ImageLink = Path.Combine(Configurations.NewsImageStockagePath, fileName); } StolonsUser user = await GetCurrentStolonsUserAsync(); news.User = user; _context.Update(news); _context.SaveChanges(); return RedirectToAction("Index"); } return View(news); }