public void PowerSupplieController_WithValidModel_ShouldEditPowerSupplyAndAddToDb() { var dbPowerSupply = new PowerSupply() { Connector = "24 pin", Effectiveness = 90, Manufacturer = "Connectors", ModelName = "Cnt5S", NumberOfCoolingVents = 8, Pfc = "PFC", Power = 800, Price = 300 }; dbContext.PowerSupplies.Add(dbPowerSupply); dbContext.SaveChanges(); var powerSupplyModel = new PowerSupplyBindingModel() { Connector = "12 pin", Effectiveness = 80, Manufacturer = "ConnectorsInc", ModelName = "Cnt5SP+", NumberOfCoolingVents = 4, Pfc = "PFCMAX", Power = 600, Price = 500 }; var powerSupply = dbContext.PowerSupplies.First(); controller.Edit(powerSupply.Id, powerSupplyModel); var dbEditedPowerSupply = dbContext.PowerSupplies.First(); Assert.IsNotNull(dbEditedPowerSupply); Assert.AreEqual(dbEditedPowerSupply.Connector, powerSupplyModel.Connector); Assert.AreEqual(dbEditedPowerSupply.Effectiveness, powerSupplyModel.Effectiveness); Assert.AreEqual(dbEditedPowerSupply.Manufacturer, powerSupplyModel.Manufacturer); Assert.AreEqual(dbEditedPowerSupply.ModelName, powerSupplyModel.ModelName); Assert.AreEqual(dbEditedPowerSupply.NumberOfCoolingVents, powerSupplyModel.NumberOfCoolingVents); Assert.AreEqual(dbEditedPowerSupply.Pfc, powerSupplyModel.Pfc); Assert.AreEqual(dbEditedPowerSupply.Power, powerSupplyModel.Power); Assert.AreEqual(dbEditedPowerSupply.Price, powerSupplyModel.Price); }
public IActionResult Edit(int id, PowerSupplyBindingModel model) { if (!ModelState.IsValid) { return(View(model)); } var powerSupply = context.PowerSupplies.FirstOrDefault(p => p.Id == id); if (powerSupply == null) { return(NotFound()); } MapProperties(powerSupply, model); context.SaveChanges(); return(RedirectToAction("Details", "PowerSupplies", new { area = "", id })); }
public IActionResult Create(PowerSupplyBindingModel model) { if (!ModelState.IsValid) { TempData["Error"] = "Invalid form."; return(RedirectToAction("Create")); } if (context.PowerSupplies.Any(p => p.ModelName == model.ModelName)) { TempData["Error"] = "Model name already exists."; return(RedirectToAction("Create")); } var powerSupply = mapper.Map <PowerSupply>(model); context.PowerSupplies.Add(powerSupply); context.SaveChanges(); return(RedirectToAction("Details", "PowerSupplies", new { area = "", id = powerSupply.Id })); }