public async Task <IActionResult> DeleteAdsImage(int?ImageId)
        {
            AnsImages deletingImage = _context.AnsImages.FirstOrDefault(i => i.Id == ImageId);

            if (deletingImage.Id != ImageId)
            {
                return(RedirectToAction("Error", "Home"));
            }


            _context.AnsImages.Remove(deletingImage);
            await _context.SaveChangesAsync();

            return(RedirectToAction("EditAds", "Account"));
        }
        public async Task <IActionResult> EditAdsPost(Announcement selectedAds)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.Categories = _context.Categories;
                ViewBag.Marks      = _context.Marks;
                ViewBag.Models     = _context.Models;
                ViewBag.Cities     = _context.Cities;
                ModelState.AddModelError("", "Xaiş olunur duzgun deyerler daxil edesiniz");
                return(View(selectedAds));
            }

            Announcement adsFromDb = await _context.Announcements.FindAsync(selectedAds.Id);

            ApplicationUser loggedUser = await _userManager.FindByNameAsync(User.Identity.Name);

            if (loggedUser == null || adsFromDb.ApplicationUserId != loggedUser.Id)
            {
                ViewBag.Categories = _context.Categories;
                ViewBag.Marks      = _context.Marks;
                ViewBag.Models     = _context.Models;
                ViewBag.Cities     = _context.Cities;
                ModelState.AddModelError("", "siz active istifadeci deyilsiniz");
                return(View(selectedAds));
            }


            if (adsFromDb == null)
            {
                return(RedirectToAction("Error", "Home"));
            }
            if (selectedAds.Photo != null)
            {
                if (selectedAds.Photo.ContentType.Contains("image/"))
                {
                    string currentFilePath = Path.Combine(_env.WebRootPath, "image", adsFromDb.PhotoUrl);
                    if (System.IO.File.Exists(currentFilePath))
                    {
                        System.IO.File.Delete(currentFilePath);
                    }

                    string folderPath = Path.Combine(_env.WebRootPath, "img");
                    string fileName   = Guid.NewGuid().ToString() + "_" + selectedAds.Photo.FileName;
                    string filePath   = Path.Combine(folderPath, fileName);
                    adsFromDb.PhotoUrl = fileName;

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



            if (selectedAds.AnsAllImages != null)
            {
                int dbImageCount = adsFromDb.AnsImages != null ? adsFromDb.AnsImages.Count : 0;
                if ((dbImageCount + selectedAds.AnsAllImages.Count) > 4)
                {
                    ViewBag.Categories = _context.Categories;
                    ViewBag.Marks      = _context.Marks;
                    ViewBag.Models     = _context.Models;
                    ViewBag.Cities     = _context.Cities;
                    ModelState.AddModelError("", "Maksimum 4 şekil daxil ede bilersiniz");
                    selectedAds.AnsImages = adsFromDb.AnsImages;
                    return(View(selectedAds));
                }
                foreach (var adsImage in selectedAds.AnsAllImages)
                {
                    if (adsImage == null)
                    {
                        ViewBag.Categories = _context.Categories;
                        ViewBag.Marks      = _context.Marks;
                        ViewBag.Models     = _context.Models;
                        ViewBag.Cities     = _context.Cities;
                        ModelState.AddModelError("", "Zəhmət olmasa şekilleri daxil edin");
                    }
                    if (adsImage.ContentType.Contains("image/"))
                    {
                        string folderPathAll = Path.Combine(_env.WebRootPath, "img");
                        string fileNameAll   = Guid.NewGuid().ToString() + "_" + adsImage.FileName;
                        string filePathALL   = Path.Combine(folderPathAll, fileNameAll);

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

                        AnsImages img = new AnsImages()
                        {
                            AnnouncementId = adsFromDb.Id,
                            AdsPhotoUrl    = fileNameAll
                        };
                        _context.AnsImages.Add(img);
                    }
                }
            }
            adsFromDb.Name              = selectedAds.Name;
            adsFromDb.Price             = selectedAds.Price;
            adsFromDb.Info              = selectedAds.Info;
            adsFromDb.ModelId           = selectedAds.ModelId;
            adsFromDb.CityId            = selectedAds.CityId;
            adsFromDb.ApplicationUserId = loggedUser.Id;
            _context.SaveChanges();

            return(RedirectToAction("UserProfile", "Account"));
        }
        public async Task <IActionResult> CreateAds(adsVM adsCreateVM)
        {
            ApplicationUser user = await _userManager.FindByNameAsync(User.Identity.Name);

            adsCreateVM.Announcement.ApplicationUserId = user.Id;

            ViewBag.Cities     = _context.Cities;
            ViewBag.Marks      = _context.Marks;
            ViewBag.Categories = _context.Categories;
            ViewBag.Models     = _context.Models;

            if (!ModelState.IsValid)
            {
                ViewBag.Cities     = _context.Cities;
                ViewBag.Marks      = _context.Marks;
                ViewBag.Categories = _context.Categories;
                ViewBag.Models     = _context.Models;
                ModelState.AddModelError("", "Zəhmət Olmasa Düzgün məlumatlar daxil edin");
                return(View(adsCreateVM));
            }

            if (adsCreateVM.Announcement.Photo == null)
            {
                ViewBag.Cities     = _context.Cities;
                ViewBag.Marks      = _context.Marks;
                ViewBag.Categories = _context.Categories;
                ViewBag.Models     = _context.Models;
                ModelState.AddModelError("", "Zəhmət Olmasa  əsas şəkil daxil Edin");
                return(View(adsCreateVM));
            }



            if (adsCreateVM.Announcement.AnsAllImages != null)
            {
                if (adsCreateVM.Announcement.AnsAllImages.Count > 4)
                {
                    ViewBag.Categories = _context.Categories;
                    ViewBag.Marks      = _context.Marks;
                    ViewBag.Models     = _context.Models;
                    ViewBag.Cities     = _context.Cities;
                    ModelState.AddModelError("", "Maksimum 4 şekil daxil ede bilersiniz");
                    return(View(adsCreateVM));
                }
                foreach (var adsImage in adsCreateVM.Announcement.AnsAllImages)
                {
                    if (adsImage == null)
                    {
                        ViewBag.Cities     = _context.Cities;
                        ViewBag.Marks      = _context.Marks;
                        ViewBag.Categories = _context.Categories;
                        ViewBag.Models     = _context.Models;
                        ModelState.AddModelError("", "Zəhmət olmasa elanın şəkillərin daxil edin");
                        return(View(adsCreateVM));
                    }
                }
            }



            Announcement creatingAds = new Announcement()
            {
                Name              = adsCreateVM.Announcement.Name,
                Price             = adsCreateVM.Announcement.Price,
                AddDate           = DateTime.Now,
                Info              = adsCreateVM.Announcement.Info,
                ModelId           = adsCreateVM.Announcement.ModelId,
                CityId            = adsCreateVM.Announcement.CityId,
                ApplicationUserId = user.Id,
            };

            if (adsCreateVM.Announcement.Photo.Length >= 2 * 1024 * 1024)
            {
                ViewBag.Cities     = _context.Cities;
                ViewBag.Marks      = _context.Marks;
                ViewBag.Categories = _context.Categories;
                ViewBag.Models     = _context.Models;
                ModelState.AddModelError("", "Sekilin Olcusu 2 meqabaytdan az olmalidir");
                return(View(adsCreateVM));
            }
            if (adsCreateVM.Announcement.Photo.ContentType.Contains("image/"))
            {
                string folderPath = Path.Combine(_env.WebRootPath, "img");
                string fileName   = Guid.NewGuid().ToString() + "_" + adsCreateVM.Announcement.Photo.FileName;
                string filePath   = Path.Combine(folderPath, fileName);
                creatingAds.PhotoUrl = fileName;

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

            await _context.Announcements.AddAsync(creatingAds);

            foreach (var adsImage in adsCreateVM.Announcement.AnsAllImages)
            {
                if (adsImage.Length >= 2 * 1024 * 1024)
                {
                    ViewBag.Cities     = _context.Cities;
                    ViewBag.Marks      = _context.Marks;
                    ViewBag.Categories = _context.Categories;
                    ViewBag.Models     = _context.Models;
                    ModelState.AddModelError("", "Sekilin Olcusu 2 meqabaytdan az olmalidir");
                    return(View(adsCreateVM));
                }
                if (adsImage.ContentType.Contains("image/"))
                {
                    string folderPathAll = Path.Combine(_env.WebRootPath, "img");
                    string fileNameAll   = Guid.NewGuid().ToString() + "_" + adsImage.FileName;
                    string filePathALL   = Path.Combine(folderPathAll, fileNameAll);

                    using (FileStream fileStreama = new FileStream(filePathALL, FileMode.Create))
                    {
                        await adsImage.CopyToAsync(fileStreama);
                    }
                    AnsImages img = new AnsImages
                    {
                        AnnouncementId = creatingAds.Id,
                        AdsPhotoUrl    = fileNameAll,
                    };
                    _context.AnsImages.Add(img);
                }
            }
            await _context.SaveChangesAsync();

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