Beispiel #1
0
        private static void ImportFile(string file)
        {
            if (!mFileSystem.File.Exists(file))
            {
                throw new FileNotFoundException(file);
            }

            mDataReader.FileName = file;

            mLoggingService.Debug("Reading set from '{0}'...", file);
            var setData = mFileSystem.File.ReadAllText(file);

            var set = mDataReader.ReadSet(setData);

            if (set == null)
            {
                throw new Exception("Set not found! Aborting.");
            }

            if (set.Type.ToLower() == "promo" && mSkipPromos)
            {
                mLoggingService.Info("Set '{0}' is a promp; skipping...", set.Name);

                return;
            }

            mLoggingService.Debug("Inserting set '{0}' [{1}]...",
                                  set.Name,
                                  set.Code);

            var cards = mDataReader.ReadCards(setData);

            var status = mImporter.Import(set, cards).Result;

            if (status != null)
            {
                mLoggingService.Info("Import Successful. Set={0}; CardsImported={1}; CardsFailed={2}",
                                     status.ImportedSet.Name,
                                     status.ImportedCards.Count,
                                     status.FailedCards.Count);
            }
            else
            {
                mLoggingService.Error("Import failed");
            }

            mLoggingService.Debug("Done reading cards from set '{0}'!", file);
        }
Beispiel #2
0
        public AdminModule(
            IMtgStore mtgStore,
            ILoggingService loggingService,
            ICardPriceStore priceStore,
            IHttpClient httpClient,
            IMtgDataReader mtgDataReader,
            IImporter importer)
            : base("/admin")
        {
            Get["/"] = parameters =>
            {
                if (this.Context.CurrentUser == null)
                {
                    return(this.Context.GetRedirect("/admin/login"));
                }
                else
                {
                    return(View["admin.html"]);
                }
            };

            Get["/login"] = parameters =>
            {
                return(View["login.html"]);
            };


            Get["/logout"] = parameters =>
            {
                return(this.LogoutAndRedirect("/admin/login/"));
            };


            Post["/login"] = args =>
            {
                var userGuid = UserMapper.ValidateUser((string)this.Request.Form.Username, (string)this.Request.Form.Password);

                if (userGuid == null)
                {
                    return(this.Context.GetRedirect("/admin/login"));
                }

                DateTime?expiry = null;
                if (this.Request.Form.RememberMe.HasValue)
                {
                    expiry = DateTime.Now.AddDays(7);
                }

                return(this.LoginAndRedirect(userGuid.Value, expiry, "/admin"));
            };

            #region Sets
            Get["/sets", true] = async(parameters, ct) =>
            {
                this.RequiresAuthentication();

                var sets = await mtgStore.GetSets();

                return(View["listSets.html", new
                            {
                                Sets = sets.Select(s => new
                    {
                        Name = s.Name,
                        Code = s.Code,
                        Block = s.Block,
                        Type = s.Type,
                        ReleasedOn = s.ReleasedOn.ToShortDateString(),
                        ReleasedOnSort = s.ReleasedOn,
                        Symbol = s.SetAsKeyRuneIcon
                    }).OrderByDescending(s => s.ReleasedOnSort)
                            }]);
            };

            Get["/set/{set}", true] = async(parameters, ct) =>
            {
                this.RequiresAuthentication();

                string setCode = parameters.set;

                var set = await mtgStore.GetSetByCode(setCode);

                var db_cards = await mtgStore.GetCardsBySet(set.Name);

                // Get price information
                var cards = db_cards.Select(c => new
                {
                    Name              = c.Name,
                    Code              = c.SetId,
                    Set               = c.SetName,
                    Cost              = c.Cost,
                    CostSymbols       = c.CostWithSymbols,
                    Type              = c.FullType,
                    Rarity            = c.Rarity,
                    Img               = c.Img,
                    MultiverseId      = c.MultiverseId,
                    SearchName        = c.SearchName,
                    Symbol            = c.SetAsKeyRuneIcon,
                    Desc              = c.Desc,
                    DescSymbols       = c.DescWithSymbols,
                    ConvertedManaCost = c.Cmc,
                    SetSymbol         = c.SetAsKeyRuneIcon,
                    Prices            = GetCardPrice(priceStore, c.MultiverseId)
                }).OrderByDescending(c => c.SearchName);

                return(View["set.html", new
                            {
                                Set = set,
                                Cards = cards
                            }]);
            };
            #endregion

            #region Add Set
            Get["/addset"] = parameters =>
            {
                this.RequiresAuthentication();

                return(View["addSet.html"]);
            };

            Post["/addset", true] = async(parameters, ct) =>
            {
                this.RequiresAuthentication();

                var setData = this.Bind <SetAddData>();

                if (string.IsNullOrEmpty(setData.url))
                {
                    return(Response.AsJson(new
                    {
                        Error = "No url provided.",
                        Status = "Error"
                    }).StatusCode = HttpStatusCode.BadRequest);
                }

                string data = await httpClient.GetAsJson(setData.url);

                if (string.IsNullOrEmpty(data))
                {
                    return(Response.AsJson(new
                    {
                        Error = "Url contains invalid data or no data.",
                        Status = "Error"
                    }).StatusCode = HttpStatusCode.BadRequest);
                }

                var set = mtgDataReader.ReadSet(data);

                if (set == null)
                {
                    return(Response.AsJson(new
                    {
                        Error = "No set defined",
                        Status = "Error"
                    }).StatusCode = HttpStatusCode.BadRequest);
                }

                if (set.Type.ToLower() == "promo")
                {
                    return(Response.AsJson(new
                    {
                        Error = "Set is promo, skipping",
                        Status = "Error"
                    }).StatusCode = HttpStatusCode.BadRequest);
                }

                var cards = mtgDataReader.ReadCards(data);

                var status = await importer.Import(set, cards);

                if (status != null)
                {
                    return(Response.AsJson(new
                    {
                        Status = "Success",
                        CardsInserted = status.ImportedCards.Select(
                            c =>
                            new
                        {
                            Name = c.Name,
                            MultiverseId = c.MultiverseId,
                            Cost = c.Cost,
                            Type = c.FullType,
                            Img = c.Img,
                            Set = c.SetId
                        }).ToList(),
                        CardsFailed = status.FailedCards.Select(
                            c =>
                            new
                        {
                            Name = c.Name,
                            MultiverseId = c.MultiverseId,
                            Cost = c.Cost,
                            Type = c.FullType,
                            Img = c.Img,
                            Set = c.SetId
                        }).ToList()
                    }));
                }
                else
                {
                    return(Response.AsText("Import failed").StatusCode = HttpStatusCode.BadRequest);
                }
            };

            #endregion
        }