public static void InsertCard(CardApiDm cardApi, string setId) { using (var db = new MtgEntities()) { try { cardApi.Editions = cardApi.Editions.Where(e => e.Set_Id == setId).ToList(); var em = CardMapper.Map(cardApi); if (db.Cards.Any(c => c.CardId == em.CardId)) { foreach (var cardSet in em.CardSets.Where(s => !db.CardSets.Any(dbSet => dbSet.CardId == s.CardId && dbSet.SetId == s.SetId))) { db.CardSets.Add(cardSet); } } else { db.Cards.Add(em); } db.SaveChanges(); } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"Unable to save the card '{cardApi.Name}' Excpetion:"); Console.WriteLine(e); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.White; } } }
public static Card Map(CardApiDm apiDm) { if (apiDm.Supertypes == null) { apiDm.Supertypes = new List <string>(); } apiDm.Supertypes.AddRange(apiDm.Types); var em = new Card { CardId = apiDm.Id, CardName = apiDm.Name, ConvertedManaCost = apiDm.Cmc, Cost = apiDm.Cost, Commander = apiDm.Formats.Commander, Legacy = apiDm.Formats.Legacy, Standard = apiDm.Formats.Standard, Modern = apiDm.Formats.Modern, Vintage = apiDm.Formats.Vintage, Power = apiDm.Power, Toughness = apiDm.Toughness, RulesText = apiDm.Text, StoreUrl = apiDm.Store_Url, Url = apiDm.Url, Types = string.Join(", ", apiDm.Supertypes), SubTypes = string.Join(", ", apiDm.Subtypes ?? new List <string>()), Colors = string.Join(", ", apiDm.Colors ?? new List <string>()), CardSets = MapSets(apiDm).ToList() }; return(em); }
private static List <CardSet> MapSets(CardApiDm apiDm) { return(apiDm.Editions.Select(set => new CardSet { Artist = set.Artist, CardId = apiDm.Id, CardNumber = set.Number, CardSetId = 0, FlavorText = set.Flavor, HighPrice = (decimal)(set.PriceApi?.High ?? 0), MedianPrice = (decimal)(set.PriceApi?.Medium ?? 0), LowPrice = (decimal)(set.PriceApi?.Low ?? 0), ImageUrl = set.Image_Url, MultiverseId = Convert.ToInt64(set.Multiverse_Id == "" ? "0" : set.Multiverse_Id), Rarity = set.Rarity, SetId = set.Set_Id, StoreUrl = set.Store_Url }).ToList()); }