public static InventoryCard AddMagicCardToCollection(MyDbContext context, MagicCard magicCard, CardCollection collection, int insertionIndex = 0) { var inventoryCard = new InventoryCard { DisplayName = magicCard.DisplayName, uuid = magicCard.uuid, multiverseId_Inv = magicCard.multiverseId, CollectionId = collection.Id, InsertionIndex = insertionIndex, Virtual = collection.Virtual }; if (magicCard.isFoilOnly) { inventoryCard.Foil = true; } else { inventoryCard.Foil = false; } if (magicCard.PartB != null) { inventoryCard.PartB_uuid = magicCard.PartB.uuid; } context.Library.Add(inventoryCard); return(inventoryCard); }
public static void AddMagicCardsToCollection(List <OLVCardItem> cards, CardCollection collection) { var setItems = new Dictionary <string, OLVSetItem>(); using (MyDbContext context = new MyDbContext()) { var cardsAdded = new List <InventoryCard>(); int insertionIndex = 0; foreach (OLVCardItem cardItem in cards) { var card = cardItem.MagicCard; var inventoryCard = AddMagicCardToCollection(context, card, collection, insertionIndex); cardsAdded.Add(inventoryCard); insertionIndex++; if (!setItems.TryGetValue(card.Edition, out OLVSetItem setItem)) { if ((setItem = Globals.Forms.DBViewForm.SetItems.FirstOrDefault(x => x.Name == card.Edition)) != null) { setItems.Add(card.Edition, setItem); } } } context.SaveChanges(); var cvForm = Globals.Forms.OpenCollectionForms.FirstOrDefault(x => x.Collection.Id == collection.Id); var fullCardsAdded = new List <FullInventoryCard>(); foreach (InventoryCard card in cardsAdded) { if (cvForm != null) { var fullCard = card.ToFullCard(context); if (fullCard != null) { fullCardsAdded.Add(fullCard); } } if (!card.Virtual && Globals.Collections.MagicCardCache.TryGetValue(card.uuid, out MagicCard magicCard)) { magicCard.CopiesOwned++; Globals.Forms.DBViewForm.cardListView.RefreshObject(magicCard); } } if (cvForm != null) { cvForm.AddFullInventoryCards(fullCardsAdded); } Globals.Forms.DBViewForm.setListView.RefreshObjects(setItems.Values.ToArray()); if (SettingsManager.ApplicationSettings.AutoFetchCardPricesOnAdd) { var pricesToFetch = new List <FullInventoryCard>(); foreach (FullInventoryCard fullInventoryCard in fullCardsAdded) { if (fullInventoryCard.tcgplayerMarketPrice == null) { pricesToFetch.Add(fullInventoryCard); } } if (pricesToFetch.Count > 0) { FetchPrices(pricesToFetch); } } } }
public static void MoveFullInventoryCardsToCollection(ArrayList fullInventoryCards, CollectionViewForm sourceCVForm, CardCollection collection) { var cardsList = new List <FullInventoryCard>(); try { using (var context = new MyDbContext()) { foreach (FullInventoryCard fullInventoryCard in fullInventoryCards) { fullInventoryCard.CollectionId = collection.Id; if (fullInventoryCard.Virtual != collection.Virtual) { fullInventoryCard.TimeAdded = DateTime.Now; } fullInventoryCard.Virtual = collection.Virtual; context.Update(fullInventoryCard.InventoryCard); cardsList.Add(fullInventoryCard); } context.SaveChanges(); } sourceCVForm.RemoveFullInventoryCards(cardsList); Globals.Forms.OpenCollectionForms.FirstOrDefault(x => x.Collection.Id == collection.Id)?.AddFullInventoryCards(cardsList); } catch (Exception ex) { DebugOutput.WriteLine(ex.ToString()); MessageBox.Show("Could not move cards to collection."); } }