コード例 #1
0
ファイル: AuthController.cs プロジェクト: ArcticTom/TCGCF.API
 public AuthController(CardInfoContext context, UserManager <User> userMgr, ILogger <AuthController> logger, IPasswordHasher <User> hasher, IHostingEnvironment env, IConfiguration config)
 {
     _context = context;
     _userMgr = userMgr;
     _logger  = logger;
     _hasher  = hasher;
     _env     = env;
     _config  = config;
 }
コード例 #2
0
 public CardInfoRepository(CardInfoContext context)
 {
     _context = context;
 }
コード例 #3
0
ファイル: Startup.cs プロジェクト: ArcticTom/TCGCF.API
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, CardInfoContext context)
        {
            if (env.IsDevelopment())
            {
                //set developer friendly error page for dev environment
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler();
            }

            //enable IP throttling
            app.UseIpRateLimiting();

            //tell automapper all mappings used between Entities and Models to remove manual mapping work
            AutoMapper.Mapper.Initialize(c =>
            {
                c.CreateMap <Set, SetWithNoCardsDTO>();
                c.CreateMap <Set, SetDTO>();
                c.CreateMap <Deck, DeckWithNoCardsDTO>();
                c.CreateMap <Deck, DeckDTO>();
                c.CreateMap <Card, CardDTO>()
                .ForMember(d => d.Loyalty, opt => opt.ResolveUsing(e => e.Loyalty == 0 ? null : e.Loyalty))
                .ForMember(d => d.LinkedCard, opt => opt.ResolveUsing(e => e.LinkedCard == 0 ? null : e.LinkedCard));
                c.CreateMap <Card, CardNoIdDTO>()
                .ForMember(d => d.Loyalty, opt => opt.ResolveUsing(e => e.Loyalty == 0 ? null : e.Loyalty))
                .ForMember(d => d.LinkedCard, opt => opt.ResolveUsing(e => e.LinkedCard == 0 ? null : e.LinkedCard));
                c.CreateMap <CardsInDeck, CardsInDeckDTO>();
                c.CreateMap <Legality, LegalityDTO>();
                c.CreateMap <CardToDeckAddingDTO, CardsInDeck>();
                c.CreateMap <DeckAddingDTO, Deck>();
                c.CreateMap <DeckUpdatingDTO, Deck>();
                c.CreateMap <Deck, DeckUpdatingDTO>();
                c.CreateMap <Game, GameDTO>();
                c.CreateMap <Game, GameWithNoSetsDTO>();
                c.CreateMap <Format, FormatDTO>();
                c.CreateMap <SetType, SetTypeDTO>();
                c.CreateMap <Language, LanguageDTO>()
                .ForMember(d => d.LanguageName, opt => opt.ResolveUsing(e => e.LanguageString()));
                c.CreateMap <Rarity, RarityDTO>()
                .ForMember(d => d.Name, opt => opt.ResolveUsing(e => e.RarityName()));
                c.CreateMap <CardLayout, CardLayoutDTO>();
                c.CreateMap <Color, ColorDTO>()
                .ForMember(d => d.Name, opt => opt.ResolveUsing(e => e.ColorName()));
                c.CreateMap <ColorIdentity, ColorIdentityDTO>()
                .ForMember(d => d.Name, opt => opt.ResolveUsing(e => e.ColorIdentityName()));
                c.CreateMap <CardSuperType, CardSuperTypeDTO>();
                c.CreateMap <CardType, CardTypeDTO>();
                c.CreateMap <CardSubType, CardSubTypeDTO>();

                c.CreateMap <ImportSet, Set>()
                .ForMember(d => d.ReleaseDate, opt => opt.ResolveUsing(e => DateTime.ParseExact(e.ReleaseDate, "yyyy-MM-dd", null)))
                .ForMember(d => d.Abbreviation, opt => opt.MapFrom(e => e.Code))
                .ForMember(d => d.SetType, opt => opt.ResolveUsing(e => new SetType()
                {
                    Name = e.Type
                }))
                .ForMember(d => d.NumberOfCards, opt => opt.ResolveUsing(e => e.NumberOfCards()));
                c.CreateMap <ImportCard, Card>()
                .ForMember(d => d.FlavorText, opt => opt.MapFrom(e => e.Flavor))
                .ForMember(d => d.Number, opt => opt.ResolveUsing(e => e.MciNumber == null ? "" : e.MciNumber))
                .ForMember(d => d.RulesText, opt => opt.MapFrom(e => e.Text))
                .ForMember(d => d.Image, opt => opt.ResolveUsing(e => "https://www.google.com/"))
                .ForMember(d => d.Loyalty, opt => opt.ResolveUsing(e => e.Loyalty == null ? 0 : e.Loyalty))
                .ForMember(d => d.LinkedCard, opt => opt.ResolveUsing(e => 0))
                .ForMember(d => d.CardLayout, opt => opt.ResolveUsing(e => new CardLayout()
                {
                    Type = e.Layout
                }))
                .ForMember(d => d.ColorIdentity, opt => opt.Ignore())
                .ForMember(d => d.Rarity, opt => opt.Ignore())
                .ForMember(d => d.Language, opt => opt.ResolveUsing(e => new Language()
                {
                    CardName = e.Name, LanguageName = 0
                }));

                //to map to a versioned model
                //c.CreateMap<Deck, DeckWithNoCardsDTOTest>().IncludeBase<Deck, DeckWithNoCardsDTO>();
                //to map to object with a different name
                //c.CreateMap<Example, ExampleDTO>().ForMember(c => c.StartDate, opt => opt.MapFrom(example => example.EventDate))
                //to calculate new values on the fly
                //.ForMember(c => c.EndDate, opt => opt.ResolveUsing(example => example.EventDate.AddDays(example.Length - 1)));
            });

            //only initialize data if project is built running instead of entity framework tooling

            if (Configuration["DesignTime"] != "true")
            {
                using (var scope = app.ApplicationServices.CreateScope())
                {
                    var userInitializer = scope.ServiceProvider.GetRequiredService <UserDataInitializer>();
                    userInitializer.Seed().Wait();
                    var cardInitializer = scope.ServiceProvider.GetRequiredService <CardDataInitializer>();
                    cardInitializer.Seed().Wait();
                }
            }


            //enable authentication
            app.UseAuthentication();

            //enable statuscode modification
            app.UseStatusCodePages();
            //enable mvc
            app.UseMvc();
        }
コード例 #4
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);
        }