コード例 #1
0
        public List <Card> GetCardList(Collection collection)
        {
            var cardNames = new List <string>();
            var setCodes  = new List <string>();

            foreach (var collectionCard in collection.Cards)
            {
                cardNames.Add(collectionCard.Name);
                setCodes.Add(collectionCard.SetCode);
            }

            using (var db = new FortyLifeDbContext())
            {
                var query = string.Empty;

                for (var i = 0; i < cardNames.Count; i++)
                {
                    query += $"\"{cardNames[i]}\" " +
                             (!string.IsNullOrEmpty(setCodes[i]) ? $"set:{setCodes[i]}" : string.Empty) +
                             " or ";
                }

                var searchResultList = CardListRequest(query);

                if (searchResultList != null)
                {
                    var cards = searchResultList.Data;

                    foreach (var card in cards)
                    {
                        if (card != null)
                        {
                            card.CacheDate = DateTime.Now;

                            if (card.ImageUris == null)
                            {
                                card.ImageUris = new ImageUris();
                            }

                            if (card.IsDoubleFaced)
                            {
                                card.CardFaces[0].CacheDate = DateTime.Now;
                                card.CardFaces[1].CacheDate = DateTime.Now;

                                if (card.CardFaces[0].ImageUris == null)
                                {
                                    card.CardFaces[0].ImageUris = new ImageUris();
                                }

                                if (card.CardFaces[1].ImageUris == null)
                                {
                                    card.CardFaces[1].ImageUris = new ImageUris();
                                }

                                foreach (var i in card.CardFaces)
                                {
                                    if (i.Colors != null)
                                    {
                                        i.ColorsString = PrimitiveListHandler.ToString(i.Colors);
                                    }
                                }
                            }

                            if (card.Colors != null)
                            {
                                card.ColorsString = PrimitiveListHandler.ToString(card.Colors);
                            }

                            if (card.ColorIdentity != null)
                            {
                                card.ColorIdentityString = PrimitiveListHandler.ToString(card.ColorIdentity);
                            }

                            if (card.Games != null)
                            {
                                card.GamesString = PrimitiveListHandler.ToString(card.Games);
                            }

                            if (card.MultiverseIds != null)
                            {
                                card.MultiverseIdsString = PrimitiveListHandler.ToString(card.MultiverseIds);
                            }

                            //db.Cards.AddOrUpdate(card);

                            //try
                            //{
                            //    db.SaveChanges();
                            //}
                            //catch (DbEntityValidationException dbEx)
                            //{
                            //    foreach (var validationErrors in dbEx.EntityValidationErrors)
                            //    {
                            //        foreach (var validationError in validationErrors.ValidationErrors)
                            //        {
                            //            Trace.TraceInformation("Property: {0} Error: {1}",
                            //                validationError.PropertyName,
                            //                validationError.ErrorMessage);
                            //        }
                            //    }

                            //    throw;
                            //}
                        }
                    }

                    return(cards);
                }
            }

            return(null);
        }
コード例 #2
0
        public Card GetCard(string cardName, string setCode = "")
        {
            using (var db = new FortyLifeDbContext())
            {
                Card card;

                //if (!string.IsNullOrEmpty(setCode))
                //{
                //    if (db.Cards.Any(i => i.Name == cardName && i.Set == setCode && DbFunctions.DiffDays(i.CacheDate, DateTime.Now) < 7))
                //    {
                //        var cards = db.Cards.Include(i => i.CardFaces);

                //        card = cards.FirstOrDefault(i => i.Name == cardName && i.Set == setCode);

                //        if (card != null)
                //        {
                //            // prep list data by parsing string data from db
                //            foreach (var i in card.CardFaces)
                //            {
                //                if (i.ColorsString != null)
                //                {
                //                    i.Colors = i.ColorsString.Split(',').ToList();
                //                }
                //            }

                //            card.Colors = card.ColorsString?.Split(',').ToList();
                //            card.ColorIdentity = card.ColorIdentityString?.Split(',').ToList();
                //            card.Games = card.GamesString?.Split(',').ToList();
                //            card.MultiverseIds = card.MultiverseIdsString?.Split(',').ToList();

                //            // return prepped card
                //            return card;
                //        }
                //    }
                //}
                //else
                //{
                //    if (db.Cards.Any(i => i.Name == cardName && DbFunctions.DiffDays(i.CacheDate, DateTime.Now) < 7))
                //    {
                //        var cards = db.Cards.Include(i => i.CardFaces);

                //        card = cards.FirstOrDefault(i => i.Name == cardName);

                //        if (card != null)
                //        {
                //            // prep list data by parsing string data from db
                //            foreach (var i in card.CardFaces)
                //            {
                //                if (i.ColorsString != null)
                //                {
                //                    i.Colors = i.ColorsString.Split(',').ToList();
                //                }
                //            }

                //            card.Colors = card.ColorsString?.Split(',').ToList();
                //            card.ColorIdentity = card.ColorIdentityString?.Split(',').ToList();
                //            card.Games = card.GamesString?.Split(',').ToList();
                //            card.MultiverseIds = card.MultiverseIdsString?.Split(',').ToList();

                //            // return prepped card
                //            return card;
                //        }
                //    }
                //}

                var searchResultList = CardPrintingsRequest(cardName);

                if (searchResultList != null)
                {
                    card = !string.IsNullOrEmpty(setCode)
                        ? searchResultList.Data?.FirstOrDefault(i =>
                                                                string.Equals(i.Name, cardName, StringComparison.CurrentCultureIgnoreCase) &&
                                                                string.Equals(i.Set, setCode, StringComparison.CurrentCultureIgnoreCase))
                        : searchResultList.Data?.FirstOrDefault(i => string.Equals(i.Name, cardName, StringComparison.CurrentCultureIgnoreCase));

                    if (card != null)
                    {
                        card.CacheDate = DateTime.Now;

                        if (card.ImageUris == null)
                        {
                            card.ImageUris = new ImageUris();
                        }

                        if (card.IsDoubleFaced)
                        {
                            card.CardFaces[0].CacheDate = DateTime.Now;
                            card.CardFaces[1].CacheDate = DateTime.Now;

                            if (card.CardFaces[0].ImageUris == null)
                            {
                                card.CardFaces[0].ImageUris = new ImageUris();
                            }

                            if (card.CardFaces[1].ImageUris == null)
                            {
                                card.CardFaces[1].ImageUris = new ImageUris();
                            }

                            foreach (var i in card.CardFaces)
                            {
                                if (i.Colors != null)
                                {
                                    i.ColorsString = PrimitiveListHandler.ToString(i.Colors);
                                }
                            }
                        }

                        if (card.Colors != null)
                        {
                            card.ColorsString = PrimitiveListHandler.ToString(card.Colors);
                        }

                        if (card.ColorIdentity != null)
                        {
                            card.ColorIdentityString = PrimitiveListHandler.ToString(card.ColorIdentity);
                        }

                        if (card.Games != null)
                        {
                            card.GamesString = PrimitiveListHandler.ToString(card.Games);
                        }

                        if (card.MultiverseIds != null)
                        {
                            card.MultiverseIdsString = PrimitiveListHandler.ToString(card.MultiverseIds);
                        }

                        //db.Cards.AddOrUpdate(card);

                        //try
                        //{
                        //    db.SaveChanges();
                        //}
                        //catch (DbEntityValidationException dbEx)
                        //{
                        //    foreach (var validationErrors in dbEx.EntityValidationErrors)
                        //    {
                        //        foreach (var validationError in validationErrors.ValidationErrors)
                        //        {
                        //            Trace.TraceInformation("Property: {0} Error: {1}",
                        //                validationError.PropertyName,
                        //                validationError.ErrorMessage);
                        //        }
                        //    }

                        //    throw;
                        //}
                    }

                    return(card);
                }
            }

            return(null);
        }