Esempio n. 1
0
        public ICollection <string> ValidateCar(AddCarFormModel model)
        {
            var errors = new List <string>();

            if (model.Model.Length < CarModelMinLength || model.Model.Length > DefaultMaxLength)
            {
                errors.Add($"Model '{model.Model}' is not valid. It must be between {CarModelMinLength} and {DefaultMaxLength} characters long.");
            }

            if (model.Year < CarYearMinValue || model.Year > CarYearMaxValue)
            {
                errors.Add($"Year '{model.Year}' is not valid. It must be between {CarYearMinValue} and {CarYearMaxValue}.");
            }

            if (!Uri.IsWellFormedUriString(model.Image, UriKind.Absolute))
            {
                errors.Add($"Image {model.Image} is not a valid URL.");
            }

            if (!Regex.IsMatch(model.PlateNumber, CarPlateNumberRegularExpression))
            {
                errors.Add($"Plate number {model.PlateNumber} is not valid. It should be in format 'AA0000AA'.");
            }

            return(errors);
        }
Esempio n. 2
0
        public HttpResponse Add(AddCarFormModel model)
        {
            if (this.userService.IsMechanic(this.User.Id))
            {
                return(Unauthorized());
            }

            var modelErrors = this.validator.ValidateCar(model);

            if (modelErrors.Any())
            {
                return(Error(modelErrors));
            }

            var car = new Car
            {
                Model       = model.Model,
                Year        = model.Year,
                PictureUrl  = model.Image,
                PlateNumber = model.PlateNumber,
                OwnerId     = this.User.Id
            };

            this.data.Cars.Add(car);

            this.data.SaveChanges();

            return(Redirect("/Cars/All"));
        }
Esempio n. 3
0
        public ICollection <string> ValidateCar(AddCarFormModel car)
        {
            var errors = new List <string>();

            if (car.Model == null || car.Model.Length < CarModelMinLength || car.Model.Length > DefaultMaxLength)
            {
                errors.Add($"Model '{car.Model}' is not valid. It must be between {CarModelMinLength} and {DefaultMaxLength} characters long.");
            }

            if (car.Year < CarYearMinValue || car.Year > CarYearMaxValue)
            {
                errors.Add($"Year '{car.Year}' is not valid. It must be between {CarYearMinValue} and {CarYearMaxValue}.");
            }

            if (car.Image == null || !Uri.IsWellFormedUriString(car.Image, UriKind.Absolute))
            {
                errors.Add($"Image '{car.Image}' is not valid. It must be a valid URL.");
            }

            if (car.PlateNumber == null || !Regex.IsMatch(car.PlateNumber, CarPlateNumberRegularExpression))
            {
                errors.Add($"Plate number '{car.PlateNumber}' is not valid. It should be in 'XX0000XX' format.");
            }

            return(errors);
        }
Esempio n. 4
0
        public ICollection <string> ValidateCarCreation(AddCarFormModel model)
        {
            var errors = new List <string>();

            if (model.Model.Length < 5 || model.Model.Length > 20)
            {
                errors.Add($"Model '{model.Model}' must be between 5 and 20 characters long.");
            }

            if (model.Year < 1900 || model.Year > 2100)
            {
                errors.Add($"Year '{model.Year}' must be between 1900 and 2100.");
            }

            if (!Uri.IsWellFormedUriString(model.Image, UriKind.Absolute))
            {
                errors.Add($"Image '{model.Image}' is not a valid Url.");
            }

            if (!Regex.IsMatch(model.PlateNumber, "[A-Z]{2}[0-9]{4}[A-Z]{2}"))
            {
                errors.Add($"Plate number {model.PlateNumber} is not valid. It should be in format 'AA0000AA'");
            }

            return(errors);
        }
Esempio n. 5
0
        public IActionResult AddCar(AddCarFormModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            //html sanitizer for model.Description

            this.cars.Add(model.Make, model.Year, model.Power, model.Importer, model.Description);

            TempData["SuccessMessage"] = "Car added successfully!";

            return(RedirectToAction(nameof(All)));
        }
        public HttpResponse Add(AddCarFormModel model)
        {
            if (this.userService.IsMechanic(this.User.Id))
            {
                return(Error("Mechanic cannot add car."));
            }

            var modelErrors = this.validator.ValidateCar(model);

            if (modelErrors.Any())
            {
                return(Error(modelErrors));
            }

            this.carService.Add(model.Model, model.Year, model.Image, model.PlateNumber, this.User.Id);

            return(Redirect("/Cars/All"));
        }
 public IActionResult Add(AddCarFormModel car)
 {
     if (ModelState.IsValid)
     {
         var userId = userManager.GetUserId(this.User);
         Car addCar = new Car
         {
             Brand      = car.Brand,
             Category   = car.Category,
             Engine     = car.Engine,
             HorsePower = car.HorsePower,
             ImageUrl   = car.ImageUrl,
             Model      = car.Model,
             Type       = car.Type,
             Year       = car.Year,
             UserId     = userId
         };
         this.db.Cars.Add(addCar);
         this.db.SaveChanges();
         return(RedirectToAction("Details", new { addCar.Id }));
     }
     return(View(car));
 }
Esempio n. 8
0
        public IActionResult Add(AddCarFormModel car)
        {
            if (ModelState.IsValid)
            {
                var userId = this.userManager.GetUserId(this.User);

                var carDb = (new Car
                {
                    ImageUrl = car.ImageUrl,
                    Make = car.Make,
                    Model = car.Model,
                    Price = car.Price,
                    Year = car.Year,
                    UserId = userId
                });

                this.db.Add(carDb);

                this.db.SaveChanges();

                return(RedirectToAction(nameof(Details), new { carDb.Id }));
            }
            return(View());
        }