Ejemplo n.º 1
0
        public async Task <IActionResult> Create(Blog blog)
        {
            if (User.Identity.IsAuthenticated && User.IsInRole("moderator"))
            {
                if (blog == null)
                {
                    return(PartialView("ErrorPage"));
                }
                if (!ModelState.IsValid)
                {
                    return(NotFound());
                }
                if (blog.Content.Length < 300)
                {
                    ModelState.AddModelError("Content", "Content is too short");
                    return(View(blog));
                }
                Blog newBlog = new Blog();
                await _context.AddAsync(newBlog);

                if (blog.MainPhoto == null)
                {
                    ModelState.AddModelError("MainPhoto", "Main image should be selected");
                    return(View(blog));
                }
                if (!blog.MainPhoto.ContentType.Contains("image/"))
                {
                    ModelState.AddModelError("MainPhoto", "File type should be image");
                    return(View(blog));
                }
                newBlog.MainImageUrl = await blog.MainPhoto.SavePhotoAsync(_env.WebRootPath, "blogs");

                newBlog.Header      = blog.Header;
                newBlog.Content     = blog.Content;
                newBlog.PublishDate = DateTime.Now;
                newBlog.UpdatedDate = DateTime.Now;
                if (blog.DetailPhotos != null)
                {
                    foreach (var photo in blog.DetailPhotos)
                    {
                        if (!photo.ContentType.Contains("image/"))
                        {
                            ModelState.AddModelError("DetailPhotos", "File type should be image");
                            return(View(blog));
                        }
                    }
                    foreach (var photo in blog.DetailPhotos)
                    {
                        BlogDetailImage detailImage = new BlogDetailImage
                        {
                            BlogId   = newBlog.Id,
                            ImageUrl = await photo.SavePhotoAsync(_env.WebRootPath, "blogs")
                        };
                        await _context.BlogDetailImages.AddAsync(detailImage);
                    }
                }
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewBag.IsHeaderNonVisible = true;
            return(Redirect("/account/login"));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Create(Advertisement advertisement)
        {
            if (User.Identity.IsAuthenticated && User.IsInRole("member"))
            {
                if (advertisement == null)
                {
                    return(PartialView("ErrorPage"));
                }
                if (!ModelState.IsValid)
                {
                    ViewBag.Brands       = _context.Brands.OrderBy(b => b.Name);
                    ViewBag.Countries    = _context.Countries.OrderBy(c => c.Name);
                    ViewBag.Categories   = _context.CarClasses;
                    ViewBag.BodyTypes    = _context.BodyTypes;
                    ViewBag.Transmissons = _context.Transmissions;
                    ViewBag.FuelTypes    = _context.FuelTypes;
                    ViewBag.Colors       = _context.Colors;
                    ViewBag.WheelDrives  = _context.WheelDrives;
                    return(NotFound());
                }
                if (!_context.Models.Any(m => m.Id == advertisement.Car.ModelId))
                {
                    ModelState.AddModelError("Car.ModelId", "Model is not accessible");
                    ViewBag.Brands       = _context.Brands.OrderBy(b => b.Name);
                    ViewBag.Countries    = _context.Countries.OrderBy(c => c.Name);
                    ViewBag.Categories   = _context.CarClasses;
                    ViewBag.BodyTypes    = _context.BodyTypes;
                    ViewBag.Transmissons = _context.Transmissions;
                    ViewBag.FuelTypes    = _context.FuelTypes;
                    ViewBag.Colors       = _context.Colors;
                    ViewBag.WheelDrives  = _context.WheelDrives;
                    return(View(advertisement));
                }
                if (!_context.Cities.Any(c => c.Id == advertisement.Car.CityId))
                {
                    ModelState.AddModelError("Car.CityId", "City is not accessible");
                    ViewBag.Brands       = _context.Brands.OrderBy(b => b.Name);
                    ViewBag.Countries    = _context.Countries.OrderBy(c => c.Name);
                    ViewBag.Categories   = _context.CarClasses;
                    ViewBag.BodyTypes    = _context.BodyTypes;
                    ViewBag.Transmissons = _context.Transmissions;
                    ViewBag.FuelTypes    = _context.FuelTypes;
                    ViewBag.Colors       = _context.Colors;
                    ViewBag.WheelDrives  = _context.WheelDrives;
                    return(View(advertisement));
                }
                if (!_context.BodyTypes.Any(b => b.Id == advertisement.Car.BodyTypeId))
                {
                    ModelState.AddModelError("Car.BodyTypeId", "Body type is not accessible");
                    ViewBag.Brands       = _context.Brands.OrderBy(b => b.Name);
                    ViewBag.Countries    = _context.Countries.OrderBy(c => c.Name);
                    ViewBag.Categories   = _context.CarClasses;
                    ViewBag.BodyTypes    = _context.BodyTypes;
                    ViewBag.Transmissons = _context.Transmissions;
                    ViewBag.FuelTypes    = _context.FuelTypes;
                    ViewBag.Colors       = _context.Colors;
                    ViewBag.WheelDrives  = _context.WheelDrives;
                    return(View(advertisement));
                }
                if (!_context.Transmissions.Any(t => t.Id == advertisement.Car.TransmissionId))
                {
                    ModelState.AddModelError("Car.TransmissionId", "Transmission is not accessible");
                    ViewBag.Brands       = _context.Brands.OrderBy(b => b.Name);
                    ViewBag.Countries    = _context.Countries.OrderBy(c => c.Name);
                    ViewBag.Categories   = _context.CarClasses;
                    ViewBag.BodyTypes    = _context.BodyTypes;
                    ViewBag.Transmissons = _context.Transmissions;
                    ViewBag.FuelTypes    = _context.FuelTypes;
                    ViewBag.Colors       = _context.Colors;
                    ViewBag.WheelDrives  = _context.WheelDrives;
                    return(View(advertisement));
                }
                if (!_context.FuelTypes.Any(f => f.Id == advertisement.Car.FuelTypeId))
                {
                    ModelState.AddModelError("Car.FuelTypeId", "Fuel type is not accessible");
                    ViewBag.Brands       = _context.Brands.OrderBy(b => b.Name);
                    ViewBag.Countries    = _context.Countries.OrderBy(c => c.Name);
                    ViewBag.Categories   = _context.CarClasses;
                    ViewBag.BodyTypes    = _context.BodyTypes;
                    ViewBag.Transmissons = _context.Transmissions;
                    ViewBag.FuelTypes    = _context.FuelTypes;
                    ViewBag.Colors       = _context.Colors;
                    ViewBag.WheelDrives  = _context.WheelDrives;
                    return(View(advertisement));
                }
                if (!_context.Colors.Any(c => c.Id == advertisement.Car.ColorId))
                {
                    ModelState.AddModelError("Car.ColorId", "Color is not accessible");
                    ViewBag.Brands       = _context.Brands.OrderBy(b => b.Name);
                    ViewBag.Countries    = _context.Countries.OrderBy(c => c.Name);
                    ViewBag.Categories   = _context.CarClasses;
                    ViewBag.BodyTypes    = _context.BodyTypes;
                    ViewBag.Transmissons = _context.Transmissions;
                    ViewBag.FuelTypes    = _context.FuelTypes;
                    ViewBag.Colors       = _context.Colors;
                    ViewBag.WheelDrives  = _context.WheelDrives;
                    return(View(advertisement));
                }
                if (!_context.WheelDrives.Any(w => w.Id == advertisement.Car.WheelDriveId))
                {
                    ModelState.AddModelError("Car.WheelDriveId", "Wheel drive class is not accessible");
                    ViewBag.Brands       = _context.Brands.OrderBy(b => b.Name);
                    ViewBag.Countries    = _context.Countries.OrderBy(c => c.Name);
                    ViewBag.Categories   = _context.CarClasses;
                    ViewBag.BodyTypes    = _context.BodyTypes;
                    ViewBag.Transmissons = _context.Transmissions;
                    ViewBag.FuelTypes    = _context.FuelTypes;
                    ViewBag.Colors       = _context.Colors;
                    ViewBag.WheelDrives  = _context.WheelDrives;
                    return(View(advertisement));
                }
                if (advertisement.Car.ManufactureYear < 1900 || advertisement.Car.ManufactureYear > DateTime.Now.Year)
                {
                    ModelState.AddModelError("Car.ManufactureYear", $"Manufacture year must be between 1900 and {DateTime.Now.Year}");
                    ViewBag.Brands       = _context.Brands.OrderBy(b => b.Name);
                    ViewBag.Countries    = _context.Countries.OrderBy(c => c.Name);
                    ViewBag.Categories   = _context.CarClasses;
                    ViewBag.BodyTypes    = _context.BodyTypes;
                    ViewBag.Transmissons = _context.Transmissions;
                    ViewBag.FuelTypes    = _context.FuelTypes;
                    ViewBag.Colors       = _context.Colors;
                    ViewBag.WheelDrives  = _context.WheelDrives;
                    return(View(advertisement));
                }
                Car newCar = new Car
                {
                    BodyTypeId      = advertisement.Car.BodyTypeId,
                    CityId          = advertisement.Car.CityId,
                    ColorId         = advertisement.Car.ColorId,
                    Description     = advertisement.Car.Description,
                    Engine          = advertisement.Car.Engine,
                    FuelTypeId      = advertisement.Car.FuelTypeId,
                    IsVip           = advertisement.Car.IsVip,
                    ManufactureYear = advertisement.Car.ManufactureYear,
                    Milage          = advertisement.Car.Milage,
                    ModelId         = advertisement.Car.ModelId,
                    Price           = advertisement.Car.Price,
                    TransmissionId  = advertisement.Car.TransmissionId,
                    WheelDriveId    = advertisement.Car.WheelDriveId
                };
                if (advertisement.Car.Price <= 100)
                {
                    newCar.CarClassId = 1;
                }
                if (advertisement.Car.Price > 100 && advertisement.Car.Price <= 200)
                {
                    newCar.CarClassId = 2;
                }
                if (advertisement.Car.Price > 200)
                {
                    newCar.CarClassId = 3;
                }
                await _context.AddAsync(newCar);

                if (advertisement.Car.Photo == null)
                {
                    ModelState.AddModelError("Car.Photo", "Main image should be selected");
                    ViewBag.Brands       = _context.Brands.OrderBy(b => b.Name);
                    ViewBag.Countries    = _context.Countries.OrderBy(c => c.Name);
                    ViewBag.Categories   = _context.CarClasses;
                    ViewBag.BodyTypes    = _context.BodyTypes;
                    ViewBag.Transmissons = _context.Transmissions;
                    ViewBag.FuelTypes    = _context.FuelTypes;
                    ViewBag.Colors       = _context.Colors;
                    ViewBag.WheelDrives  = _context.WheelDrives;
                    return(View(advertisement));
                }
                if (!advertisement.Car.Photo.ContentType.Contains("image/"))
                {
                    ModelState.AddModelError("Car.Photo", "File type should be image");
                    ViewBag.Brands       = _context.Brands.OrderBy(b => b.Name);
                    ViewBag.Countries    = _context.Countries.OrderBy(c => c.Name);
                    ViewBag.Categories   = _context.CarClasses;
                    ViewBag.BodyTypes    = _context.BodyTypes;
                    ViewBag.Transmissons = _context.Transmissions;
                    ViewBag.FuelTypes    = _context.FuelTypes;
                    ViewBag.Colors       = _context.Colors;
                    ViewBag.WheelDrives  = _context.WheelDrives;
                    return(View(advertisement));
                }
                newCar.MainImageUrl = await advertisement.Car.Photo.SavePhotoAsync(_env.WebRootPath, "advertisements");

                if (advertisement.Car.DetailPhotos != null)
                {
                    foreach (var photo in advertisement.Car.DetailPhotos)
                    {
                        if (!photo.ContentType.Contains("image/"))
                        {
                            ModelState.AddModelError("Car.DetailPhotos", "File type should be image");
                            ViewBag.Brands       = _context.Brands.OrderBy(b => b.Name);
                            ViewBag.Countries    = _context.Countries.OrderBy(c => c.Name);
                            ViewBag.Categories   = _context.CarClasses;
                            ViewBag.BodyTypes    = _context.BodyTypes;
                            ViewBag.Transmissons = _context.Transmissions;
                            ViewBag.FuelTypes    = _context.FuelTypes;
                            ViewBag.Colors       = _context.Colors;
                            ViewBag.WheelDrives  = _context.WheelDrives;
                            return(View(advertisement));
                        }
                    }
                    foreach (var photo in advertisement.Car.DetailPhotos)
                    {
                        CarDetailImage detailImage = new CarDetailImage
                        {
                            CarId    = newCar.Id,
                            ImageUrl = await photo.SavePhotoAsync(_env.WebRootPath, "advertisements")
                        };
                        await _context.CarDetailImages.AddAsync(detailImage);
                    }
                }
                Advertisement newAd = new Advertisement
                {
                    CarId             = newCar.Id,
                    ApplicationUserId = _userManager.GetUserId(User),
                    PublishDate       = DateTime.Now,
                    UpdatedDate       = DateTime.Now,
                    IsVerified        = false
                };
                await _context.AddAsync(newAd);

                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewBag.IsHeaderNonVisible = true;
            return(Redirect("/account/login"));
        }