public void CheckIfAddMotorcycleAddsTheMotorcycle() { var dbContext = new AutomobileDbContext(); var service = new OfferService(dbContext); IFormFile file = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")), 0, 0, "Data", "dummy.txt"); service.AddMotorcycle(new AddMotorcycleViewModel() { Title = "Test", Make = "BMW", Model = "HB4", Year = 2003, Price = 220, Condition = (Condition)Enum.Parse(typeof(Condition), "New"), Color = "black", FuelType = (FuelType)Enum.Parse(typeof(FuelType), "Diesel"), HorsePower = 50, CubicCentimeters = 650, Gearbox = (Gearbox)Enum.Parse(typeof(Gearbox), "Automatic"), Kilometers = 15000, Description = "asasrsa", ContactNumber = "03210320232", MainImageFile = file }, "e4bf2992-cc46-4ebb-baf2-667f245a3582"); var motorcycleOffersContainMotorcycle = dbContext.MotorcycleOffers.Any(x => x.Title == "Test"); Assert.True(motorcycleOffersContainMotorcycle); var mockMotorcycle = dbContext.MotorcycleOffers.FirstOrDefault(x => x.Title == "Test"); dbContext.MotorcycleOffers.Remove(mockMotorcycle); dbContext.SaveChanges(); }
public void ChangeMotorcycleDetails(VisualizeMotorcycleDetailsViewModel model) { var motorcycleOffer = dbContext.MotorcycleOffers.FirstOrDefault(x => x.Id == model.Id); motorcycleOffer.Condition = (Condition)Enum.Parse(typeof(Condition), model.Type); motorcycleOffer.Make = model.Make; motorcycleOffer.Model = model.Model; motorcycleOffer.Kilometers = model.Kilometers; motorcycleOffer.FuelType = (FuelType)Enum.Parse(typeof(FuelType), model.FuelType); motorcycleOffer.HorsePower = model.HorsePower; motorcycleOffer.CubicCentimeters = model.CubicCentimeters; motorcycleOffer.Gearbox = (Gearbox)Enum.Parse(typeof(Gearbox), model.Gearbox); motorcycleOffer.Price = model.Price; motorcycleOffer.Color = model.Color; motorcycleOffer.Year = model.Year; motorcycleOffer.Description = model.Description; dbContext.SaveChanges(); }
public void ChangeCarDetails(VisualizeCarDetailsViewModel model) { var carOffer = dbContext.CarOffers.FirstOrDefault(x => x.Id == model.Id); carOffer.Condition = (Condition)Enum.Parse(typeof(Condition), model.Type); carOffer.Make = model.Make; carOffer.Model = model.Model; carOffer.Kilometers = model.Kilometers; carOffer.FuelType = (FuelType)Enum.Parse(typeof(FuelType), model.FuelType); carOffer.HorsePower = model.HorsePower; carOffer.EngineSize = model.EngineSize; carOffer.Gearbox = (Gearbox)Enum.Parse(typeof(Gearbox), model.Gearbox); carOffer.Price = model.Price; carOffer.Doors = model.Doors; carOffer.Color = model.Color; carOffer.Year = model.Year; carOffer.Description = model.Description; dbContext.SaveChanges(); }
public void ChangeScooterDetails(VisualizeScooterDetailsViewModel model) { var scooterOffer = dbContext.ElectricScooterOffers.FirstOrDefault(x => x.Id == model.Id); scooterOffer.Condition = (Condition)Enum.Parse(typeof(Condition), model.Condition); scooterOffer.Make = model.Make; scooterOffer.Model = model.Model; scooterOffer.Kilometers = model.Kilometers; scooterOffer.MotorPower = model.MotorPower; scooterOffer.TravellingDistance = model.TravellingDistance; scooterOffer.MaxSpeedAchievable = model.MaxSpeedAchievable; scooterOffer.TiresSize = model.TiresSize; scooterOffer.WaterproofLevel = model.WaterproofLevel; scooterOffer.MaxWeight = model.MaxWeight; scooterOffer.Price = model.Price; scooterOffer.ScooterSize = model.ScooterSize; scooterOffer.Year = model.Year; scooterOffer.Description = model.Description; dbContext.SaveChanges(); }
public void CheckIfDeleteMotorcycleOfferRemovesTheOfferFromTheDatabase() { var dbContext = new AutomobileDbContext(); var service = new MotorcyclesService(dbContext); var dbOffer = dbContext.MotorcycleOffers.FirstOrDefault(); var offerImages = dbContext.OfferImages.Where(x => x.MotorcycleOfferId == dbOffer.Id); var getMotorcycleOfferById = service.GetMotorcycleOfferById(dbOffer.Id); service.DeleteMotorcycleOffer(getMotorcycleOfferById); Assert.True(dbContext.MotorcycleOffers.Any(x => x.Id == dbOffer.Id) == false); //Add removed motorcycle and images so that db remains the same. Set Offer Id = 0 to not override identity for Id in database. getMotorcycleOfferById.Id = 0; dbContext.MotorcycleOffers.Add(getMotorcycleOfferById); dbContext.OfferImages.AddRange(offerImages); dbContext.SaveChanges(); }
public void CheckIfAddElectricScooterAddsTheScooter() { var dbContext = new AutomobileDbContext(); var service = new OfferService(dbContext); IFormFile file = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")), 0, 0, "Data", "dummy.txt"); service.AddElectricScooter(new AddElectricScooterViewModel() { Title = "Test", Make = "Xiaomi", Model = "365", Year = 2003, Price = 220, Condition = (Condition)Enum.Parse(typeof(Condition), "New"), WaterproofLevel = "test", MaxSpeedAchievable = 25, MotorPower = 250, ScooterSize = "2413", TiresSize = 8.5m, TravellingDistance = 205, Battery = "test", Kilometers = 15000, Description = "asasrsa", ContactNumber = "03210320232", MainImageFile = file }, "e4bf2992-cc46-4ebb-baf2-667f245a3582"); var scooterOffersContainScooter = dbContext.ElectricScooterOffers.Any(x => x.Title == "Test"); Assert.True(scooterOffersContainScooter); var mockScooter = dbContext.ElectricScooterOffers.FirstOrDefault(x => x.Title == "Test"); dbContext.ElectricScooterOffers.Remove(mockScooter); dbContext.SaveChanges(); }
public void CheckIfAddCarAddsTheCar() { var dbContext = new AutomobileDbContext(); var service = new OfferService(dbContext); IFormFile file = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")), 0, 0, "Data", "dummy.txt"); service.AddCar(new AddCarViewModel() { Title = "Test", Make = "Audi", Model = "A3", Year = 2003, Price = 220, Condition = (Condition)Enum.Parse(typeof(Condition), "New"), Color = "black", SteeringWheelSide = 0, FuelType = (FuelType)Enum.Parse(typeof(FuelType), "Diesel"), HorsePower = 150, EngineSize = 1060, Gearbox = (Gearbox)Enum.Parse(typeof(Gearbox), "Automatic"), Doors = "2", Kilometers = 15000, ContactNumber = "03210320232", MainImageFile = file }, "e4bf2992-cc46-4ebb-baf2-667f245a3582"); var carOffersContainCar = dbContext.CarOffers.Any(x => x.Title == "Test"); Assert.True(carOffersContainCar); var mockCar = dbContext.CarOffers.FirstOrDefault(x => x.Title == "Test"); dbContext.CarOffers.Remove(mockCar); dbContext.SaveChanges(); }
public void AddCar(AddCarViewModel addCarViewModel, string userId) { var offer = new CarOffer() { Title = addCarViewModel.Title, Make = addCarViewModel.Make, Model = addCarViewModel.Model, Year = addCarViewModel.Year, Price = addCarViewModel.Price, Condition = addCarViewModel.Condition, Color = addCarViewModel.Color, SteeringWheelSide = addCarViewModel.SteeringWheelSide, FuelType = addCarViewModel.FuelType, HorsePower = addCarViewModel.HorsePower, EngineSize = addCarViewModel.EngineSize, Gearbox = addCarViewModel.Gearbox, Doors = addCarViewModel.Doors, Kilometers = addCarViewModel.Kilometers, Description = addCarViewModel.Description, ContactNumber = addCarViewModel.ContactNumber, CreatedOn = DateTime.UtcNow, }; using (var target = new MemoryStream()) { if (addCarViewModel.MainImageFile != null) { addCarViewModel.MainImageFile.CopyTo(target); offer.OfferImage = target.ToArray(); } } var user = dbContext.AspNetUsers.FirstOrDefault(x => x.Id == userId); offer.User = user; offer.UserId = userId; dbContext.CarOffers.Add(offer); dbContext.SaveChanges(); var offerType = "CarOffer"; if (addCarViewModel.SecondImageFile != null) { using (var target = new MemoryStream()) { addCarViewModel.SecondImageFile.CopyTo(target); var offerImage = GetOfferImage(target, offer.Id, offerType); dbContext.OfferImages.Add(offerImage); } } if (addCarViewModel.ThirdImageFile != null) { using (var target = new MemoryStream()) { addCarViewModel.ThirdImageFile.CopyTo(target); var offerImage = GetOfferImage(target, offer.Id, offerType); dbContext.OfferImages.Add(offerImage); } } if (addCarViewModel.FourthImageFile != null) { using (var target = new MemoryStream()) { addCarViewModel.FourthImageFile.CopyTo(target); var offerImage = GetOfferImage(target, offer.Id, offerType); dbContext.OfferImages.Add(offerImage); } } if (addCarViewModel.FifthImageFile != null) { using (var target = new MemoryStream()) { addCarViewModel.FifthImageFile.CopyTo(target); var offerImage = GetOfferImage(target, offer.Id, offerType); dbContext.OfferImages.Add(offerImage); } } if (addCarViewModel.SixthImageFile != null) { using (var target = new MemoryStream()) { addCarViewModel.SixthImageFile.CopyTo(target); var offerImage = GetOfferImage(target, offer.Id, offerType); dbContext.OfferImages.Add(offerImage); } } dbContext.SaveChanges(); }