Exemplo n.º 1
0
        // TODO: Should not be bulk inserting with EF
        /// <summary>
        /// Requires MTG_Sets table to have values
        /// </summary>
        /// <returns></returns>
        public async Task UpdateCardsTable()
        {
            context.ChangeTracker.AutoDetectChangesEnabled = false;
            //https://scryfall.com/docs/api/bulk-data

            Console.WriteLine("Starting update cards tables");

            List <CardApi> cards         = new List <CardApi>();
            List <Card>    current_cards = context.MTG_Cards.ToList();

            IRestClient client = new RestClient();

            string json;

            using (WebClient wc = new WebClient())
            {
                json = wc.DownloadString("https://c2.scryfall.com/file/scryfall-bulk/default-cards/default-cards-20210119220408.json");
            }

            cards = JsonConvert.DeserializeObject <List <CardApi> >(json);
            Console.WriteLine("Converted to object");

            int i = 0;

            foreach (CardApi item in cards)
            {
                if (current_cards.Any(x => x.Oracle_ID == item.oracle_id && x.Set == item.set))
                {
                    continue;
                }

                Card card = new Card
                {
                    Oracle_ID        = item.oracle_id,
                    Name             = item.Name,
                    Layout           = item.layout,
                    Image_Small      = item.image_uris?.small.ToString(),
                    Image_Normal     = item.image_uris?.normal.ToString(),
                    Image_Large      = item.image_uris?.large.ToString(),
                    Image_Art_Crop   = item.image_uris?.art_crop.ToString(),
                    Digital          = Convert.ToInt32(item.digital),
                    Rarity           = (int)Enum.Parse(typeof(eRarity), item.rarity),
                    Collector_Number = item.collector_number,
                    Set        = item.set,
                    Reprint    = item.reprint,
                    Promo      = item.promo,
                    HasFoil    = item.foil,
                    HasNonFoil = item.nonfoil,

                    ManaCost       = item.mana_cost,
                    CMC            = item.cmc,
                    Type_Line      = item.type_line,
                    Oracle_Text    = item.oracle_text,
                    Power          = item.power,
                    Toughness      = item.toughness,
                    Colors         = item.colors is null ? null : string.Join(',', item.colors),
                    Color_Identity = string.Join(',', item.color_identity),
                    Keywords       = string.Join(',', item.keywords),
                    Loyalty        = item.loyalty,

                    Legalality    = item.legalities.ToString(),
                    Hand_Modifier = item.hand_modifier,

                    Artist       = item.artist,
                    Booster      = item.booster,
                    Border_Color = item.border_color,
                    Flavor_text  = item.flavor_text,
                    Frame_Effect = item.frame_effects is null ? null : string.Join(',', item.frame_effects),
                    Full_Art     = item.full_art,

                    USD      = item.prices.usd,
                    USD_Foil = item.prices.usd_foil,
                    EUR      = item.prices.eur,
                    EUR_Foil = item.prices.eur_foil
                };

                context.MTG_Cards.Add(card);
                i++;
                if (i == 200)
                {
                    i = 0;
                    context.SaveChanges();
                }
            }
            context.SaveChanges();

            context.ChangeTracker.AutoDetectChangesEnabled = true;
        }