internal static Media Load(IDataReader reader) { int id = (int)reader["mediaID"]; if (cachedMedia.ContainsKey(id)) return cachedMedia[id]; else { Media m = new Media(reader); cachedMedia[id] = m; return m; } }
private InventoryItem(IDataReader reader) { Quality q; Enum.TryParse(reader["quality"].ToString(), out q); Quality = q; this.DatePurchased = (DateTime)reader["datePurchased"]; this.PurchasePrice = (double)(Decimal)reader["purchasePrice"]; this.Description = reader["description"].ToString(); this.DateSold = (reader["dateSold"] == DBNull.Value ? null : (DateTime?)reader["dateSold"]); this.SoldPrice = (reader["soldPrice"] == DBNull.Value ? null : (double?)(Decimal)reader["soldPrice"]); this.Description = reader["description"].ToString(); this.mediaId = (int)reader["mediaId"]; this.buyerId = (reader["buyer"] == DBNull.Value ? null : (int?)reader["buyer"]); this.sellerId = (reader["seller"] == DBNull.Value ? null : (int?)reader["seller"]); this.InvnetoryId = (int)reader["inventoryId"]; this.media = Media.Load(reader); }
/// <summary> /// Adds the purchased media to the list of inventory /// </summary> /// <param name="media">The Media that was purchased</param> /// <param name="customer">The customer that you purchased the media from (This field can be null)</param> /// <param name="datePurchased">The data the media was purchased</param> /// <param name="purchasePrice">The price paid for the media</param> /// <param name="quality">The quality of the media</param> /// <param name="description">A short description of the media(Look, Damages...) to be able to make sure you found the correct physical media</param> /// <returns>returns the new Inventory entry</returns> public static InventoryItem PurchaseInventory(Media media, Customer seller, DateTime datePurchased, double purchasePrice, Quality quality, string description) { using (SqlCeCommand cmd = new SqlCeCommand(@"insert into Inventory (datePurchased,purchasePrice,quality,mediaId,description,seller) values (@datePurchased,@purchasePrice,@quality,@mediaId,@description,@seller)")) { cmd.Parameters.AddWithValue("@datePurchased", datePurchased.Date); cmd.Parameters.AddWithValue("@purchasePrice", purchasePrice); cmd.Parameters.AddWithValue("@quality", QualityString(quality)); cmd.Parameters.AddWithValue("@mediaId", media.MediaId); cmd.Parameters.AddWithValue("@description", (object)description ?? DBNull.Value); cmd.Parameters.AddWithValue("@seller", seller != null ? (object)seller.CustomerId : DBNull.Value); int id = ExecuteCreate(cmd); using (SqlCeCommand select = new SqlCeCommand("select * from Inventory inner join Media on Media.MediaID = Inventory.MediaID where inventoryId = @inventoryId")) { select.Parameters.AddWithValue("@inventoryId", id); IDataReader reader = ExecuteReader(select); if (reader.Read()) { return InventoryItem.Load(reader); } throw new Exception("Unable to create the inventory item. Unknown error."); } } }