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); }
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 string oldImage = Path.Combine(uploads, news.ImageLink); if (System.IO.File.Exists(oldImage) && news.ImageLink != Path.Combine(Configurations.NewsImageStockagePath,Configurations.DefaultFileName)) System.IO.File.Delete(Path.Combine(uploads, news.ImageLink)); //Image uploading string fileName = Guid.NewGuid().ToString() + "_" + ContentDispositionHeaderValue.Parse(uploadFile.ContentDisposition).FileName.Trim('"'); await uploadFile.SaveAsAsync(Path.Combine(uploads, fileName)); //Setting new value, saving news.ImageLink = Path.Combine(Configurations.NewsImageStockagePath, fileName); } var appUser = await GetCurrentUserAsync(); User user; user = _context.Consumers.FirstOrDefault(x => x.Email == appUser.Email); if (user == null) { user = _context.Producers.FirstOrDefault(x => x.Email == appUser.Email); } news.User = user; _context.Update(news); _context.SaveChanges(); return RedirectToAction("Index"); } return View(news); }