public IActionResult Create([Bind] TruckViewModel truck) { if (ModelState.IsValid) { var truckModel = _service.FindModelById((int)truck.ModelId); var newTruck = new Truck() { ModelYear = truck.ModelYear, Description = truck.Description, ManufacturingYear = truck.ManufacturingYear, Model = truckModel }; _service.SaveOrUpdateTruck(newTruck); } return(RedirectToAction("ListTrucks")); }
public void TruckServiceFunctions() { ITruckService _service = serviceProvider.GetService <ITruckService>(); List <TruckModel> models = _service.ListAvailableModels(); Assert.NotNull(models); Assert.AreEqual(2, models.Count); // Get first available model var model = models[0]; // Saving new Truck with specific information Truck truck = new Truck() { Description = "TEST", ManufacturingYear = 1944, ModelYear = 1944, Model = model, TruckModelId = model.TruckModelId }; List <Truck> receivedTrucks = _service.ListAllTrucks(); List <Truck> filtered = receivedTrucks.Where(t => t.ManufacturingYear == 1944 || t.ModelYear == 1944).ToList(); Assert.AreEqual(0, filtered.Count); _service.SaveOrUpdateTruck(truck); receivedTrucks = _service.ListAllTrucks(); filtered = receivedTrucks.Where(t => t.ManufacturingYear == 1944 || t.ModelYear == 1944).ToList(); Assert.AreEqual(1, filtered.Count); Truck saved = filtered[0]; Assert.AreEqual(1944, saved.ManufacturingYear); Assert.AreEqual(1944, saved.ModelYear); Assert.AreEqual("TEST", saved.Description); int id = saved.TruckId; Truck toUpdate = _service.FindTruckById(id); Assert.NotNull(toUpdate); toUpdate.ModelYear = 1950; _service.SaveOrUpdateTruck(toUpdate); receivedTrucks = _service.ListAllTrucks(); filtered = receivedTrucks.Where(t => t.ManufacturingYear == 1944 && t.ModelYear == 1950).ToList(); Assert.AreEqual(1, filtered.Count); Truck updated = filtered[0]; Assert.AreEqual(1944, updated.ManufacturingYear); Assert.AreEqual(1950, updated.ModelYear); Assert.AreEqual("TEST", updated.Description); _service.DeleteTruck(id); receivedTrucks = _service.ListAllTrucks(); filtered = receivedTrucks.Where(t => t.ManufacturingYear == 1944 || t.ModelYear == 1950).ToList(); Assert.AreEqual(0, filtered.Count); }