public async Task <IActionResult> Create(Blog blog)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(Redirect("/account/login"));
            }
            if (!User.IsInRole("admin"))
            {
                return(Redirect("/account/login"));
            }
            if (blog.Content == null)
            {
                ModelState.AddModelError("Content", "Plese input a valid content");
                return(View(blog));
            }
            if (blog.ShortContent == null)
            {
                ModelState.AddModelError("Short Content", "Plese input a valid short content");
                return(View(blog));
            }
            if (blog.Title == null)
            {
                ModelState.AddModelError("Title", "Plese input a valid title");
                return(View(blog));
            }
            if (blog.Photo == null)
            {
                ModelState.AddModelError("Photo", "Photo should be selected");
                return(View());
            }
            if (!blog.Photo.ContentType.Contains("image/"))
            {
                ModelState.AddModelError("Photo", "File type should be image");
                return(View());
            }

            string filename = await blog.Photo.SavePhotoAsync(_env.WebRootPath, "blogs");

            blog.Image       = filename;
            blog.CreateDate  = DateTime.Now;
            blog.UpdatedDate = DateTime.Now;

            await _context.Blogs.AddAsync(blog);

            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> ConfirmEmail(string id)
        {
            ApplicationUser confirmedUser = await _userManager.FindByIdAsync(id);

            confirmedUser.EmailConfirmed = true;
            await _context.SaveChangesAsync();

            return(View(confirmedUser));
        }
        public async Task <IActionResult> Create(Car car)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(Redirect("/account/login"));
            }
            if (!User.IsInRole("member"))
            {
                return(Redirect("/account/login"));
            }
            if (car.MakeId == null)
            {
                ModelState.AddModelError("MakeId", "Select a valid make");
                ViewBag.Countries     = _context.Countries;
                ViewBag.Brands        = _context.Brands;
                ViewBag.Transmissions = _context.Transmissions;
                ViewBag.Fuels         = _context.FuelTypes;
                return(View(car));
            }
            if (car.ManufactureYear < 1700 || car.ManufactureYear > DateTime.Now.Year)
            {
                ModelState.AddModelError("ManufactureYear", "Input a valid manufacture year");
                ViewBag.Countries     = _context.Countries;
                ViewBag.Brands        = _context.Brands;
                ViewBag.Transmissions = _context.Transmissions;
                ViewBag.Fuels         = _context.FuelTypes;
                return(View(car));
            }
            if (car.Motor <= 0)
            {
                ModelState.AddModelError("Motor", "Input a valid Motor");
                ViewBag.Countries     = _context.Countries;
                ViewBag.Brands        = _context.Brands;
                ViewBag.Transmissions = _context.Transmissions;
                ViewBag.Fuels         = _context.FuelTypes;
                return(View(car));
            }
            if (car.Millage < 0)
            {
                ModelState.AddModelError("Millage", "Input a valid Millage");
                ViewBag.Countries     = _context.Countries;
                ViewBag.Brands        = _context.Brands;
                ViewBag.Transmissions = _context.Transmissions;
                ViewBag.Fuels         = _context.FuelTypes;
                return(View(car));
            }
            if (int.TryParse(car.Color, out _))
            {
                ModelState.AddModelError("Color", "Input a valid Color");
                ViewBag.Countries     = _context.Countries;
                ViewBag.Brands        = _context.Brands;
                ViewBag.Transmissions = _context.Transmissions;
                ViewBag.Fuels         = _context.FuelTypes;
                return(View(car));
            }
            if (car.TransmissionId == null)
            {
                ModelState.AddModelError("TransmissionId", "Select a valid Transmission");
                ViewBag.Countries     = _context.Countries;
                ViewBag.Brands        = _context.Brands;
                ViewBag.Transmissions = _context.Transmissions;
                ViewBag.Fuels         = _context.FuelTypes;
                return(View(car));
            }
            if (car.FuelTypeId == null)
            {
                ModelState.AddModelError("FuelTypeId", "Select a valid Fuel type");
                ViewBag.Countries     = _context.Countries;
                ViewBag.Brands        = _context.Brands;
                ViewBag.Transmissions = _context.Transmissions;
                ViewBag.Fuels         = _context.FuelTypes;
                return(View(car));
            }
            if (car.Description.Length < 200)
            {
                ModelState.AddModelError("Description", "Description is too short");
                ViewBag.Countries     = _context.Countries;
                ViewBag.Brands        = _context.Brands;
                ViewBag.Transmissions = _context.Transmissions;
                ViewBag.Fuels         = _context.FuelTypes;
                return(View(car));
            }
            if (car.Price < 0)
            {
                ModelState.AddModelError("Price", "Input a valid Price");
                ViewBag.Countries     = _context.Countries;
                ViewBag.Brands        = _context.Brands;
                ViewBag.Transmissions = _context.Transmissions;
                ViewBag.Fuels         = _context.FuelTypes;
                return(View(car));
            }
            if (car.Advertisement.CityId == null)
            {
                ModelState.AddModelError("Advertisement.CityId", "Select a valid City");
                ViewBag.Countries     = _context.Countries;
                ViewBag.Brands        = _context.Brands;
                ViewBag.Transmissions = _context.Transmissions;
                ViewBag.Fuels         = _context.FuelTypes;
                return(View(car));
            }
            if (car.Photo == null)
            {
                ModelState.AddModelError("Photo", "Select a Photo");
                ViewBag.Countries     = _context.Countries;
                ViewBag.Brands        = _context.Brands;
                ViewBag.Transmissions = _context.Transmissions;
                ViewBag.Fuels         = _context.FuelTypes;
                return(View(car));
            }
            if (!car.Photo.ContentType.Contains("image/"))
            {
                ModelState.AddModelError("Photo", "File type should be image");
                ViewBag.Countries     = _context.Countries;
                ViewBag.Brands        = _context.Brands;
                ViewBag.Transmissions = _context.Transmissions;
                ViewBag.Fuels         = _context.FuelTypes;
                return(View(car));
            }

            if (car.DetailPhotos == null)
            {
                ModelState.AddModelError("DetailPhotos", "Select car detail photos");
                ViewBag.Countries     = _context.Countries;
                ViewBag.Brands        = _context.Brands;
                ViewBag.Transmissions = _context.Transmissions;
                ViewBag.Fuels         = _context.FuelTypes;
                return(View(car));
            }
            await _context.Cars.AddAsync(car);

            foreach (var photo in car.DetailPhotos)
            {
                if (!photo.ContentType.Contains("image/"))
                {
                    ModelState.AddModelError("DetailPhotos", "File type should be image");
                    ViewBag.Countries     = _context.Countries;
                    ViewBag.Brands        = _context.Brands;
                    ViewBag.Transmissions = _context.Transmissions;
                    ViewBag.Fuels         = _context.FuelTypes;
                    return(View(car));
                }

                //await _context.SaveChangesAsync();
            }
            foreach (var photo in car.DetailPhotos)
            {
                CarDetailPhotos detailPhoto = new CarDetailPhotos
                {
                    CarId = car.Id,
                    Image = await photo.SavePhotoAsync(_env.WebRootPath, "advertisements")
                };
                await _context.CarDetailPhotos.AddAsync(detailPhoto);
            }
            string filename = await car.Photo.SavePhotoAsync(_env.WebRootPath, "advertisements");

            car.Image = filename;
            car.Advertisement.CurrentDate       = DateTime.Now;
            car.Advertisement.UpdatedDate       = DateTime.Now;
            car.Advertisement.ApplicationUserId = _context.Users.Where(u => u.UserName == User.Identity.Name).FirstOrDefault().Id;


            await _context.Advertisements.AddAsync(car.Advertisement);

            await _context.SaveChangesAsync();

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