/// <summary> /// Saves a CarMake to the data store. /// </summary> /// <param name="item">The item to save</param> public static void Save(CarMake item) { if (item.IsItemModified) { if (item.MakeId == null) { item.MakeId = Insert(item); } else { Update(item); } } }
public bool SaveControl() { bool valid = false; CarMake modelToSave = new CarMake(); if (EditOption) { modelToSave = CarMakeManager.Load(MakeId); } modelToSave.Name = txtMakeName.Text; modelToSave.Manufacturer = txtManufacturer.Text; string errorMessage; valid = CarMakeManager.Save(modelToSave, out errorMessage); return valid; }
/// <summary> /// Validate CarMake Entity /// </summary> /// <param name="item">Entity to validate</param> /// <param name="errorMessage">error message if validation failed</param> /// <returns>return true if entity passes validation logic, else return false</returns> public static bool Validate(CarMake item, out string errorMessage) { errorMessage = string.Empty; if (item.Name.IsNullOrWhiteSpace()) { errorMessage += "Name is required. "; } if (item.Manufacturer.IsNullOrWhiteSpace()) { errorMessage += "Manufacturer is required. "; } if (Search(new SearchCarMake { Name = item.Name }).SafeAny(c => c.MakeId != item.MakeId)) { errorMessage += "Cannot have multiple car makes with the same name. "; } errorMessage = errorMessage.TrimSafely(); return errorMessage.IsNullOrWhiteSpace(); }
/// <summary> /// Save CarMake Entity /// </summary> /// <param name="item">Entity to save</param> /// <param name="errorMessage">Error Message</param> /// <returns>return true if save successfully, else return false</returns> public static bool Save(CarMake item, out string errorMessage) { bool isValid = Validate(item, out errorMessage); if (isValid) { CarMakeDao.Save(item); } return isValid; }
public void CreateTestObject() { int carMakeId = default(int); string errorMessage; Random rand = new Random(); List<CarMake> carMakes = CarMakeManager.LoadAll().ToList(); if (carMakes.SafeAny()) { carMakeId = carMakes.First().MakeId.GetValueOrDefault(); } else { CarMake carMake = new CarMake { Name = string.Format("TestCarMake_{0}", rand.Next(1, 1000)), Manufacturer = "TestManufacturer" }; CarMakeManager.Save(carMake, out errorMessage); if (carMake.MakeId.HasValue) { carMakeId = carMake.MakeId.Value; } } CarModelTestObject = new CarModel { ModelId = null, MakeId = carMakeId, Name = string.Format("TestCarModel_{0}", rand.Next(1, 1000)) }; CarModelManager.Save(CarModelTestObject, out errorMessage); }
public void CarMakeValidateTest() { Assert.IsNotNull(CarMakeTestObject, "Test object was null"); string errorMessage; bool valid = CarMakeManager.Validate(CarMakeTestObject, out errorMessage); Assert.IsTrue(valid, "CarMake was not valid when it should have been"); Assert.IsTrue(errorMessage.IsNullOrWhiteSpace(), "Errors found when there shouldn't have been any"); string name = CarMakeTestObject.Name; CarMakeTestObject.Name = string.Empty; valid = CarMakeManager.Validate(CarMakeTestObject, out errorMessage); Assert.IsFalse(valid, "CarMake was valid when it shouldn't have been"); Assert.IsTrue(errorMessage.HasValue(), "Errors not found when there should have been any"); Assert.IsTrue(errorMessage.Contains("Name is required", StringComparison.InvariantCultureIgnoreCase)); CarMake newCarMake = new CarMake { Name = name, Manufacturer = name }; valid = CarMakeManager.Validate(newCarMake, out errorMessage); Assert.IsFalse(valid, "CarMake was valid when it shouldn't have been"); Assert.IsTrue(errorMessage.HasValue(), "Errors not found when there should have been any"); Assert.IsTrue(errorMessage.Contains("Cannot have multiple car makes with the same name", StringComparison.InvariantCultureIgnoreCase)); }
public void CreateTestObject() { Random rand = new Random(); CarMakeTestObject = new CarMake { MakeId = null, Name = string.Format("TestCarMake_{0}", rand.Next(1, 1000)), Manufacturer = "TestCarManufacturer" }; string errorMessage; CarMakeManager.Save(CarMakeTestObject, out errorMessage); }
public void CreateTestObject() { int inventoryId; Guid userId = Guid.Empty; Random rand = new Random(); string errorMessage; string userName = string.Format("NewUser_{0}", rand.Next(1, 100000)); const string passWord = "******"; string email = string.Format("emailaddress{0}@test.com", rand.Next(1, 1000000000)); MembershipCreateStatus status; MembershipUser user = Membership.CreateUser(userName, passWord, email, "quieres apple?", "no, apple es para mujeres", true, out status); if (user != null && user.ProviderUserKey is Guid) { userId = (Guid)user.ProviderUserKey; } List<Inventory> inventoryList = InventoryManager.LoadAll().ToList(); if (inventoryList.SafeAny()) { inventoryId = inventoryList.First().InventoryId.GetValueOrDefault(); } else { CarMake newMake = new CarMake { Manufacturer = "TestManufacturer", Name = string.Format("Make_{0}", rand.Next(1, 1000)) }; CarMakeManager.Save(newMake, out errorMessage); CarModel newModel = new CarModel { MakeId = newMake.MakeId.GetValueOrDefault(), Name = string.Format("Model_{0}", rand.Next(1, 1000)) }; CarModelManager.Save(newModel, out errorMessage); Location location = new Location { Name = string.Format("Location_{0}", rand.Next(1, 100000)), Address = string.Format("{0} Street St", rand.Next(10, 1000)), City = "Fake City", State = "FS", Zip = "11111", Email = "*****@*****.**", Phone = "1112223333" }; LocationManager.Save(location, out errorMessage); Inventory inventory = new Inventory { ModelId = newModel.ModelId.GetValueOrDefault(), LocationId = location.LocationId.GetValueOrDefault(), Color = "Red", Price = (decimal) ((rand.NextDouble() + 0.1)*rand.Next(10, 60)), Quantity = rand.Next(1, 10), Year = rand.Next(1990, 2015) }; inventoryId = inventory.InventoryId.GetValueOrDefault(); } TransactionTestObject = new Transaction { BillingAddress = "123 Fake Street", BillingCity = "Fake City", BillingState = "FS", BillingZip = "12345", CCV = 123, CreditCardNumber = "4444999911110000", ExpirationDate = DateTime.Today.AddYears(rand.Next(5, 10)), InventoryId = inventoryId, Price = (decimal)( ( rand.NextDouble() + 0.1 ) * rand.Next(10, 60) ), RentalDateEnd = DateTime.Today.AddDays(rand.Next(11, 50)), RentalDateStart = DateTime.Today.AddDays(rand.Next(1, 10)), TransactionDate = DateTime.Today, UserId = userId }; TransactionManager.Save(TransactionTestObject, out errorMessage); }
/// <summary> /// Updates a CarMake /// </summary> /// <param name="item">The CarMake item to save</param> private static void Update(CarMake item) { List<SqlParameter> parameters = new List<SqlParameter> { new SqlParameter("@MakeId", item.MakeId), new SqlParameter("@Name", item.Name), new SqlParameter("@Manufacturer", item.Manufacturer), new SqlParameter("@Deleted", item.Deleted) }; DataManager.ExecuteProcedure(KarzPlusConnectionString, "PKP_UpdateCarMake", parameters); }
/// <summary> /// Inserts a new CarMake /// </summary> /// <param name="item">The CarMake item to insert</param> /// <returns>The id of the CarMake item just inserted</returns> private static int Insert(CarMake item) { List<SqlParameter> parameters = new List<SqlParameter> { new SqlParameter("@Name", item.Name), new SqlParameter("@Manufacturer", item.Manufacturer), new SqlParameter("@Deleted", item.Deleted) }; return Convert.ToInt32(DataManager.ExecuteScalarProcedure(KarzPlusConnectionString, "PKP_InsertCarMake", parameters)); }