public async Task EditShouldEdit()
        {
            var options           = new DbContextOptionsBuilder <ApplicationDbContext>().UseInMemoryDatabase("Edit").Options;
            var dbContext         = new ApplicationDbContext(options);
            var imageService      = new ImageService(dbContext);
            var motorcycleService = new MotorcycleService(dbContext, imageService);

            AutoMapperConfig.RegisterMappings(typeof(EditMotorcycleViewModel).Assembly, typeof(Motorcycle).Assembly);

            var addModel = new AddMotorcycleViewModel()
            {
                Type         = MotorcycleType.Cross,
                Made         = "Honda",
                Transmission = TransmissionType.Automatic,
                PriceForHour = 2.00M,
                Description  = "Test",
            };

            var addedScooter = await motorcycleService.AddVehicle <AddMotorcycleViewModel>(addModel);

            var editModel = new EditMotorcycleViewModel()
            {
                Type         = MotorcycleType.Cross,
                Made         = "Edited",
                Transmission = TransmissionType.Automatic,
                PriceForHour = 2.00M,
                Description  = "Test",
            };

            await motorcycleService.DoEdit <EditMotorcycleViewModel>(editModel);

            var editedModel = motorcycleService.EditDetails <EditMotorcycleViewModel>(addedScooter);

            Assert.Equal("Edited", editModel.Made);
        }
        public async Task AddImage()
        {
            var options           = new DbContextOptionsBuilder <ApplicationDbContext>().UseInMemoryDatabase("add_image").Options;
            var dbContext         = new ApplicationDbContext(options);
            var imageService      = new ImageService(dbContext);
            var motorcycleService = new MotorcycleService(dbContext, imageService);

            AutoMapperConfig.RegisterMappings(typeof(MotorcycleViewModel).Assembly, typeof(Motorcycle).Assembly);
            AutoMapperConfig.RegisterMappings(typeof(EditMotorcycleViewModel).Assembly, typeof(Motorcycle).Assembly);

            var scooter1 = new AddMotorcycleViewModel()
            {
                Type         = MotorcycleType.Cross,
                Made         = "Honda",
                Transmission = TransmissionType.Automatic,
                PriceForHour = 2.00M,
                Description  = "Test",
            };

            var imagePath = "Test";
            var scooter   = await motorcycleService.AddVehicle <AddMotorcycleViewModel>(scooter1);

            await motorcycleService.AddImageUrls(scooter, imagePath);

            var result = motorcycleService.EditDetails <MotorcycleViewModel>(scooter);

            Assert.Equal(imagePath, result.ImageUrl);
        }
        public async Task Delete()
        {
            var options           = new DbContextOptionsBuilder <ApplicationDbContext>().UseInMemoryDatabase("delete_bicycle").Options;
            var dbContext         = new ApplicationDbContext(options);
            var imageService      = new ImageService(dbContext);
            var motorcycleService = new MotorcycleService(dbContext, imageService);

            AutoMapperConfig.RegisterMappings(typeof(AddMotorcycleViewModel).Assembly, typeof(Motorcycle).Assembly);

            var scooter1 = new AddMotorcycleViewModel()
            {
                Type         = MotorcycleType.Cross,
                Made         = "Honda",
                Transmission = TransmissionType.Automatic,
                PriceForHour = 2.00M,
                Description  = "Test",
            };

            var scooter = await motorcycleService.AddVehicle <AddMotorcycleViewModel>(scooter1);

            await motorcycleService.Delete(scooter);

            var bycicles = motorcycleService.GetAll <MotorcycleViewModel>(false);

            Assert.Empty(bycicles.ToList());
        }
        public IActionResult AddMotorcycle(AddMotorcycleViewModel offer)
        {
            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);

            if (!ModelState.IsValid)
            {
                return(this.View(new AddMotorcycleViewModel(dbContext)));
            }

            this.offerService.AddMotorcycle(offer, userId);
            return(this.Redirect("/Motorcycles/AllMotorcycles"));
        }
        public async Task <IActionResult> Add(AddMotorcycleViewModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View());
            }

            var motorcycleId = await this.motorcycleService.AddVehicle <AddMotorcycleViewModel>(input);

            if (input.Image != null)
            {
                await this.imageService.UploadImage(input.Image, string.Format(GlobalConstants.IMAGEPATH, IMAGE, motorcycleId));

                await this.motorcycleService.AddImageUrls(motorcycleId, string.Format(GlobalConstants.IMAGEPATH, IMAGE, motorcycleId));
            }

            return(this.Redirect(string.Format(ALLPATH, input.Type)));
        }
        public async Task AddTests()
        {
            var options           = new DbContextOptionsBuilder <ApplicationDbContext>().UseInMemoryDatabase("Add").Options;
            var dbContext         = new ApplicationDbContext(options);
            var imageService      = new ImageService(dbContext);
            var motorcycleService = new MotorcycleService(dbContext, imageService);

            var model = new AddMotorcycleViewModel()
            {
                Type         = MotorcycleType.Cross,
                Made         = "Honda",
                Transmission = TransmissionType.Automatic,
                PriceForHour = 2.00M,
                Description  = "Test",
            };

            var motorcycle = await motorcycleService.AddVehicle <AddMotorcycleViewModel>(model);

            Assert.Equal(1, motorcycle);
        }
        public async Task GetAllByType()
        {
            var options           = new DbContextOptionsBuilder <ApplicationDbContext>().UseInMemoryDatabase("get_all_by_type").Options;
            var dbContext         = new ApplicationDbContext(options);
            var imageService      = new ImageService(dbContext);
            var motorcycleService = new MotorcycleService(dbContext, imageService);

            AutoMapperConfig.RegisterMappings(typeof(EditMotorcycleViewModel).Assembly, typeof(Motorcycle).Assembly);

            var scooter1 = new AddMotorcycleViewModel()
            {
                Type         = MotorcycleType.Cross,
                Made         = "Honda",
                Transmission = TransmissionType.Automatic,
                PriceForHour = 2.00M,
                Description  = "Test",
            };
            var addedMotorcycle = await motorcycleService.AddVehicle <AddMotorcycleViewModel>(scooter1);

            var scooter = motorcycleService.GetAllByType <MotorcycleViewModel>(MotorcycleType.Cross.ToString(), false);

            Assert.Single(scooter.ToList());
        }
        public void AddMotorcycle(AddMotorcycleViewModel addMotorcycleViewModel, string userId)
        {
            var offer = new MotorcycleOffer()
            {
                Title            = addMotorcycleViewModel.Title,
                Make             = addMotorcycleViewModel.Make,
                Model            = addMotorcycleViewModel.Model,
                Year             = addMotorcycleViewModel.Year,
                Price            = addMotorcycleViewModel.Price,
                Color            = addMotorcycleViewModel.Color,
                FuelType         = addMotorcycleViewModel.FuelType,
                Gearbox          = addMotorcycleViewModel.Gearbox,
                Condition        = addMotorcycleViewModel.Condition,
                HorsePower       = addMotorcycleViewModel.HorsePower,
                CubicCentimeters = addMotorcycleViewModel.CubicCentimeters,
                Kilometers       = addMotorcycleViewModel.Kilometers,
                Description      = addMotorcycleViewModel.Description,
                ContactNumber    = addMotorcycleViewModel.ContactNumber,
                CreatedOn        = DateTime.UtcNow,
            };

            using (var target = new MemoryStream())
            {
                addMotorcycleViewModel.MainImageFile.CopyTo(target);
                offer.OfferImage = target.ToArray();
            }

            var user = dbContext.AspNetUsers.FirstOrDefault(x => x.Id == userId);

            offer.User   = user;
            offer.UserId = userId;

            dbContext.MotorcycleOffers.Add(offer);
            dbContext.SaveChanges();

            var offerType = "MotorcycleOffer";

            if (addMotorcycleViewModel.SecondImageFile != null)
            {
                using (var target = new MemoryStream())
                {
                    addMotorcycleViewModel.SecondImageFile.CopyTo(target);
                    var offerImage = GetOfferImage(target, offer.Id, offerType);
                    dbContext.OfferImages.Add(offerImage);
                }
            }

            if (addMotorcycleViewModel.ThirdImageFile != null)
            {
                using (var target = new MemoryStream())
                {
                    addMotorcycleViewModel.ThirdImageFile.CopyTo(target);
                    var offerImage = GetOfferImage(target, offer.Id, offerType);
                    dbContext.OfferImages.Add(offerImage);
                }
            }

            if (addMotorcycleViewModel.FourthImageFile != null)
            {
                using (var target = new MemoryStream())
                {
                    addMotorcycleViewModel.FourthImageFile.CopyTo(target);
                    var offerImage = GetOfferImage(target, offer.Id, offerType);
                    dbContext.OfferImages.Add(offerImage);
                }
            }

            if (addMotorcycleViewModel.FifthImageFile != null)
            {
                using (var target = new MemoryStream())
                {
                    addMotorcycleViewModel.FifthImageFile.CopyTo(target);
                    var offerImage = GetOfferImage(target, offer.Id, offerType);
                    dbContext.OfferImages.Add(offerImage);
                }
            }

            if (addMotorcycleViewModel.SixthImageFile != null)
            {
                using (var target = new MemoryStream())
                {
                    addMotorcycleViewModel.SixthImageFile.CopyTo(target);
                    var offerImage = GetOfferImage(target, offer.Id, offerType);
                    dbContext.OfferImages.Add(offerImage);
                }
            }

            dbContext.SaveChanges();
        }