Exemplo n.º 1
0
        public SearchModule () 
        {
            Get ["/search"] = parameters => {
                SearchModel model = new SearchModel();
                model.Planeswalker = ((Planeswalker)this.Context.CurrentUser);
                model.ActiveMenu = "search";

                return View["Search", model];
            };

            Post ["/search"] = parameters => {
                SearchModel model = this.Bind<SearchModel>();
                model.Planeswalker = ((Planeswalker)this.Context.CurrentUser);
                UserCard [] walkerCards = null;

                try
                {
                    Card[] cards = magicdb.Search(model.Term);
                    model.ActiveMenu = "search";

                    cards = cards
                        .AsEnumerable()
                        .OrderBy(x => x.Name)
                        .ThenByDescending(x => x.ReleasedAt).ToArray();

                    if(model.Planeswalker != null)
                    {
                        int [] cardIds = cards.AsEnumerable().Select(c => c.Id).ToArray();
                        walkerCards = repository.GetUserCards(model.Planeswalker.Id,cardIds);
                    }

                    foreach(var c in cards)
                    {
                        CardInfo cardInfo = new CardInfo();

                        if(walkerCards != null && walkerCards.Length > 0)
                        {
                            cardInfo.Amount = walkerCards.AsEnumerable()
                                .Where(info => info.MultiverseId == c.Id)
                                .Select(info => info.Amount).FirstOrDefault();

                        }
                        else
                        {
                            cardInfo.Amount = 0;
                        }

                        cardInfo.Card = c;
                        model.Cards.Add(cardInfo);
                    }
                }
                catch(Exception e)
                {
                    model.Errors.Add(e.Message);
                }
               
                return View["Search", model];
            };
        }
Exemplo n.º 2
0
        public CardModule()
        {
            this.RequiresAuthentication();

            Post ["/cards/{id}/amount/{count}"] = parameters => {
                int  multiverseId = (int)parameters.id;
                int  count        = (int)parameters.count;
                Guid walkerId     = ((Planeswalker)this.Context.CurrentUser).Id;

                repository.AddUserCard(walkerId, multiverseId, count);

                return(count.ToString());
            };

            //TODO: Refactor this, to long and confusing
            Get ["/mycards/{setId?}", true] = async(parameters, ct) => {
                PlaneswalkerModel model = new PlaneswalkerModel();
                model.ActiveMenu = "mycards";
                string setId = (string)parameters.setId;
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                if (setId == null)
                {
                    //try to get an setId if no value was provided in the url
                    setId = repository.GetSetCardCounts(model.Planeswalker.Id)
                            .Select(x => x.Key)
                            .OrderBy(x => x).FirstOrDefault();

                    //if still no setId then user has no cards
                    if (setId == null)
                    {
                        PageModel pm = new PageModel();
                        pm.ActiveMenu   = "mycards";
                        pm.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                        pm.Errors.Add("You have no cards in this set or no cards in your collection");
                        return(View["Page", pm]);
                    }
                }

                CardSet current = magicdb.GetSet(setId);
                //force type into block if block is null
                model.Block = current.Block ?? current.Type;

                try
                {
                    model.Counts = repository.GetSetCardCounts(model.Planeswalker.Id);
                    //if no counts then no cards again
                    if (model.Counts == null ||
                        model.Counts.Count == 0)
                    {
                        PageModel pm = new PageModel();
                        pm.ActiveMenu   = "mycards";
                        pm.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                        pm.Errors.Add("You have no cards in this set or no cards in your collection");
                        return(View["Page", pm]);
                    }

                    model.TotalCards  = model.Counts.Sum(x => x.Value);
                    model.TotalAmount = repository.GetUserCards(model.Planeswalker.Id).Sum(x => x.Amount);

                    List <string> blocks;

                    if (model.Counts.Count > 0)
                    {
                        model.Sets = magicdb.GetSets(model.Counts
                                                     .AsEnumerable().Select(x => x.Key)
                                                     .ToArray());

                        //keep a list of all sets user has cards in
                        //this is to get the default set for each block or type
                        model.AllSets = model.Sets;

                        //get all blocks
                        blocks = model.Sets
                                 .Select(x => x.Block)
                                 .Where(x => x != null)
                                 .Distinct()
                                 .OrderBy(x => x).ToList();
                        //force types into blocks
                        blocks.AddRange(model.Sets
                                        .Where(x => x.Block == null)
                                        .Select(x => x.Type)
                                        .Distinct()
                                        .OrderBy(x => x).ToList());

                        //macke dure no dupes
                        blocks = blocks.Distinct().ToList();

                        foreach (string block in blocks)
                        {
                            List <CardSet> sets = new List <CardSet>();
                            sets = model.Sets.Where(x => x.Block == block).ToList();
                            sets.AddRange(model.Sets.Where(x => x.Type == block && x.Block == null).ToList());

                            int total = 0;
                            foreach (CardSet set in sets)
                            {
                                total += model.Counts[set.Id];
                            }
                            if (block != null)
                            {
                                model.Blocks.Add(block, total);
                            }
                        }

                        //filter sets for current block or type
                        model.Sets = model.Sets
                                     .Where(x => x.Block == model.Block || x.Type == model.Block)
                                     .OrderBy(x => x.Name).ToArray();

                        //nothing in the set
                        if (model.Sets == null || model.Sets.Length == 0)
                        {
                            PageModel pm = new PageModel();
                            pm.ActiveMenu   = "mycards";
                            pm.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                            pm.Errors.Add("You have no cards in this set or no cards in your collection");
                            return(View["Page", pm]);
                        }

                        if (setId == null)
                        {
                            setId = model.Sets.FirstOrDefault().Id;
                        }

                        model.SetId     = setId;
                        model.UserCards = repository.GetUserCards(model.Planeswalker.Id, setId);

                        //man same here no cards
                        if (model.UserCards == null ||
                            model.UserCards.Length == 0)
                        {
                            PageModel pm = new PageModel();
                            pm.ActiveMenu   = "mycards";
                            pm.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                            pm.Errors.Add("You have no cards in this set or no cards in your collection");
                            return(View["Page", pm]);
                        }

                        Card[] dbcards = null;
                        if (Request.Query.Show != null)
                        {
                            dbcards    = magicdb.GetSetCards(model.SetId);
                            model.Show = true;
                        }
                        else
                        {
                            dbcards = magicdb.GetCards(model.UserCards
                                                       .AsEnumerable()
                                                       .Where(x => x.Amount > 0)
                                                       .Select(x => x.MultiverseId)
                                                       .ToArray());

                            model.Show = false;
                        }

                        //same here no cards, this is getting old
                        if (dbcards == null ||
                            dbcards.Length == 0)
                        {
                            PageModel pm = new PageModel();
                            pm.ActiveMenu   = "mycards";
                            pm.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                            pm.Errors.Add("You have no cards in this set or no cards in your collection");
                            return(View["Page", pm]);
                        }

                        List <CardInfo> cards = new List <CardInfo>();

                        CardInfo card = null;
                        foreach (Card c in dbcards)
                        {
                            card        = new CardInfo();
                            card.Amount = model.UserCards
                                          .AsEnumerable()
                                          .Where(x => x.MultiverseId == c.Id)
                                          .Select(x => x.Amount)
                                          .FirstOrDefault();

                            card.Card = c;

                            cards.Add(card);
                        }

                        model.Cards = cards.OrderBy(x => x.Card.SetNumber).ToArray();
                    }
                    else
                    {
                        model.Messages.Add("You have no cards in your library.");
                    }
                }
                catch (Exception e)
                {
                    model.Errors.Add(e.Message);
                }

                return(View["MyCards", model]);
            };

            //can use this for public profile stuff
            Get ["/pw/{planeswalker}/cards"] = parameters => {
                PageModel model = new PageModel();
                model.ActiveMenu   = "mycards";
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                return(View["Page", model]);
            };
        }
Exemplo n.º 3
0
        public CardModule ()
        {
            this.RequiresAuthentication ();

            Get["/cr/{status?}"] = parameters => {
                ChangeRequestModel model = new ChangeRequestModel();

                string status = (string)parameters.status;
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                model.Title = "M:tgDb.Info Admin";

                if(status == null)
                {
                    model.Changes = repository.GetChangeRequests().ToList();
                }
                else
                {
                    model.Changes = repository.GetChangeRequests(status).ToList();
                }

                return View["Change/ChangeRequests", model];
            };

            Get["/cards/{id}/logs"] = parameters => {
                CardLogsModel model =   new CardLogsModel();
                model.ActiveMenu =      "sets";
                model.Planeswalker =    (Planeswalker)this.Context.CurrentUser;
                model.Mvid =            (int)parameters.id;

                try
                {
                    model.Changes = repository.GetCardChangeRequests((int)parameters.id)
                    .OrderByDescending(x => x.Version) 
                    .ToList();
                }
                catch(Exception e)
                {
                    model.Errors.Add(e.Message);
                }

                return View["Change/CardLogs", model];
            };

            Get["/cards/{id}/logs/{logid}"] = parameters => {
                CardChange model = new CardChange();
            
                try
                {
                    model = repository.GetCardChangeRequest((Guid)parameters.logid);
                }
                catch(Exception e)
                {
                    model.Errors.Add(e.Message);
                }

                model.ActiveMenu =      "sets";
                model.Planeswalker =    (Planeswalker)this.Context.CurrentUser;
                model.Mvid =            (int)parameters.id;

               
                return View["Change/CardChange", model];
            };

            Post["/change/{id}/field/{field}"] = parameters => {
                Admin admin =               new Admin(ConfigurationManager.AppSettings.Get("api"));
                Planeswalker planeswalker = (Planeswalker)this.Context.CurrentUser;
                Guid changeId  =            Guid.Parse((string)parameters.id);
                string field =              (string)parameters.field;
                CardChange change =         null;

                if(!planeswalker.InRole("admin"))
                {
                    return HttpStatusCode.Unauthorized;
                }

                try
                {
                    change = repository.GetCardChangeRequest(changeId);

                    if(field == "close")
                    {
                        repository.UpdateCardChangeStatus(change.Id, "Closed");

                        return Response.AsRedirect(string.Format("/cards/{0}/logs/{1}",
                            change.Mvid, change.Id));
                    }

                    if(field == "open")
                    {
                        repository.UpdateCardChangeStatus(change.Id, "Pending");

                        return Response.AsRedirect(string.Format("/cards/{0}/logs/{1}",
                            change.Mvid, change.Id));
                    }


                    if(field == "formats")
                    {
                        admin.UpdateCardFormats(planeswalker.AuthToken,
                            change.Mvid, change.Formats);
                    }
                    else if(field == "rulings")
                    {
                        admin.UpdateCardRulings(planeswalker.AuthToken,
                            change.Mvid, change.Rulings);
                    }
                    else
                    {
                        string value = change.GetFieldValue(field);
                        admin.UpdateCardField(planeswalker.AuthToken,
                            change.Mvid, field, (string)value);
                    }

                    repository.UpdateCardChangeStatus(change.Id, "Accepted", field);
                }
                catch(Exception e)
                {
                    throw e;
                }
                    
                return Response.AsRedirect(string.Format("/cards/{0}/logs/{1}",
                    change.Mvid, change.Id));
            };
                
            Get ["/cards/{id}/change"] = parameters => {
                CardChange model = new CardChange();
                Card card; 

                try
                {
                    card = magicdb.GetCard((int)parameters.id);
                    model = CardChange.MapCard(card);
                }
                catch(Exception e)
                {
                    throw e;
                }


                return View["Change/Card", model];
            };

            Post ["/cards/{id}/change"] = parameters => {
                CardChange model =      this.Bind<CardChange>();
                Card current =          magicdb.GetCard((int)parameters.id);
                model.CardSetId =       current.CardSetId;
                model.CardSetName =     current.CardSetName;
                model.Name =            current.Name;
                model.Mvid =            (int)parameters.id;
                model.Rulings =         Bind.Rulings(Request);
                model.Formats =         Bind.Formats(Request);
                model.Planeswalker =    (Planeswalker)this.Context.CurrentUser;
                model.UserId =          model.Planeswalker.Id;
                CardChange card =       null;

                var result = this.Validate(model);

                if (!result.IsValid)
                {
                    model.Errors = ErrorUtility.GetValidationErrors(result);
                    return View["Change/Card", model];
                }
                    
                try
                {
                    card = repository.GetCardChangeRequest(
                        repository.AddCardChangeRequest(model)
                    );
                }
                catch(Exception e)
                {
                    model.Errors.Add(e.Message);
                    return View["Change/Card", model];
                }

                return Response.AsRedirect(string.Format("/cards/{0}/logs?v={1}", 
                    card.Mvid, card.Version));

                //return model.Description;
                //return View["Change/Card", model];

            };
                
            Post ["/cards/{id}/amount/{count}"] = parameters => {
                int multiverseId =  (int)parameters.id;
                int count =         (int)parameters.count;
                Guid walkerId =     ((Planeswalker)this.Context.CurrentUser).Id;
      
                repository.AddUserCard(walkerId,multiverseId,count);
               
                return count.ToString();
            };

            //TODO: Refactor this, to long and confusing 
            Get ["/pw/{planeswalker}/blocks/{block}/cards/{setId?}"] = parameters => {
                PlaneswalkerModel model =   new PlaneswalkerModel();
                model.ActiveMenu =          "mycards";
                string setId =              (string)parameters.setId;
                model.Block =               (string)parameters.block;
                model.Planeswalker =        (Planeswalker)this.Context.CurrentUser;

                if(model.Planeswalker.UserName.ToLower() != 
                    ((string)parameters.planeswalker).ToLower())
                {
                    model.Errors.Add(string.Format("Tsk Tsk! {0}, this profile is not yours.",
                        model.Planeswalker.UserName));

                    return View["Page", model];
                }

                try
                {
                    model.Counts = repository.GetSetCardCounts(model.Planeswalker.Id);

                    if(model.Counts == null || 
                        model.Counts.Count == 0)
                    {
                        return Response.AsRedirect("~/" + model.Planeswalker.UserName + 
                            "/cards");
                    }

                    model.TotalCards = model.Counts.Sum(x => x.Value);
                    model.TotalAmount = repository.GetUserCards(model.Planeswalker.Id).Sum(x => x.Amount);
                    
                    string[] blocks;

                    if(model.Counts.Count > 0)
                    {
                        model.Sets = magicdb.GetSets(model.Counts
                            .AsEnumerable().Select(x => x.Key)
                            .ToArray());

                        blocks = model.Sets.Select(x => x.Block).Distinct().OrderBy(x => x).ToArray();

                        foreach(string block in blocks)
                        {
                            CardSet[] sets = model.Sets.Where(x => x.Block == block).ToArray();
                        
                            int total = 0; 
                            foreach(CardSet set in sets)
                            {
                                total += model.Counts[set.Id];
                            }

                            model.Blocks.Add(block,total);
                        }

                        model.Sets = model.Sets
                            .Where(x => x.Block == model.Block)
                            .OrderBy(x => x.Name).ToArray();

                        if(model.Sets == null || model.Sets.Length == 0)
                        {
                            return Response.AsRedirect(string.Format("~/{0}/cards", 
                                model.Planeswalker.UserName));
                        }
                       

                        if(setId == null)
                        {
                            setId = model.Sets.FirstOrDefault().Id;
                        }

                        model.SetId = setId;
                        model.UserCards = repository.GetUserCards(model.Planeswalker.Id, setId);

                        if(model.UserCards == null || 
                            model.UserCards.Length  == 0)
                        {
                            return Response.AsRedirect(string.Format("~/{0}/cards", 
                                model.Planeswalker.UserName));
                        }


                        Card[] dbcards = null;
                        if(Request.Query.Show != null)
                        {
                            dbcards = magicdb.GetSetCards(model.SetId);
                            model.Show = true;
                        }
                        else
                        {
                            dbcards = magicdb.GetCards(model.UserCards
                                .AsEnumerable()
                                .Where(x => x.Amount > 0)
                                .Select(x => x.MultiverseId)
                                .ToArray());

                            model.Show = false;
                        }

                        if(dbcards == null || 
                            dbcards.Length == 0)
                        {
                            return Response.AsRedirect(string.Format("~/{0}/cards", 
                                model.Planeswalker.UserName));
                        }

                        List<CardInfo> cards = new List<CardInfo>();

                        CardInfo card = null;
                        foreach(Card c in dbcards)
                        {
                            card = new CardInfo();
                            card.Amount = model.UserCards
                                .AsEnumerable()
                                .Where(x => x.MultiverseId == c.Id)
                                .Select(x => x.Amount)
                                .FirstOrDefault();

                            card.Card = c;
                        
                            cards.Add(card);
                        }

                        model.Cards = cards.OrderBy(x => x.Card.SetNumber).ToArray();
                    }
                    else
                    {
                        model.Messages.Add("You have no cards in your library.");
                    }
                }
                catch(Exception e)
                {
                    model.Errors.Add(e.Message);
                }

                return View["MyCards", model];
            };

            Get ["/pw/{planeswalker}/cards"] = parameters => {
                PageModel model =       new PageModel();
                model.ActiveMenu =      "mycards";
                model.Planeswalker =    (Planeswalker)this.Context.CurrentUser;

                if(model.Planeswalker.UserName.ToLower() != 
                    ((string)parameters.planeswalker).ToLower())
                {
                    model.Errors.Add(string.Format("Tsk Tsk! {0}, this profile is not yours.",
                        model.Planeswalker.UserName));

                    return View["Page", model];
                }

                try
                {
                    string setId = repository.GetSetCardCounts(model.Planeswalker.Id)
                        .Select(x => x.Key)
                        .OrderBy(x => x).FirstOrDefault();

                    if(setId != null)
                    {
                        CardSet s = magicdb.GetSet(setId);
                      
                        return Response.AsRedirect(string.Format("/pw/{0}/blocks/{1}/cards",
                            model.Planeswalker.UserName, s.Block ));
                    }

                    model.Information.Add("You have no cards yet in your library. " +
                        "Browse the sets and start adding cards Planeswalker!");
                }
                catch(Exception e)
                {
                    model.Errors.Add(e.Message);
                }

                return View["Page", model];
            };
        }
Exemplo n.º 4
0
        public IndexModule()
        {
            Get ["/about"] = parameters => {
                PageModel model = new PageModel();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                model.ActiveMenu   = "about";

                return(View["about", model]);
            };

            Get ["/faq"] = parameters => {
                PageModel model = new PageModel();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                model.ActiveMenu   = "faq";

                return(View["faq", model]);
            };

            Get ["/terms"] = parameters => {
                PageModel model = new PageModel();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                return(View["Terms", model]);
            };

            Get ["/"] = parameters => {
                IndexModel model = new IndexModel();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                Card card = new Card();

                try
                {
                    model.SetList.Sets = magicdb.GetSets();
                    model.RandomCards.Add(magicdb.GetRandomCard());
                    model.RandomCards.Add(magicdb.GetRandomCard());
                    model.RandomCards.Add(magicdb.GetRandomCard());
                    model.RandomCards.Add(magicdb.GetRandomCard());
                    model.RandomCards.Add(magicdb.GetRandomCard());
                }
                catch (Exception e)
                {
                    model.Errors.Add(e.Message);
                }


                return(View["Index", model]);
            };

            Get ["/sets/"] = parameters => {
                SetsModel model = new SetsModel();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                try
                {
                    model.Sets       = magicdb.GetSets();
                    model.ActiveMenu = "sets";

                    if (model.Planeswalker != null)
                    {
                        model.UserCards = repository.GetSetCardCounts(model.Planeswalker.Id);
                    }
                }
                catch (Exception e)
                {
                    model.Errors.Add(e.Message);
                }

                return(View["Sets", model]);
            };

            Get ["/cards/{id}"] = parameters => {
                CardModel model = new CardModel();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                try
                {
                    model.Card       = magicdb.GetCard((int)parameters.id);
                    model.Prints     = magicdb.GetCards(model.Card.Name);
                    model.Page       = (model.Card.SetNumber / _pageSize) + 1;
                    model.Set        = magicdb.GetSet(model.Card.CardSetId);
                    model.ActiveMenu = "sets";

                    model.NextCard = magicdb.GetCardInSet(model.Set.Id,
                                                          model.Card.SetNumber + 1);

                    model.PrevCard = magicdb.GetCardInSet(model.Set.Id,
                                                          model.Card.SetNumber - 1);

                    if (model.Planeswalker != null)
                    {
                        UserCard uCard = repository.GetUserCards(model.Planeswalker.Id,
                                                                 new int[] { model.Card.Id }).FirstOrDefault();

                        if (uCard != null)
                        {
                            model.Amount = uCard.Amount;
                        }
                        else
                        {
                            model.Amount = 0;
                        }
                    }
                }
                catch (Exception e)
                {
                    model.Errors.Add(e.Message);
                }

                return(View["Card", model]);
            };

            Get ["/sets/{id}"] = parameters => {
                BookModel model = new BookModel();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                string setId = (string)parameters.id;
                model.ActiveMenu = "sets";

                int page = 1;
                try
                {
                    model.SetList.Sets      = magicdb.GetSets();
                    model.SetList.ActiveSet = setId;
                    model.Set = magicdb.GetSet(setId);

                    if (Request.Query.Page != null)
                    {
                        if (int.TryParse((string)Request.Query.Page, out page))
                        {
                            if (page < 1)
                            {
                                page = 1;
                            }
                        }
                    }


                    int    end   = page * _pageSize;
                    int    start = page > 1 ? end - _pageSize : page;
                    Card[] cards = magicdb.GetSetCards(setId, page > 1 ? start + 1 : start,
                                                       end);

                    UserCard [] walkerCards = null;

                    if (model.Planeswalker != null)
                    {
                        int [] cardIds = cards.AsEnumerable().Select(c => c.Id).ToArray();
                        walkerCards = repository.GetUserCards(model.Planeswalker.Id, cardIds);
                    }

                    foreach (var c in cards)
                    {
                        CardInfo cardInfo = new CardInfo();
                        if (walkerCards != null && walkerCards.Length > 0)
                        {
                            cardInfo.Amount = walkerCards.AsEnumerable()
                                              .Where(info => info.MultiverseId == c.Id)
                                              .Select(info => info.Amount).FirstOrDefault();
                        }
                        else
                        {
                            cardInfo.Amount = 0;
                        }

                        cardInfo.Card = c;
                        model.Cards.Add(cardInfo);
                    }

                    model.Page     = page;
                    model.NextPage = page + 1;
                    model.PrevPage = page > 1 ? page - 1 : page;
                }
                catch (Exception e)
                {
                    model.Errors.Add(e.Message);
                }

                return(View["Book", model]);
            };

            Get ["/home"] = parameters => {
                IndexModel model = new IndexModel();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                return(View["Index", model]);
            };


            Get ["/pw/{planeswalker}"] = parameters => {
                PlaneswalkerModel model = new PlaneswalkerModel();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                model.ActiveMenu   = "planeswalker";

                string planeswalker = ((string)parameters.planeswalker).ToLower();

                try
                {
                    if (model.Planeswalker != null &&
                        model.Planeswalker.Profile.Name.ToLower() != planeswalker)
                    {
                        model.Profile = repository.GetProfile(planeswalker);

                        if (model.Profile == null)
                        {
                            model.Messages.Add("Planeswalker does not exist or is private profile.");
                        }
                        else
                        {
                            model.UserCards = repository.GetUserCards(model.Profile.Id);
                        }
                    }
                    else if (model.Planeswalker != null)
                    {
                        model.Profile   = model.Planeswalker.Profile;
                        model.UserCards = repository.GetUserCards(model.Profile.Id);
                    }
                    else
                    {
                        model.Profile = repository.GetProfile(planeswalker);

                        if (model.Profile == null)
                        {
                            model.Messages.Add("Planeswalker does not exist or is private profile.");
                        }
                        else
                        {
                            model.UserCards = repository.GetUserCards(model.Profile.Id);
                        }
                    }
                }
                catch (Exception e)
                {
                    model.Errors.Add(e.Message);
                }

                return(View["Planeswalker", model]);
            };
        }
Exemplo n.º 5
0
        public IndexModule ()
        {
            Get ["/about"] = parameters => {
                PageModel model = new PageModel();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                return View["about", model];
            };

			Get ["/FAQ"] = parameters => {
                PageModel model = new PageModel();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
				model.ActiveMenu = "FAQ";

				return View["FAQ", model];
            };

            Get ["/terms"] = parameters => {
                PageModel model = new PageModel();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
              
                return View["Terms", model];
            };

            Get ["/"] = parameters => {
                IndexModel model = new IndexModel();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                try
                {
                    model.SetList.Sets = magicdb.GetSets();
                }
                catch(Exception e)
                {
                    model.Errors.Add(e.Message);
                }


                return View["Index", model];
            };
         
            Get ["/sets/"] = parameters => {
                SetsModel model = new SetsModel();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                try
                {
                    model.Sets = magicdb.GetSets();
                    model.ActiveMenu = "sets";

                    if(model.Planeswalker != null)
                    {
                        model.UserCards = repository.GetSetCardCounts(model.Planeswalker.Id);
                    }
                }
                catch(Exception e)
                {
                    model.Errors.Add(e.Message);
                }

                return View["Sets", model];
            };

            Get ["/cards/{id}"] = parameters => {
                CardModel model = new CardModel();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                try
                {
                    model.Card = magicdb.GetCard((int)parameters.id);
                    model.Prints = magicdb.GetCards(model.Card.Name);
                    model.Page = (model.Card.SetNumber / _pageSize) + 1;
                    model.ActiveMenu = "sets";

                    if(model.Planeswalker != null)
                    {
                        UserCard uCard = repository.GetUserCards(model.Planeswalker.Id,
                            new int[]{model.Card.Id}).FirstOrDefault();

                        if(uCard != null)
                        {
                            model.Amount = uCard.Amount;
                        }
                        else
                        {
                            model.Amount = 0;
                        }
                    }
                }
                catch(Exception e)
                {
                    model.Errors.Add(e.Message);
                }
                    
                return View["Card", model];
            };
          
            Get ["/sets/{id}"] = parameters => {
                BookModel model = new BookModel();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                string setId = (string)parameters.id;
                model.ActiveMenu = "sets";

                int page = 1; 
                try
                {
                    model.SetList.Sets = magicdb.GetSets();
                    model.SetList.ActiveSet = setId;

                    if(Request.Query.Page != null)
                    {
                        if(int.TryParse((string)Request.Query.Page, out page))
                        {
                            if(page < 1){ page = 1; }
                        }
                    }

                   
                    int end = page * _pageSize;
                    int start = page > 1 ? end - _pageSize : page;
                    Card[] cards = magicdb.GetSetCards(setId, start, 
                        page > 1 ? end - 1 : end);

                    UserCard [] walkerCards = null;

                    if(model.Planeswalker != null)
                    {
                        int [] cardIds = cards.AsEnumerable().Select(c => c.Id).ToArray();
                        walkerCards = repository.GetUserCards(model.Planeswalker.Id,cardIds);
                    }

                    foreach(var c in cards)
                    {
                        CardInfo cardInfo = new CardInfo();
                        if(walkerCards != null && walkerCards.Length > 0)
                        {
                            cardInfo.Amount = walkerCards.AsEnumerable()
                                .Where(info => info.MultiverseId == c.Id)
                                .Select(info => info.Amount).FirstOrDefault();

                        }
                        else
                        {
                            cardInfo.Amount = 0;
                        }

                        cardInfo.Card = c;
                        model.Cards.Add(cardInfo);
                    }

                    model.Page = page;
                    model.NextPage = page + 1;
                    model.PrevPage = page > 1 ? page - 1 : page;
                }
                catch(Exception e)
                {
                    model.Errors.Add(e.Message);
                }

                return View["Book", model];
            };

            Get ["/home"] = parameters => {
                IndexModel model = new IndexModel();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                return View["Index", model];
            };

          
            Get ["/pw/{planeswalker}"] = parameters => {
                PlaneswalkerModel model = new PlaneswalkerModel();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                string planeswalker = ((string)parameters.planeswalker).ToLower();

                try
                {
                    if(model.Planeswalker != null && 
                        model.Planeswalker.Profile.Name.ToLower() != planeswalker)
                    {
                        model.Profile = repository.GetProfile(planeswalker);

                        if(model.Profile == null)
                        {
                            model.Messages.Add("Planeswalker does not exist or is private profile.");
                        }
                        else
                        {
                            model.UserCards = repository.GetUserCards(model.Profile.Id);
                        }
                    }
                    else if(model.Planeswalker != null)
                    {
                        model.Profile = model.Planeswalker.Profile;
                        model.UserCards = repository.GetUserCards(model.Profile.Id);
                    }
                    else
                    {
                        model.Profile = repository.GetProfile(planeswalker);

                        if(model.Profile == null)
                        {
                            model.Messages.Add("Planeswalker does not exist or is private profile.");
                        }
                        else
                        {
                            model.UserCards = repository.GetUserCards(model.Profile.Id);
                        }
                    }
                }
                catch(Exception e)
                {
                    model.Errors.Add(e.Message);
                }

                return View["Planeswalker", model];
            };
        }