public async void DeleteSingleImage(int?ImageId)
        {
            var deleting = _context.CarImages.Find(ImageId);

            _context.CarImages.Remove(deleting);
            await _context.SaveChangesAsync();
        }
        public async Task <IActionResult> CreatePost(News news)
        {
            if (!ModelState.IsValid)
            {
                return(View(news));
            }

            if (news.Photo == null)
            {
                ModelState.AddModelError("Photo", "Please upload photo");
                return(View(news));
            }

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


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

                News news1 = new News()
                {
                    PhotoUrl = fileName,
                    Title    = news.Title,
                    Info     = news.Info
                };

                _context.News.Add(news1);
                await _context.SaveChangesAsync();
            }

            return(RedirectToAction(nameof(IndexNews)));
        }
示例#3
0
        public async Task <IActionResult> Advertisement(Automobile automobile)
        {
            ApplicationUser user = await _userManager.FindByNameAsync(User.Identity.Name);

            automobile.ApplicationUserId = user.Id;

            ViewBag.Cities     = _context.Cities;
            ViewBag.Marks      = _context.Marks;
            ViewBag.Fuels      = _context.Fuels;
            ViewBag.Bans       = _context.Bans;
            ViewBag.Colors     = _context.Colors;
            ViewBag.SpeedBoxes = _context.SpeedBoxes;

            if (!ModelState.IsValid)
            {
                ViewBag.Cities = _context.Cities;
                ViewBag.Marks  = _context.Marks;
                ViewBag.Fuels  = _context.Fuels;
                ViewBag.Bans   = _context.Bans;
                ViewBag.Colors = _context.Colors;
                ModelState.AddModelError("", "Please input valid properties");
                return(View(automobile));
            }

            if (automobile.Photo == null)
            {
                ViewBag.Cities = _context.Cities;
                ViewBag.Marks  = _context.Marks;
                ViewBag.Fuels  = _context.Fuels;
                ViewBag.Bans   = _context.Bans;
                ViewBag.Colors = _context.Colors;
                ModelState.AddModelError("Image", "Please input Image");
                return(View(automobile));
            }

            foreach (var item in automobile.AllCarImages)
            {
                if (item == null)
                {
                    ViewBag.Cities = _context.Cities;
                    ViewBag.Marks  = _context.Marks;
                    ViewBag.Fuels  = _context.Fuels;
                    ViewBag.Bans   = _context.Bans;
                    ViewBag.Colors = _context.Colors;
                    ModelState.AddModelError("Image", "Please input Image");
                    return(View(automobile));
                }
            }

            Automobile ad = new Automobile()
            {
                Name              = automobile.Name,
                Price             = automobile.Price,
                GraduationYear    = automobile.GraduationYear,
                EnginePower       = automobile.EnginePower,
                EngineVolume      = automobile.EngineVolume,
                ColorId           = automobile.ColorId,
                FuelId            = automobile.FuelId,
                ModelId           = automobile.ModelId,
                BanId             = automobile.BanId,
                SpeedBoxId        = automobile.SpeedBoxId,
                ApplicationUserId = automobile.ApplicationUserId,
                IsNew             = automobile.IsNew,
                Mileage           = automobile.Mileage,
                CityId            = automobile.CityId
            };

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

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

            await _context.Automobiles.AddAsync(ad);

            foreach (var p in automobile.AllCarImages)
            {
                if (p.ContentType.Contains("image/"))
                {
                    string folderPathAll = Path.Combine(_env.WebRootPath, "img");
                    string fileNameAll   = Guid.NewGuid().ToString() + "_" + p.FileName;
                    string filePathALL   = Path.Combine(folderPathAll, fileNameAll);

                    using (FileStream fileStreama = new FileStream(filePathALL, FileMode.Create))
                    {
                        await p.CopyToAsync(fileStreama);
                    }
                    CarImages img = new CarImages()
                    {
                        AutomobileId = ad.Id,
                        CarPhotoUrl  = fileNameAll
                    };
                    _context.CarImages.Add(img);
                }
            }

            await _context.SaveChangesAsync();

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