Example #1
0
        public async Task <IActionResult> Create(News news)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Login", "Account"));
            }

            if (!User.IsInRole("Admin"))
            {
                return(RedirectToAction("Login", "Account"));
            }
            if (!ModelState.IsValid)
            {
                ViewBag.Active = "Home";

                return(View(news));
            }

            //if (ModelState["Photo"].ValidationState == ModelValidationState.Invalid)
            //{
            //    ViewBag.Active = "Home";

            //    return View(news);
            //}

            //if (!news.Photo.IsImage())
            //{
            //    ViewBag.Active = "Home";

            //    ModelState.AddModelError("Photo", "File type should be image");
            //    return View(news);
            //}

            //string filename = await news.Photo.SaveAsync(_env.WebRootPath);
            //news.PhotoURL = filename;

            News mynews = new News()
            {
                Title     = news.Title,
                ShortInfo = news.ShortInfo,
                MainInfo  = news.MainInfo,
                Time      = DateTime.Now,
                BrandsId  = news.BrandsId
            };

            await _context.News.AddAsync(mynews);

            bool isMain = true;

            foreach (var p in news.AllPhotos)
            {
                if (p != null)
                {
                    if (p.ContentType.Contains("image/"))
                    {
                        string filenamePhotos = await p.SaveAsync(_env.WebRootPath);

                        NewsPhotos img = new NewsPhotos()
                        {
                            NewsId   = mynews.Id,
                            PhotoURL = filenamePhotos,
                        };
                        if (isMain == true)
                        {
                            img.IsMain = true;
                        }
                        isMain = false;

                        await _context.NewsPhotos.AddAsync(img);
                    }
                }
            }

            ViewBag.Active = "Home";

            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
Example #2
0
        public async Task <IActionResult> Create(News news)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Login", "Account"));
            }

            if (!User.IsInRole("Admin"))
            {
                return(RedirectToAction("Login", "Account"));
            }
            if (!ModelState.IsValid)
            {
                ViewBag.Active = "Home";

                return(View(news));
            }

            if (news.AllPhotos == null)
            {
                ViewBag.Active = "Home";

                return(View(news));
            }

            News mynews = new News()
            {
                Title      = news.Title,
                ShortInfo  = news.ShortInfo,
                MainInfo   = news.MainInfo,
                Time       = DateTime.Now,
                CategoryId = news.CategoryId
            };

            await _context.News.AddAsync(mynews);

            bool isMain = true;

            foreach (var p in news.AllPhotos)
            {
                if (p != null)
                {
                    if (p.ContentType.Contains("image/"))
                    {
                        string filenamePhotos = await p.SaveAsync(_env.WebRootPath, "news");

                        NewsPhotos img = new NewsPhotos()
                        {
                            NewsId   = mynews.Id,
                            PhotoURL = filenamePhotos,
                        };
                        if (isMain == true)
                        {
                            img.IsMain = true;
                        }
                        isMain = false;

                        await _context.NewsPhotos.AddAsync(img);
                    }
                }
            }

            ViewBag.Active = "Home";

            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }