/// <summary> /// Constructor with specified initial values. /// </summary> /// <param name="profile">User's Profile</param> public Address(Profile profile) { if (!string.IsNullOrEmpty(profile.Username) && profile.Accounts.Count > 0) { //Just grab the first account. Account account = profile.Accounts[0]; FirstName = account.FirstName; LastName = account.LastName; Address1 = account.Address1; Address2 = account.Address2; City = account.City; State = account.State; Zip = account.Zip; Country = account.Country; Phone = account.Phone; Email = account.Email; } }
/// <summary> /// Update an existing row in the datasource. /// </summary> /// <param name="transactionManager"><see cref="TransactionManager"/> object</param> /// <param name="entity">PetShop.Business.Profile object to update.</param> /// <remarks> /// After updating the datasource, the PetShop.Business.Profile object will be updated /// to refelect any changes made by the datasource. (ie: identity or computed columns) /// </remarks> /// <returns>Returns true if operation is successful.</returns> /// <exception cref="System.Exception">The command could not be executed.</exception> /// <exception cref="System.Data.DataException">The <paramref name="transactionManager"/> is not open.</exception> /// <exception cref="System.Data.Common.DbException">The command could not be executed.</exception> public override bool Update(TransactionManager transactionManager, PetShop.Business.Profile entity) { SqlDatabase database = new SqlDatabase(this._connectionString); DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "dbo.Profiles_Update", _useStoredProcedure); database.AddInParameter(commandWrapper, "@UniqueId", DbType.Int32, entity.UniqueId); database.AddInParameter(commandWrapper, "@Username", DbType.AnsiString, entity.Username); database.AddInParameter(commandWrapper, "@ApplicationName", DbType.AnsiString, entity.ApplicationName); database.AddInParameter(commandWrapper, "@IsAnonymous", DbType.Boolean, (entity.IsAnonymous.HasValue ? (object)entity.IsAnonymous : System.DBNull.Value)); database.AddInParameter(commandWrapper, "@LastActivityDate", DbType.DateTime, (entity.LastActivityDate.HasValue ? (object)entity.LastActivityDate : System.DBNull.Value)); database.AddInParameter(commandWrapper, "@LastUpdatedDate", DbType.DateTime, (entity.LastUpdatedDate.HasValue ? (object)entity.LastUpdatedDate : System.DBNull.Value)); int results = 0; //Provider Data Requesting Command Event OnDataRequesting(new CommandEventArgs(commandWrapper, "Update", entity)); if (transactionManager != null) { results = Utility.ExecuteNonQuery(transactionManager, commandWrapper); } else { results = Utility.ExecuteNonQuery(database, commandWrapper); } //Stop Tracking Now that it has been updated and persisted. if (DataRepository.Provider.EnableEntityTracking) { EntityManager.StopTracking(entity.EntityTrackingKey); } entity.AcceptChanges(); //Provider Data Requested Command Event OnDataRequested(new CommandEventArgs(commandWrapper, "Update", entity)); return(Convert.ToBoolean(results)); }
private static void AddCartItem(ref Profile profile, string itemId, int quantity) { int index = 0; bool found = false; foreach (Cart cart in profile.WishList) { if (cart.ItemId == itemId) { found = true; break; } index++; } if (found) profile.WishList[index].Quantity += quantity; else { Item item = new ItemService().GetByItemId(itemId); Product product = new ProductService().GetByProductId(item.ProductId); Cart cart = new Cart { UniqueId = profile.UniqueId, ItemId = itemId, Name = item.Name, ProductId = item.ProductId, IsShoppingCart = false, Price = item.ListPrice ?? item.UnitCost ?? 0, Type = product.Name, CategoryId = product.CategoryId, Quantity = quantity }; profile.WishList.Add(cart); } }
/// <summary> /// Inserts a PetShop.Business.Profile object into the datasource using a transaction. /// </summary> /// <param name="transactionManager"><see cref="TransactionManager"/> object</param> /// <param name="entity">PetShop.Business.Profile object to insert.</param> /// <remarks> /// After inserting into the datasource, the PetShop.Business.Profile object will be updated /// to refelect any changes made by the datasource. (ie: identity or computed columns) /// </remarks> /// <returns>Returns true if operation is successful.</returns> /// <exception cref="System.Exception">The command could not be executed.</exception> /// <exception cref="System.Data.DataException">The <paramref name="transactionManager"/> is not open.</exception> /// <exception cref="System.Data.Common.DbException">The command could not be executed.</exception> public override bool Insert(TransactionManager transactionManager, PetShop.Business.Profile entity) { SqlDatabase database = new SqlDatabase(this._connectionString); DbCommand commandWrapper = StoredProcedureProvider.GetCommandWrapper(database, "dbo.Profiles_Insert", _useStoredProcedure); database.AddOutParameter(commandWrapper, "@UniqueId", DbType.Int32, 4); database.AddInParameter(commandWrapper, "@Username", DbType.AnsiString, entity.Username); database.AddInParameter(commandWrapper, "@ApplicationName", DbType.AnsiString, entity.ApplicationName); database.AddInParameter(commandWrapper, "@IsAnonymous", DbType.Boolean, (entity.IsAnonymous.HasValue ? (object)entity.IsAnonymous : System.DBNull.Value)); database.AddInParameter(commandWrapper, "@LastActivityDate", DbType.DateTime, (entity.LastActivityDate.HasValue ? (object)entity.LastActivityDate : System.DBNull.Value)); database.AddInParameter(commandWrapper, "@LastUpdatedDate", DbType.DateTime, (entity.LastUpdatedDate.HasValue ? (object)entity.LastUpdatedDate : System.DBNull.Value)); int results = 0; //Provider Data Requesting Command Event OnDataRequesting(new CommandEventArgs(commandWrapper, "Insert", entity)); if (transactionManager != null) { results = Utility.ExecuteNonQuery(transactionManager, commandWrapper); } else { results = Utility.ExecuteNonQuery(database, commandWrapper); } object _uniqueId = database.GetParameterValue(commandWrapper, "@UniqueId"); entity.UniqueId = (int)_uniqueId; entity.AcceptChanges(); //Provider Data Requested Command Event OnDataRequested(new CommandEventArgs(commandWrapper, "Insert", entity)); return(Convert.ToBoolean(results)); }
private void Child_Update(Profile profile, SqlConnection connection) { bool cancel = false; OnChildUpdating(profile, connection, ref cancel); if (cancel) return; if(connection.State != ConnectionState.Open) connection.Open(); const string commandText = "UPDATE [dbo].[Cart] SET [UniqueID] = @p_UniqueID, [ItemId] = @p_ItemId, [Name] = @p_Name, [Type] = @p_Type, [Price] = @p_Price, [CategoryId] = @p_CategoryId, [ProductId] = @p_ProductId, [IsShoppingCart] = @p_IsShoppingCart, [Quantity] = @p_Quantity WHERE [CartId] = @p_CartId"; using(var command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_CartId", this.CartId); command.Parameters.AddWithValue("@p_UniqueID", profile != null ? profile.UniqueID : this.UniqueID); command.Parameters.AddWithValue("@p_ItemId", this.ItemId); command.Parameters.AddWithValue("@p_Name", this.Name); command.Parameters.AddWithValue("@p_Type", this.Type); command.Parameters.AddWithValue("@p_Price", this.Price); command.Parameters.AddWithValue("@p_CategoryId", this.CategoryId); command.Parameters.AddWithValue("@p_ProductId", this.ProductId); command.Parameters.AddWithValue("@p_IsShoppingCart", this.IsShoppingCart); command.Parameters.AddWithValue("@p_Quantity", this.Quantity); using(var reader = new SafeDataReader(command.ExecuteReader())) { if(reader.Read()) { } } // Update foreign keys values. This code will update the values passed in from the parent only if no errors occurred after executing the query. if(profile != null && profile.UniqueID != this.UniqueID) LoadProperty(_uniqueIDProperty, profile.UniqueID); } // A child relationship exists on this Business Object but its type is not a child type (E.G. EditableChild). // TODO: Please override OnChildUpdated() and update this child manually. // FieldManager.UpdateChildren(this, connection); OnChildUpdated(); }
private void Child_Insert(Profile profile, SqlConnection connection) { bool cancel = false; OnChildInserting(profile, connection, ref cancel); if (cancel) return; if(connection.State != ConnectionState.Open) connection.Open(); const string commandText = "INSERT INTO [dbo].[Cart] ([UniqueID], [ItemId], [Name], [Type], [Price], [CategoryId], [ProductId], [IsShoppingCart], [Quantity]) VALUES (@p_UniqueID, @p_ItemId, @p_Name, @p_Type, @p_Price, @p_CategoryId, @p_ProductId, @p_IsShoppingCart, @p_Quantity); SELECT [CartId] FROM [dbo].[Cart] WHERE CartId = SCOPE_IDENTITY()"; using(var command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_UniqueID", profile != null ? profile.UniqueID : this.UniqueID); command.Parameters.AddWithValue("@p_ItemId", this.ItemId); command.Parameters.AddWithValue("@p_Name", this.Name); command.Parameters.AddWithValue("@p_Type", this.Type); command.Parameters.AddWithValue("@p_Price", this.Price); command.Parameters.AddWithValue("@p_CategoryId", this.CategoryId); command.Parameters.AddWithValue("@p_ProductId", this.ProductId); command.Parameters.AddWithValue("@p_IsShoppingCart", this.IsShoppingCart); command.Parameters.AddWithValue("@p_Quantity", this.Quantity); using(var reader = new SafeDataReader(command.ExecuteReader())) { if(reader.Read()) { // Update identity primary key value. LoadProperty(_cartIdProperty, reader.GetInt32("CartId")); } } // Update foreign keys values. This code will update the values passed in from the parent only if no errors occurred after executing the query. if(profile != null && profile.UniqueID != this.UniqueID) LoadProperty(_uniqueIDProperty, profile.UniqueID); } // A child relationship exists on this Business Object but its type is not a child type (E.G. EditableChild). // TODO: Please override OnChildInserted() and insert this child manually. // FieldManager.UpdateChildren(this, connection); OnChildInserted(); }
private void Child_Update(Profile profile, SqlConnection connection) { bool cancel = false; OnChildUpdating(profile, connection, ref cancel); if (cancel) return; if(connection.State != ConnectionState.Open) connection.Open(); const string commandText = "UPDATE [dbo].[Account] SET [UniqueID] = @p_UniqueID, [Email] = @p_Email, [FirstName] = @p_FirstName, [LastName] = @p_LastName, [Address1] = @p_Address1, [Address2] = @p_Address2, [City] = @p_City, [State] = @p_State, [Zip] = @p_Zip, [Country] = @p_Country, [Phone] = @p_Phone WHERE [AccountId] = @p_AccountId"; using(var command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_AccountId", this.AccountId); command.Parameters.AddWithValue("@p_UniqueID", profile != null ? profile.UniqueID : this.UniqueID); command.Parameters.AddWithValue("@p_Email", this.Email); command.Parameters.AddWithValue("@p_FirstName", this.FirstName); command.Parameters.AddWithValue("@p_LastName", this.LastName); command.Parameters.AddWithValue("@p_Address1", this.Address1); command.Parameters.AddWithValue("@p_Address2", ADOHelper.NullCheck(this.Address2)); command.Parameters.AddWithValue("@p_City", this.City); command.Parameters.AddWithValue("@p_State", this.State); command.Parameters.AddWithValue("@p_Zip", this.Zip); command.Parameters.AddWithValue("@p_Country", this.Country); command.Parameters.AddWithValue("@p_Phone", ADOHelper.NullCheck(this.Phone)); using(var reader = new SafeDataReader(command.ExecuteReader())) { if(reader.Read()) { } } // Update foreign keys values. This code will update the values passed in from the parent only if no errors occurred after executing the query. if(profile != null && profile.UniqueID != this.UniqueID) LoadProperty(_uniqueIDProperty, profile.UniqueID); } // A child relationship exists on this Business Object but its type is not a child type (E.G. EditableChild). // TODO: Please override OnChildUpdated() and update this child manually. // FieldManager.UpdateChildren(this, connection); OnChildUpdated(); }
private void Child_Insert(Profile profile, SqlConnection connection) { bool cancel = false; OnChildInserting(profile, connection, ref cancel); if (cancel) return; if(connection.State != ConnectionState.Open) connection.Open(); const string commandText = "INSERT INTO [dbo].[Account] ([UniqueID], [Email], [FirstName], [LastName], [Address1], [Address2], [City], [State], [Zip], [Country], [Phone]) VALUES (@p_UniqueID, @p_Email, @p_FirstName, @p_LastName, @p_Address1, @p_Address2, @p_City, @p_State, @p_Zip, @p_Country, @p_Phone); SELECT [AccountId] FROM [dbo].[Account] WHERE AccountId = SCOPE_IDENTITY()"; using(var command = new SqlCommand(commandText, connection)) { command.Parameters.AddWithValue("@p_UniqueID", profile != null ? profile.UniqueID : this.UniqueID); command.Parameters.AddWithValue("@p_Email", this.Email); command.Parameters.AddWithValue("@p_FirstName", this.FirstName); command.Parameters.AddWithValue("@p_LastName", this.LastName); command.Parameters.AddWithValue("@p_Address1", this.Address1); command.Parameters.AddWithValue("@p_Address2", ADOHelper.NullCheck(this.Address2)); command.Parameters.AddWithValue("@p_City", this.City); command.Parameters.AddWithValue("@p_State", this.State); command.Parameters.AddWithValue("@p_Zip", this.Zip); command.Parameters.AddWithValue("@p_Country", this.Country); command.Parameters.AddWithValue("@p_Phone", ADOHelper.NullCheck(this.Phone)); using(var reader = new SafeDataReader(command.ExecuteReader())) { if(reader.Read()) { // Update identity primary key value. LoadProperty(_accountIdProperty, reader.GetInt32("AccountId")); } } // Update foreign keys values. This code will update the values passed in from the parent only if no errors occurred after executing the query. if(profile != null && profile.UniqueID != this.UniqueID) LoadProperty(_uniqueIDProperty, profile.UniqueID); } // A child relationship exists on this Business Object but its type is not a child type (E.G. EditableChild). // TODO: Please override OnChildInserted() and insert this child manually. // FieldManager.UpdateChildren(this, connection); OnChildInserted(); }
/// <summary> /// Creates a new user. /// </summary> /// <param name="username">The username.</param> /// <param name="isAnonymous">True if the the user anonymous.</param> /// <returns>A newly created user.</returns> public Profile CreateUser(string username, bool isAnonymous) { var profile = new Profile(); profile.Username = username; profile.ApplicationName = ".NET Pet Shop 4.0"; profile.IsAnonymous = isAnonymous; profile.LastActivityDate = DateTime.Now; profile.LastUpdatedDate = DateTime.Now; var profileService = new ProfileService(); profileService.DeepSave(profile); return profile; }