public async Task <bool> SaveChanges()
 {
     return((await _context.SaveChangesAsync()) > 0);
 }
Exemple #2
0
        public static async Task <string> Begin(IConfiguration config)
        {
            var jsonresult = "";

            //create dbcontext options from config
            var optionsBuilder = new DbContextOptionsBuilder <CardInfoContext>();

            optionsBuilder.UseNpgsql(config["connectionStrings:TCGCardFetcher"]);

            using (CardInfoContext context = new CardInfoContext(optionsBuilder.Options))
            {
                //set base game data
                List <Game> _sample = new List <Game>
                {
                    new Game()
                    {
                        Name               = "Magic: The Gathering",
                        Abbreviation       = "MTG",
                        Published          = DateTime.Parse("1993-08-05 00:00:00.000000"),
                        Publisher          = "Wizards of the Coast",
                        Website            = "https://magic.wizards.com/en",
                        Description        = "Magic can be played by two or more players in various formats, which fall into two categories: constructed and limited. Limited formats involve players building a deck spontaneously out of a pool of random cards with a minimum deck size of 40 cards. In constructed, players create decks from cards they own, usually 60 cards with no more than 4 of any given card. Each game represents a battle between wizards known as planeswalkers, who employ spells, artifacts, and creatures depicted on individual Magic cards to defeat their opponents.",
                        AvailableOnConsole = true,
                        AvailableOnMobile  = true,
                        AvailableOnPaper   = true,
                        AvailableOnPC      = true,
                        Formats            = new List <Format>
                        {
                            new Format()
                            {
                                Name          = "Standard",
                                NumberOfCards = 60,
                                CopyLimit     = 4,
                                Category      = "Constructed",
                                Description   = ""
                            },
                            new Format()
                            {
                                Name          = "Modern",
                                NumberOfCards = 60,
                                CopyLimit     = 4,
                                Category      = "Constructed",
                                Description   = ""
                            },
                            new Format()
                            {
                                Name          = "Legacy",
                                NumberOfCards = 60,
                                CopyLimit     = 4,
                                Category      = "Constructed",
                                Description   = ""
                            },
                            new Format()
                            {
                                Name          = "Vintage",
                                NumberOfCards = 60,
                                CopyLimit     = 4,
                                Category      = "Constructed",
                                Description   = ""
                            },
                            new Format()
                            {
                                Name          = "Commander",
                                NumberOfCards = 100,
                                CopyLimit     = 1,
                                Category      = "Casual",
                                Description   = ""
                            },
                            new Format()
                            {
                                Name          = "Sealed",
                                NumberOfCards = 40,
                                CopyLimit     = 40,
                                Category      = "Limited",
                                Description   = ""
                            },
                            new Format()
                            {
                                Name          = "Draft",
                                NumberOfCards = 40,
                                CopyLimit     = 40,
                                Category      = "Limited",
                                Description   = ""
                            },
                            new Format()
                            {
                                Name          = "Pauper",
                                NumberOfCards = 60,
                                CopyLimit     = 4,
                                Category      = "Casual",
                                Description   = ""
                            },
                            new Format()
                            {
                                Name          = "Frontier",
                                NumberOfCards = 60,
                                CopyLimit     = 4,
                                Category      = "Casual",
                                Description   = ""
                            },
                            new Format()
                            {
                                Name          = "Arena",
                                NumberOfCards = 60,
                                CopyLimit     = 4,
                                Category      = "Constructed",
                                Description   = ""
                            }
                        }
                    }
                };

                //empty all data before records are added
                context.Database.ExecuteSqlCommand("TRUNCATE TABLE \"Games\" RESTART IDENTITY CASCADE;");

                context.Games.AddRange(_sample);

                await context.SaveChangesAsync();
            }

            //read set data from json file
            JObject jObject   = JObject.Load(new JsonTextReader(File.OpenText("enrichData.json")));
            JArray  resources = (JArray)jObject["set"];

            //fetch more data for each set from online, bind to the model and enrich it
            foreach (var set in resources)
            {
                jsonresult = await DownloadJSON("https://mtgjson.com/json/" + set["id"].ToString() + "-x.json");

                if (jsonresult != null)
                {
                    using (CardInfoContext context = new CardInfoContext(optionsBuilder.Options))
                    {
                        var modelresult   = ConvertToModel(jsonresult);
                        var enrichedModel = EnrichModel(modelresult);

                        //add set to context
                        context.Sets.Add(enrichedModel);

                        //set power and toughness even when null
                        var nulls = enrichedModel.Cards.Where(item => item.Power == null);
                        foreach (Card card in nulls)
                        {
                            card.Power     = " ";
                            card.Toughness = " ";
                        }

                        //update cards in context
                        context.Cards.UpdateRange(nulls);

                        await context.SaveChangesAsync();
                    }
                }
            }

            return(jsonresult);
        }