public async Task <IActionResult> Create(NewsPost newsPost)
        {
            if (!ModelState.IsValid)
            {
                return(View(newsPost));
            }

            if (newsPost.Photo == null)
            {
                ModelState.AddModelError("Photo", "Zəhmət olmasa, şəkil yükləyin");
                return(View(newsPost));
            }

            if (newsPost.Photo.ContentType.Contains("image/"))
            {
                string folderPath = Path.Combine(_env.WebRootPath, "img");
                string fileName   = Guid.NewGuid().ToString() + "_" + newsPost.Photo.FileName;
                string filePath   = Path.Combine(folderPath, fileName);

                using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
                {
                    await newsPost.Photo.CopyToAsync(fileStream);
                }

                CustomUser customUserFromDb = await _userManager.FindByNameAsync(User.Identity.Name);

                NewsPost newNewsPost = new NewsPost()
                {
                    PhotoURL     = fileName,
                    Title        = newsPost.Title,
                    ShortInfo    = newsPost.ShortInfo,
                    MainArticle  = newsPost.MainArticle,
                    PublishDate  = DateTime.Now,
                    CustomUserId = customUserFromDb.Id
                };

                _context.NewsPosts.Add(newNewsPost);
                await _context.SaveChangesAsync();
            }

            return(RedirectToAction("Posts", "News"));
        }
        public async Task <IActionResult> Confirmation(string userId)
        {
            if (userId != null)
            {
                CustomUser customUser = await _userManager.FindByIdAsync(userId);

                if (customUser != null)
                {
                    customUser.EmailConfirmed = true;
                    await _context.SaveChangesAsync();

                    TempData["Succeed"] = true;
                }
            }

            return(View());
        }
        public async Task <IActionResult> Create(AnnouncementVM announcementVM)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.Brands        = _context.Brands;
                ViewBag.Models        = _context.Models;
                ViewBag.Colors        = _context.Colors;
                ViewBag.Locations     = _context.Locations;
                ViewBag.Fuels         = _context.Fuels;
                ViewBag.SpeedControls = _context.SpeedControls;
                return(View(announcementVM));
            }

            if (announcementVM.Photo == null)
            {
                ViewBag.Brands        = _context.Brands;
                ViewBag.Models        = _context.Models;
                ViewBag.Colors        = _context.Colors;
                ViewBag.Locations     = _context.Locations;
                ViewBag.Fuels         = _context.Fuels;
                ViewBag.SpeedControls = _context.SpeedControls;
                ModelState.AddModelError("Photo", "Zəhmət olmasa, şəkil yükləyin");
                return(View(announcementVM));
            }

            if (announcementVM.Photo.ContentType.Contains("image/"))
            {
                string folderPath = Path.Combine(_env.WebRootPath, "img");
                string fileName   = Guid.NewGuid().ToString() + "_" + announcementVM.Photo.FileName;
                string filePath   = Path.Combine(folderPath, fileName);

                using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
                {
                    await announcementVM.Photo.CopyToAsync(fileStream);
                }

                automobile = new Automobile
                {
                    MainPhotoURL   = fileName,
                    Price          = announcementVM.Price,
                    ModelId        = announcementVM.ModelId,
                    Year           = announcementVM.Year,
                    Motor          = announcementVM.Motor,
                    Distance       = announcementVM.Distance,
                    ColorId        = announcementVM.ColorId,
                    FuelId         = announcementVM.FuelId,
                    SpeedControlId = announcementVM.SpeedControlId,
                    ShortInfo      = announcementVM.ShortInfo
                };

                await _context.Automobiles.AddAsync(automobile);

                await _context.SaveChangesAsync();
            }


            if (announcementVM.Photos != null && announcementVM.Photos.Count() > 0)
            {
                foreach (IFormFile photo in announcementVM.Photos)
                {
                    if (photo.ContentType.Contains("image/"))
                    {
                        string folderPath = Path.Combine(_env.WebRootPath, "img");
                        string fileName   = Guid.NewGuid().ToString() + "_" + photo.FileName;
                        string filePath   = Path.Combine(folderPath, fileName);

                        using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
                        {
                            await photo.CopyToAsync(fileStream);
                        }

                        AutoPhoto autoPhoto = new AutoPhoto
                        {
                            PhotoURL     = fileName,
                            AutomobileId = automobile.Id
                        };

                        await _context.AutoPhotos.AddAsync(autoPhoto);

                        await _context.SaveChangesAsync();
                    }
                }
            }



            CustomUser customUserFromDb = await _userManager.FindByNameAsync(User.Identity.Name);

            Announcement announcement = new Announcement
            {
                AutomobileId = automobile.Id,
                PublishDate  = DateTime.Now,
                LocationId   = announcementVM.LocationId,
                CustomUserId = customUserFromDb.Id,
                IsVIP        = announcementVM.IsVIP
            };

            await _context.Announcements.AddAsync(announcement);

            await _context.SaveChangesAsync();

            TempData["Announcement created"] = true;

            ViewBag.Brands        = _context.Brands;
            ViewBag.Models        = _context.Models;
            ViewBag.Colors        = _context.Colors;
            ViewBag.Locations     = _context.Locations;
            ViewBag.Fuels         = _context.Fuels;
            ViewBag.SpeedControls = _context.SpeedControls;

            return(RedirectToAction("Index", "Home"));
        }