public void Create(Price price) { if (price == null) throw new ArgumentNullException("price"); if (price.Id == Guid.Empty) price.Id = Guid.NewGuid(); if (price.ProductId == Guid.Empty) throw new ArgumentNullException("ProductId"); using (var connection = new SqlConnection(_connectionString)) { connection.Open(); using (var command = connection.CreateCommand()) { /// Check id command.CommandText = string.Format("select 1 from [BUSINESS.WMS.PRICE] where [GUID_RECORD] = '{0}'", price.Id); using (var reader = command.ExecuteReader()) { if (reader.HasRows) throw new Exception("Record already exists"); } /// Check product id command.CommandText = string.Format("select 1 from [BUSINESS.WMS.PRICE] where [ITEM_GUID] = '{0}'", price.ProductId); using (var reader = command.ExecuteReader()) { if (reader.HasRows) throw new Exception("Record already exists"); } command.CommandText = @"insert into [BUSINESS.WMS.PRICE] ( [GUID_RECORD], [ITEM_GUID], [CURRENCY_GUID], [LOCATION_GUID], [PRICE], [BATCH_GUID], [HIDDEN], [DELETED]) values (@GUID_RECORD, @ITEM_GUID, @CURRENCY_GUID, @LOCATION_GUID, @PRICE, @BATCH_GUID, @HIDDEN, @DELETED)"; command.Parameters.AddWithValue("@GUID_RECORD", price.Id); command.Parameters.AddWithValue("@ITEM_GUID", price.ProductId); command.Parameters.AddWithValue("@CURRENCY_GUID", DBNull.Value); command.Parameters.AddWithValue("@LOCATION_GUID", DBNull.Value); command.Parameters.AddWithValue("@PRICE", price.ProductPrice); command.Parameters.AddWithValue("@BATCH_GUID", DBNull.Value); command.Parameters.AddWithValue("@HIDDEN", 0); command.Parameters.AddWithValue("@DELETED", 0); command.ExecuteNonQuery(); } } }
public void Update(Price price) { if (price == null) throw new ArgumentNullException("price"); if (price.Id == Guid.Empty) throw new ArgumentNullException("Id"); if (price.ProductId == Guid.Empty) throw new ArgumentNullException("ProductId"); var record = GetById(price.Id); var alterColumns = new List<string>(); var alterValues = new List<SqlParameter>(); if (!Guid.Equals(record.ProductId, price.ProductId)) { alterColumns.Add("[ITEM_GUID] = @ITEM_GUID"); alterValues.Add(new SqlParameter("@ITEM_GUID", price.ProductId)); } if (!Guid.Equals(record.ProductPrice, price.ProductPrice)) { alterColumns.Add("[PRICE] = @PRICE"); alterValues.Add(new SqlParameter("@PRICE", price.ProductPrice)); } if (alterColumns.Any()) { using (var connection = new SqlConnection(_connectionString)) { connection.Open(); using (var command = connection.CreateCommand()) { command.CommandText = String.Format( "update [BUSINESS.WMS.PRICE] set {0} where [GUID_RECORD] = @GUID_RECORD", String.Join(", ", alterColumns)); command.Parameters.AddWithValue("@GUID_RECORD", price.Id); command.Parameters.AddRange(alterValues.ToArray()); command.ExecuteNonQuery(); } } } }