private NewsArticleView getNewsArticlesView(NewsArticle newsArticle, SelectList newsCategoriesList)
        {
            NewsArticleView newsArticleView = new NewsArticleView()
            {
                NewsArticle            = newsArticle,
                NewsCategoriesList     = newsCategoriesList,
                SelectedNewsCategories = newsArticle.NewsArticleCategories.Select(sc => sc.NewsCategoryID)
            };

            return(newsArticleView);
        }
        public IActionResult Edit(int id, NewsArticleView updatedNewsArticle, IFormFile image)
        {
            if (id != updatedNewsArticle.NewsArticle.NewsArticleID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                // First remove all the current couplings between this article and categories
                var rml = _context.NewsArticleCategory.Where(sc => sc.NewsArticleID == id);
                _context.RemoveRange(rml);
                _context.SaveChanges();

                // Add all the selected categories to the current article
                updatedNewsArticle.NewsArticle.NewsArticleCategories = new List <NewsArticleCategory>();

                if (updatedNewsArticle.SelectedNewsCategories != null)
                {
                    updatedNewsArticle.SelectedNewsCategories.ToList().ForEach(x => _context.NewsArticleCategory.Add(new NewsArticleCategory()
                    {
                        NewsArticleID = updatedNewsArticle.NewsArticle.NewsArticleID, NewsCategoryID = x
                    }));
                }

                // Update the image path if an image is selected
                if (image != null)
                {
                    string   filename  = ChangePathName(updatedNewsArticle.NewsArticle.ImagePath);
                    FileInfo fi        = new FileInfo(image.FileName);
                    string   extension = fi.Extension;
                    string   path      = Basepath + filename + extension;
                    updatedNewsArticle.NewsArticle.ImagePath = path;
                    using (FileStream fs = System.IO.File.Create("wwwroot/" + path))
                    {
                        image.CopyTo(fs);
                        fs.Flush();
                    }
                }

                _context.NewsArticle.Update(updatedNewsArticle.NewsArticle);
                _context.SaveChanges();

                return(RedirectToAction("Index"));
            }

            return(View(updatedNewsArticle.NewsArticle));
        }
        public IActionResult Create(NewsArticleView newNewsArticle, IFormFile image)
        {
            if (ModelState.IsValid && image != null)
            {
                // Upload an image and add the path to the article object
                string   filename  = ChangePathName(newNewsArticle.NewsArticle.ImagePath);
                FileInfo fi        = new FileInfo(image.FileName);
                string   extension = fi.Extension;
                string   path      = Basepath + filename + extension;
                newNewsArticle.NewsArticle.ImagePath = path;
                using (FileStream fs = System.IO.File.Create("wwwroot" + path))
                {
                    image.CopyTo(fs);
                    fs.Flush();
                }

                _context.Add(newNewsArticle.NewsArticle);
                _context.SaveChanges();

                if (newNewsArticle.SelectedNewsCategories != null)
                {
                    foreach (int id in newNewsArticle.SelectedNewsCategories)
                    {
                        _context.NewsArticleCategory.Add(new NewsArticleCategory()
                        {
                            NewsArticleID  = newNewsArticle.NewsArticle.NewsArticleID,
                            NewsCategoryID = id
                        });
                    }
                }

                _context.SaveChanges();

                return(RedirectToAction("Index"));
            }

            return(View(newNewsArticle));
        }