// GET: Article/Edit/5 public async Task <IActionResult> Edit(int?id, ViewModelBoth viewModelBoth) { if (id == null) { return(NotFound()); } viewModelBoth.Article = await _context.Articles.FindAsync(id); if (viewModelBoth.Article == null) { return(NotFound()); } return(View(viewModelBoth)); }
public async Task <IActionResult> Edit(int id, [Bind("ArticleId,ArticleTitle,desc_mini,desc,ArticleAdress,Articlecoor,ArticleContact,ImagePath,ImageName,AuthorId,AuthorName,FlagCount,Status")] Article article, ViewModelBoth viewModelBoth, IFormCollection formFields) { if (id != article.ArticleId) { return(NotFound()); } if (ModelState.IsValid) { try { //grab the article straight from db var articleFromDb = await _context.Articles.FirstOrDefaultAsync(k => k.ArticleId == article.ArticleId); var authorId = _context.Articles.FirstOrDefault(x => x.ArticleId == article.ArticleId).AuthorId; //assign the articlefromdb properties to what the user edited articleFromDb.ArticleTitle = article.ArticleTitle; articleFromDb.ArticleAdress = article.ArticleAdress; articleFromDb.ArticleContact = article.ArticleContact; articleFromDb.desc = article.desc; articleFromDb.desc_mini = article.desc_mini;; articleFromDb.ArticleLat = article.ArticleLat; articleFromDb.ArticleLng = article.ArticleLng; articleFromDb.Status = ContactStatus.Approved; var w = formFields["Category"]; switch (w) { case "1": article.Category = "Business"; break; case "2": article.Category = "Entertainment"; break; case "3": article.Category = "Health"; break; } var uploads = Path.Combine(_hostingEnvironment.WebRootPath, "Image", authorId, articleFromDb.ArticleId.ToString()); foreach (var file in viewModelBoth.Files) { if (file.Length > 0) { // TODO: change the filename so it doesnt save the original user one, could be malicious or bad idea //making the img name string filename = $"{article.ArticleTitle}{DateTime.Now.ToString("ssddmmyyyy")}{Path.GetExtension(file.FileName)}"; //assigning values articleFromDb.ImageName = filename; articleFromDb.ImagePath = uploads; //directing path for file var filePath = Path.Combine(uploads, filename); //streaming file using (var fileStream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(fileStream); } } } //put back the articlefromdb back to db _context.Update(articleFromDb); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ArticleExists(article.ArticleId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(article)); }
public async Task <IActionResult> Create([Bind("ArticleId,ArticleTitle,desc_mini,desc,ArticleAdress,ArticleLat,ArticleLng,ArticleContact,ImagePath,ImageName,AuthorId,AuthorName,FlagCount,Status")] Article article, ViewModelBoth viewModelBoth, IFormCollection formFields) { if (ModelState.IsValid) { //id stuff var userId = _userManager.GetUserId(HttpContext.User); var userName = _userManager.GetUserName(HttpContext.User); article.AuthorId = userId; article.AuthorName = userName; article.Status = ContactStatus.Pending; article.LikeCount = 0; article.DislikeCount = 0; article.FlagCount = 0; var w = formFields["Category"]; switch (w) { case "1": article.Category = "Business"; break; case "2": article.Category = "Entertainment"; break; case "3": article.Category = "Health"; break; } //img upload stuff var uploads = Path.Combine(_hostingEnvironment.WebRootPath, "Image", userId, article.ArticleTitle); Directory.CreateDirectory(uploads); foreach (var file in viewModelBoth.Files) { if (file.Length > 0) { // TODO: change the filename so it doesnt save the original user one, could be malicious or bad idea //making the img name string filename = $"{article.ArticleTitle}{DateTime.Now.ToString("ssddmmyyyy")}{Path.GetExtension(file.FileName)}"; //assigning values article.ImageName = filename; article.ImagePath = uploads; //directing path for file var filePath = Path.Combine(uploads, filename); //streaming file using (var fileStream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(fileStream); _context.Add(article); } } } await _context.SaveChangesAsync(); //renaming directory to a more secure one var id = article.ArticleId.ToString(); var SecUploads = Path.Combine(_hostingEnvironment.WebRootPath, "Image", userId, id); Directory.Move(uploads, SecUploads); return(RedirectToAction(nameof(Index))); } return(View(viewModelBoth)); }