Ejemplo n.º 1
0
        public IActionResult Post([FromBody] AddCarInputModel model)
        {
            //SE CADASTRO FUNCIONAR, RETORNA CREATED 201
            //SE OS DADOS DE ENTRADA ESTIVEREM INCORRETOS, RETORNA BAD REQUEST 400
            //SE O CADASTRO FUNCIONAR, MAS NAO TIVER UMA API DE CONSULTA, RETORNA 204 NO-CONTENT
            if (model.Model.Length > 50)
            {
                return(BadRequest("Modelo não pode ter mais de 50 caracteres"));
            }

            var addCar = new Car(
                model.VinCode,
                model.Brand,
                model.Model,
                model.Ano,
                model.Price,
                model.Color,
                model.ProductionDate
                );

            _dbContext.Cars.Add(addCar);
            _dbContext.SaveChanges();

            return(CreatedAtAction(nameof(GetById), new { id = addCar.Id }, model));
        }
Ejemplo n.º 2
0
        public IActionResult Post([FromBody] AddCarInputModel model)
        {
            //Se o cadastro funcionar, retorna created (201)
            //Se os dados de entrada estiverem incorretos, retrorna BAD REQUEST (400)
            //Se o cadastro funcionar, mas nao tiver uma api de consular, retorna No Content 204
            if (model.Model.Length > 50)
            {
                return(BadRequest("Modelo não pode ter mais de 50 caracteres."));
            }

            var car = new Car(

                model.VinCode,
                model.Brand,
                model.Model,
                model.Year,
                model.Price,
                model.Color,
                model.ProductionModel);

            _dbcontext.Cars.Add(car);

            _dbcontext.SaveChanges();

            return(CreatedAtAction(
                       nameof(GetById),
                       new { id = car.Id },
                       model
                       ));
        }
Ejemplo n.º 3
0
        public async Task <int> CreateCar(string userId, AddCarInputModel input)
        {
            var newCar = new Car()
            {
                UserId     = userId,
                MakeId     = input.MakeId,
                Model      = input.Model,
                TopSpeed   = input.TopSpeed,
                Weight     = input.Weight,
                Horsepower = input.Horsepower,
                Year       = input.Year,
                Torque     = input.Torque,
            };

            if (input.MainImage != null)
            {
                var carMainImageUrl = await CloudinaryExtension.UploadFileAsync(this.cloudinary, input.MainImage);

                newCar.MainImageUrl = carMainImageUrl;
            }

            await this.carRepository.AddAsync(newCar);

            await this.carRepository.SaveChangesAsync();

            return(newCar.Id);
        }
        public HttpResponse Add(AddCarInputModel input)
        {
            var userId = this.GetUserId();

            if (userId == null)
            {
                return(this.Redirect("/Users/Login"));
            }

            if (string.IsNullOrWhiteSpace(input.Model) || input.Model.Length < 5 || input.Model.Length > 20)
            {
                return(this.Error("Invalid Model"));
            }

            if (string.IsNullOrWhiteSpace(input.Image))
            {
                return(this.Error("Invalid Image"));
            }

            if (!Regex.IsMatch(input.PlateNumber, @"^[A-Z]{2}[0-9]{4}[A-Z]{2}$"))
            {
                return(this.Error("Invalid Plate Number"));
            }

            if (this.usersService.IsUserMechanic(userId))
            {
                return(this.All());
            }

            this.carsService.Add(input.Model, input.Year, input.Image, input.PlateNumber, userId);

            return(this.All());
        }
Ejemplo n.º 5
0
        public async Task CreateAsync(AddCarInputModel input, string userId, string imagePath)
        {
            var car = new Car()
            {
                Title           = input.Title,
                ConditionId     = input.ConditionId,
                MakeId          = input.MakeId,
                ModelId         = input.ModelId,
                CoupeId         = input.CoupeId,
                Milage          = input.Milage,
                ManufactureDate = input.ManufactureDate,
                ColorId         = input.ColorId,
                Price           = input.Price,
                RegionId        = input.RegionId,
                TownId          = input.TownId,
                TechDataUrl     = input.TechDataUrl,
                FuelId          = input.FuelId,
                GearBoxId       = input.GearBoxId,
                Description     = input.Description,
                AddedByUserId   = userId,
                HorsePower      = input.HorsePower,
            };

            Directory.CreateDirectory($"{imagePath}/cars/");

            foreach (var img in input.Images)
            {
                var extension = Path.GetExtension(img.FileName).TrimStart('.');

                var dbImage = new Image()
                {
                    AddedByUserId = userId,
                    Extension     = extension,
                };

                car.Images.Add(dbImage);

                var path = $"{imagePath}/cars/{dbImage.Id}.{extension}";
                using Stream fileStream = new FileStream(path, FileMode.Create);
                await img.CopyToAsync(fileStream);
            }

            foreach (var add in input.Additions)
            {
                var addition = this.additionsRepository.AllAsNoTracking().FirstOrDefault(a => a.Id == add.Id);

                if (add.IsCheked)
                {
                    car.Additions.Add(new CarAddition()
                    {
                        AdditionId = addition.Id,
                    });
                }
            }

            await this.carsRepository.AddAsync(car);

            await this.carsRepository.SaveChangesAsync();
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> AddCar()
        {
            var input = new AddCarInputModel();

            await this.carsService.AddAllSelectListValuesForCarInputModel(input);

            return(this.View(input));
        }
Ejemplo n.º 7
0
        public Car RegisterCar(AddCarInputModel model)
        {
            Car newCar = new Car(model.VinCode, model.Brand, model.Model, model.Year, model.Price, model.Color, model.ProductionDate);

            _dbContext.Cars.Add(newCar);
            _dbContext.SaveChanges();

            return(newCar);
        }
Ejemplo n.º 8
0
        public IActionResult Create()
        {
            var makes = this.makesService.GetAll();
            var createCarViewModel = new AddCarInputModel()
            {
                Makes = new SelectList(makes, "Id", "Name"),
            };

            return(this.View(createCarViewModel));
        }
Ejemplo n.º 9
0
        public IActionResult Post([FromBody] AddCarInputModel model)
        {
            var registeredCar = _carServices.RegisterCar(model);

            return(CreatedAtAction(
                       nameof(GetById),
                       new { id = registeredCar.Id },
                       model
                       ));
        }
Ejemplo n.º 10
0
        public IActionResult Post([FromBody] AddCarInputModel model)
        {
            var car = new Car(4, model.VinCode, model.Brand, model.Model, model.Year, model.Price, model.Color, model.ProductionDate);

            _dbContext.Cars.Add(car);

            return(CreatedAtAction(
                       nameof(GetById),
                       new { id = car.Id },
                       model
                       ));
        }
Ejemplo n.º 11
0
        public IActionResult Post([FromBody] AddCarInputModel model)
        {
            if (model.Model.Length > 50)
            {
                return(BadRequest("Modelo não pode ter mais de 50 caracteres"));
            }
            var car = new Car(model.VinCode, model.Brand, model.Model, model.Year, model.Price, model.Color, model.ProductionDate);

            _dbContext.Cars.Add(car);
            _dbContext.SaveChanges();
            return(CreatedAtAction(nameof(GetById), new { id = car.Id }, model));
        }
Ejemplo n.º 12
0
        public void Create(string userId, AddCarInputModel model)
        {
            var car = new Car
            {
                Model       = model.Model,
                Year        = model.Year,
                PictureUrl  = model.Image,
                PlateNumber = model.PlateNumber
            };

            this.db.Cars.Add(car);
            this.db.SaveChanges();
        }
Ejemplo n.º 13
0
        public async Task <IActionResult> Create(AddCarInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            var userId = this.userManager.GetUserId(this.User);

            await this.carsService.CreateCar(userId, input);

            return(this.RedirectToAction("Cars", "Profiles"));
        }
Ejemplo n.º 14
0
        public void Add(AddCarInputModel model, string userId)
        {
            var car = new Car()
            {
                Model       = model.Model,
                Year        = model.Year,
                PictureUrl  = model.Image,
                PlateNumber = model.PlateNumber,
                OwnerId     = userId
            };

            this.db.Cars.Add(car);
            this.db.SaveChanges();
        }
Ejemplo n.º 15
0
        public async Task <AddCarInputModel> GetCarInputModelWithFilledListItems()
        {
            var carInputModel = new AddCarInputModel
            {
                ManufactureDate   = DateTime.UtcNow,
                CategoriesItems   = await this.categoriesService.GetAllAsKeyValuePairsAsync(),
                MakesItems        = await this.makesService.GetAllAsKeyValuePairsAsync(),
                FuelTypeItems     = await this.fuelTypesService.GetAllAsKeyValuePairsAsync(),
                EuroStandartItems = await this.euroStandartsService.GetAllAsKeyValuePairsAsync(),
                GearboxesItems    = await this.gearboxesService.GetAllAsKeyValuePairsAsync(),
                ColorstItems      = await this.colorsService.GetAllAsKeyValuePairsAsync(),
            };

            return(carInputModel);
        }
Ejemplo n.º 16
0
        public void AddCar(AddCarInputModel input, string userId)
        {
            var car = new Car
            {
                Model       = input.Model,
                Year        = input.Year,
                PictureUrl  = input.Image,
                PlateNumber = input.PlateNumber,
                OwnerId     = userId,
            };

            this.db.Cars.Add(car);

            this.db.SaveChanges();
        }
Ejemplo n.º 17
0
        public async Task <IActionResult> Add(AddCarInputModel inputModel)
        {
            if (!ModelState.IsValid)
            {
                return(this.View(inputModel));
            }

            var car = this.mapper.Map <Car>(inputModel);

            car.Image = await this.imagesService.UploadImage(this.cloudinary, inputModel.ImageFile, inputModel.Model);

            await this.carsService.AddCar(car);

            return(RedirectToAction("All", "Cars"));
        }
Ejemplo n.º 18
0
        public async Task <IActionResult> AddCar(AddCarInputModel model)
        {
            if (!this.ModelState.IsValid)
            {
                await this.carsService.AddAllSelectListValuesForCarInputModel(model);

                return(this.View(model));
            }

            var userId    = this.User.FindFirst(ClaimTypes.NameIdentifier).Value;
            var imagePath = this.environment.WebRootPath;

            await this.carsService.CreateAsync(model, userId, imagePath);

            return(this.RedirectToAction("ThankYou"));
        }
Ejemplo n.º 19
0
        public ActionResult Post([FromBody] AddCarInputModel carInputModel)
        {
            var newCar = new Car(
                carInputModel.VinCode,
                carInputModel.Brand,
                carInputModel.Model,
                carInputModel.Year,
                carInputModel.Price,
                carInputModel.Color,
                carInputModel.ProductionDate);

            Context.Cars.Add(newCar);
            Context.SaveChanges();

            return(CreatedAtAction(nameof(GetById), new { id = newCar.Id }, newCar));
        }
Ejemplo n.º 20
0
        public async Task AddAllSelectListValuesForCarInputModel(AddCarInputModel input)
        {
            input.MakesItems = await this.makeService.GetAllMakes();

            input.Colors = await this.colorService.GetAllColors();

            input.CoupeTypes = await this.coupesService.GetAllCoupes();

            input.Conditions = await this.conditionsService.GetAllConditionsAsync();

            input.GearBoxes = await this.gearBoxesService.GetAllGearBoxesAsync();

            input.Regions = await this.regionsServices.GetAllRegionsAsync();

            input.Fuels = await this.fuelsServices.GetAllFuelTypesAsync();

            input.Additions = this.additionsService.GetAllAditions();
        }
Ejemplo n.º 21
0
        public IActionResult Post([FromBody] AddCarInputModel model)
        {
            if (model.Model.Length > 50)
            {
                return(BadRequest("Modelo não pode ter mais do que 50 caracteres"));
            }

            var carro = new Carro(model.VinCode, model.Marca, model.Model, model.Ano, model.Preco, model.ColorCar, model.AnoProducao);

            _dbContext.Carros.Add(carro);
            _dbContext.SaveChanges();

            return(CreatedAtAction(
                       nameof(GetbyId),
                       new { id = carro.Id },
                       model
                       ));
        }
Ejemplo n.º 22
0
        public HttpResponse Add(AddCarInputModel input)
        {
            if (!this.IsUserSignedIn())
            {
                return(this.Error(notLoggedInErrMsg));
            }

            var userId = this.GetUserId();

            if (usersService.IsUserMechanic(userId))
            {
                return(this.Error(notCLientErrMsg));
            }

            this.carsService.Add(input, userId);;

            return(this.Redirect(pathCarsAll));
        }
Ejemplo n.º 23
0
        public IActionResult Post([FromBody] AddCarInputModel model)
        {
            // IF the register works out, return CREATED(201)
            // IF Data in is incorrect, Return BAD REQUEST (400)
            // IF Register works out, but there is a Consult API, Return NOCONTENT
            if (model.Model.Length > 50)
            {
                return(BadRequest("Model can not have more than 50 characters"));
            }
            var car = new Car(model.VinCode, model.Brand, model.Model, model.Year, model.Price, model.Color, model.ProductionData);

            _dbContext.Cars.Add(car);
            _dbContext.SaveChanges();
            return(CreatedAtAction(
                       nameof(GetById),
                       new { id = car.Id },
                       model
                       ));
        }
Ejemplo n.º 24
0
        public IActionResult Post([FromBody] AddCarInputModel model)
        {
            if (model.Model.Length > 50)
            {
                return(BadRequest("Limite de caracteres ultrapassado"));
            }

            // Se o cadastro funcionar, created 201, se dados incorretos, badrequest (400)
            var car = new Car(model.VinCode, model.Brand, model.Model, model.Year, model.Price, model.Color, model.ProductionDate);

            _dbContext.Cars.Add(car);
            _dbContext.SaveChanges();

            return(CreatedAtAction(
                       nameof(GetById),
                       new { id = car.Id },
                       model
                       ));
        }
Ejemplo n.º 25
0
        public HttpResponse Add(AddCarInputModel input)
        {
            if (!this.IsUserSignedIn())
            {
                return(this.Redirect("/Users/Login"));
            }

            if (this.usersService.IsUserMechanic(this.GetUserId()))
            {
                return(this.Redirect("/"));
            }

            if (string.IsNullOrWhiteSpace(input.Model) || input.Model.Length < 4 || input.Model.Length > 20)
            {
                return(this.Error("Model is required and should be between 5 and 20 characters!"));
            }

            if (string.IsNullOrWhiteSpace(input.Image))
            {
                return(this.Error("Image is required!"));
            }

            if (!Uri.TryCreate(input.Image, UriKind.Absolute, out _))
            {
                return(this.Error("Invalid image URL!"));
            }

            if (string.IsNullOrWhiteSpace(input.PlateNumber))
            {
                return(this.Error("Plate Number is required!"));
            }

            if (!Regex.IsMatch(input.PlateNumber, @"^[A-Z]{2}[0-9]{4}[A-Z]{2}$"))
            {
                return(this.Error("Invalid Plate Number!"));
            }

            this.carsService.AddCar(input, this.GetUserId());

            return(this.Redirect("/Cars/All"));
        }
Ejemplo n.º 26
0
        public IActionResult Post([FromBody] AddCarInputModel model)
        {
            //SE O CADASTRO FUNCIONAR, RETORNA CREATED (201)
            //SE OS DADOS DE ENTRADA ESTIVER INCORRETOS, RETORNA BAD REQUEST (400)
            //SE O CADASTRO FUNCIOINAR, MAIS NÃO TIVER UMA API DE CONSULTA, RETORNA NOCONTENT(204)
            if (model.Model.Length > 50)
            {
                return(BadRequest("Modelo nãop pode ter mais de 50 caracteres."));
            }

            var car = new Car(model.VinCode, model.Brand, model.Model, model.Year, model.Price, model.Color, model.ProductionDate);

            _dbContext.Cars.Add(car);
            _dbContext.SaveChanges();

            return(CreatedAtAction(
                       nameof(GetById),
                       new { id = car.Id },
                       model
                       ));
        }
Ejemplo n.º 27
0
        public Car CreateCar(AddCarInputModel input)
        {
            var carToAdd = new Car
            {
                ModelId         = input.ModelId,
                MakeId          = input.MakeId,
                CategoryId      = input.CategoryId,
                FuelTypeId      = input.FuelTypeId,
                EngineSize      = input.EngineSize,
                HorsePower      = input.HorsePower,
                EuroStandartId  = input.EuroStandartId,
                GearboxId       = input.GearboxId,
                ColorId         = input.ColorId,
                Seats           = input.Seats,
                Doors           = input.Doors,
                State           = input.State,
                Mileage         = input.Mileage,
                ManufactureDate = input.ManufactureDate,
            };

            return(carToAdd);
        }
Ejemplo n.º 28
0
        public HttpResponse Add(AddCarInputModel input)
        {
            if (!this.IsUserSignedIn())
            {
                return(this.Redirect("/Users/Login"));
            }

            var userId = this.GetUserId();

            if (this.usersService.IsUserMechanic(userId))
            {
                return(this.Error("Cannot add a car! You are not a client."));
            }

            if (string.IsNullOrEmpty(input.Model) || input.Model.Length < 5 || input.Model.Length > 20)
            {
                return(this.Error("Model is required and should be between 5 and 20 characters long."));
            }

            if (input.Year < 1980)
            {
                return(this.Error("Car year too old."));
            }

            if (string.IsNullOrEmpty(input.Image) || !Uri.TryCreate(input.Image, UriKind.Absolute, out _))
            {
                return(this.Error("Image url should be valid."));
            }

            if (string.IsNullOrEmpty(input.PlateNumber) || !Regex.IsMatch(input.PlateNumber, @"^[A-Z]{2}\s[0-9]{4}\s[A-Z]{2}$"))
            {
                return(this.Error(
                           "Plate Number is required and should contain 2 Capital English letters, followed by 4 digits, followed by 2 Capital English letters."));
            }

            this.carsService.AddCar(input, userId);
            return(this.Redirect("/Cars/All"));
        }
Ejemplo n.º 29
0
        public async Task CreateCarShouldAddCarToDb()
        {
            var userId     = this.DbContext.Users.Select(u => u.Id).First();
            var makeId     = this.DbContext.Makes.Select(m => m.Id).First();
            var inputModel = new AddCarInputModel()
            {
                Horsepower = 100,
                TopSpeed   = 100,
                MakeId     = makeId,
                Model      = "M5",
                Torque     = 100,
                Weight     = 100,
                Year       = 2010,
            };

            await this.Service.CreateCar(userId, inputModel);

            var expectedCarsCount = 2;

            var actualCarsCount = this.DbContext.Cars.Count();

            Assert.Equal(expectedCarsCount, actualCarsCount);
        }
Ejemplo n.º 30
0
        public HttpResponse Add(AddCarInputModel model)
        {
            //var userId = this.GetUserId();
            //if (this.IsUserSignedIn() ||
            //    !this.usersService.IsUserMechanic(userId))
            //{

            //}
            var userId = GetUserId();

            if (!this.IsUserSignedIn())
            {
                return(this.Redirect("/Users/Login"));
            }

            if (this.usersService.IsUserMechanic(userId))
            {
                return(this.Redirect("/"));
            }

            if (string.IsNullOrEmpty(model.Model) ||
                model.Model.Length < 5 ||
                model.Model.Length > 20)
            {
                return(this.Error("Model should be between 5 and 20 characters long."));
            }

            if (model.Year < 1900 || model.Year > 2100)
            {
                return(this.Error("The year should be between 1900 and 2100."));
            }

            if (string.IsNullOrEmpty(model.Image))
            {
                return(this.Error("Image needs to be uploaded."));
            }

            if (!Uri.TryCreate(model.Image, UriKind.Absolute, out _))
            {
                return(this.Error("Image url should be valid."));
            }

            //!Regex.IsMatch(model.PlateNumber, @"[A-Z]{2}[0-9]{4}[A-Z]{2}")
            if (string.IsNullOrEmpty(model.PlateNumber))
            {
                return(this.Error($"Car plate number {model.PlateNumber} is not valid. It should be in format 'AA0000AA'."));
            }

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

            //var userId = GetUserId();
            //this.carsService.Create(userId, model);
            this.db.Cars.Add(car);
            this.db.SaveChanges();
            return(this.Redirect("/"));
        }