Beispiel #1
0
        public CarAdd CreateCarAdd(CarAddInputModel model, string carId, string creatorId)
        {
            if (string.IsNullOrEmpty(carId))
            {
                throw new ArgumentException();
            }

            List <CarPicture> carPictures = new List <CarPicture>();

            foreach (var picture in model.Pictures)
            {
                if (!picture.FileName.EndsWith(PictureSuffix))
                {
                    throw new InvalidOperationException();
                }

                var pictureDirectory = Directory.GetCurrentDirectory() + Slash + Constants.RootDirectory + Slash + ImagesFolderName + Slash + CarImagesFolerName;

                var directory = Directory.CreateDirectory(pictureDirectory).ToString();

                var path = Path.Combine(directory, picture.FileName);

                using (var stream = new FileStream(path, FileMode.Create))
                {
                    picture.CopyTo(stream);
                }

                var carPicture = new CarPicture
                {
                    Url = Slash + ImagesFolderName + Slash + CarImagesFolerName + Slash + picture.FileName
                };

                carPictures.Add(carPicture);
            }

            var carAdd = new CarAdd
            {
                Title          = model.Title,
                CarId          = carId,
                AdditionalInfo = model.AdditionalInfo,
                CreatorId      = creatorId,
                Pictures       = carPictures
            };

            this.db.CarAdds.Add(carAdd);
            this.db.SaveChanges();

            return(carAdd);
        }
        private CarAddInputModel GetCarAddInputModel()
        {
            var imageMock = new Mock <IFormFile>();

            var content = "Fake file";

            var fileName = "text.jpg";

            var memoryStream = new MemoryStream();

            var writer = new StreamWriter(memoryStream);

            writer.Write(content);
            writer.Flush();

            memoryStream.Position = 0;

            imageMock.Setup(_ => _.OpenReadStream()).Returns(memoryStream);
            imageMock.Setup(_ => _.FileName).Returns(fileName);
            imageMock.Setup(_ => _.Length).Returns(memoryStream.Length);

            var fakeImage = imageMock.Object;

            var inputModel = new CarAddInputModel
            {
                CarMake             = "Opel",
                CarModel            = "Vectra",
                CarCategory         = "Saloon",
                CarColor            = "White",
                CarEngineType       = "Gasoline / LPG",
                CarExtras           = "CD Player, Electric windows, electric mirrors, AC",
                CarHorsePower       = 130,
                CarLocation         = "Nqkude",
                CarPrice            = 5000m,
                CarTransmission     = "Manual",
                CarYearOfProduction = 2006,
                Title          = "Opel Vectra; Good Quality",
                AdditionalInfo = "The car is ina great shape! You can drive it anywhere without a problem!",
                Pictures       = new List <IFormFile>()
                {
                    fakeImage
                }
            };

            return(inputModel);
        }
        public IActionResult Create(CarAddInputModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(model));
            }

            try
            {
                var engineType = this.carAddsService.GetCarEngineType(model.CarEngineType);

                var transmission = this.carAddsService.GetCarTransmissionType(model.CarTransmission);

                var carCategory = this.carAddsService.GetCarCategory(model.CarCategory);

                var extras = this.carAddsService.CreateCarExtras(model.CarExtras);

                var carId = this.carAddsService.CreateCar(model, engineType, transmission, carCategory, extras).Id;

                var userId = this.userService.GetUserByUsername(this.User.Identity.Name).Id;

                var carAddId = this.carAddsService.CreateCarAdd(model, carId, userId).Id;

                var viewModel = this.carAddsService.GetViewAddModel(carAddId);

                return(this.View(Constants.ViewAddView, viewModel));
            }
            catch (ArgumentException)
            {
                var errorModel = this.carAddsService.GetErrorViewModel(Constants.CarAddInputErrorMessage);

                return(this.View(Constants.ErrorView, errorModel));
            }
            catch (InvalidOperationException)
            {
                var errorModel = this.carAddsService.GetErrorViewModel(Constants.PictureNotValidMessage);

                return(this.View(Constants.ErrorView, errorModel));
            }
        }
Beispiel #4
0
        public Car CreateCar(CarAddInputModel model, EngineType engineType, Transmission transmission, CarCategory carCategory, ICollection <CarExtra> extras)
        {
            var car = new Car
            {
                Color            = model.CarColor,
                EngineType       = engineType,
                HorsePower       = model.CarHorsePower,
                Location         = model.CarLocation,
                Make             = model.CarMake,
                Model            = model.CarModel,
                YearOfProduction = model.CarYearOfProduction,
                Price            = model.CarPrice,
                Transmission     = transmission,
                Category         = carCategory,
                Extras           = extras
            };

            this.db.Cars.Add(car);
            this.db.SaveChanges();

            return(car);
        }