Ejemplo n.º 1
0
        public IndexModule(IRepository repository, Cache cache)
        {
            repo = repository;

            Get ["/"] = parameters =>
            {
                return(View ["index"]);
            };

            Get ["/search/", true] = async(parameters, ct) =>
            {
                string query = (string)Request.Query.q;
                int    limit = 0;
                int    start = 0;
                string total = "";

                if (Request.Query.limit != null)
                {
                    limit = (int)Request.Query.limit;
                }

                if (Request.Query.start != null)
                {
                    start = (int)Request.Query.start;
                }

                if (Request.Query.total != null)
                {
                    total = (string)Request.Query.total;
                    if (total.ToLower() == "true")
                    {
                        long count = await repo.SearchTotal(query, true);

                        return(Response.AsJson(count));
                    }
                }

                Card [] cards = await repo.Search(query, start, limit, true);

                return(Response.AsJson(cards));
            };


            Get ["/search/{text}", true] = async(parameters, ct) =>
            {
                int limit = 0;
                int start = 0;


                if (Request.Query.limit != null)
                {
                    limit = (int)Request.Query.limit;
                }

                if (Request.Query.start != null)
                {
                    start = (int)Request.Query.start;
                }

                Card [] cards = await repo.Search((string)parameters.text,
                                                  start, limit, false);

                return(Response.AsJson(cards));
            };

            Get ["/cards/types", true] = async(parameters, ct) =>
            {
                string [] types = await repo.GetCardTypes();

                return(Response.AsJson(types.OrderBy(x => x)));
            };

            Get ["/cards/subtypes", true] = async(parameters, ct) =>
            {
                string [] types = await repo.GetCardSubTypes();

                return(Response.AsJson(types.OrderBy(x => x)));
            };

            Get ["/cards/rarity", true] = async(parameters, ct) =>
            {
                string [] rarity = await repo.GetCardRarity();

                return(Response.AsJson(rarity.OrderBy(x => x)));
            };

            Get ["/cards/random", true] = async(parameters, ct) =>
            {
                JsonSettings.MaxJsonLength = 100000000;
                Card card = null;

                card = await repo.GetRandomCard();

                return(Response.AsJson(card));
            };

            Get ["/cards", true] = async(parameters, ct) =>
            {
                JsonSettings.MaxJsonLength = 100000000;
                Card[] cards = null;

                if (Request.Query.Fields != null)
                {
                    string[] fields = ((string)Request.Query.Fields).Split(',');

                    foreach (string field in fields)
                    {
                        if (typeof(Card).GetProperty(field, BindingFlags.IgnoreCase |
                                                     BindingFlags.Public | BindingFlags.Instance) == null)
                        {
                            return(Response.AsJson(string.Format("Field: {0} is invalid.", field),
                                                   HttpStatusCode.NotAcceptable));
                        }
                    }

                    cards = await repo.GetCards(Request.Query);

                    var c = cards
                            .AsQueryable()
                            .Select(string.Format("new ({0})",
                                                  (string)Request.Query.Fields));

                    return(Response.AsJson(c));
                }

                cards = await repo.GetCards(Request.Query);

                return(Response.AsJson(cards));
            };

            Get ["/cards/{id}", true] = async(parameters, ct) =>
            {
                try
                {
                    int[] multiverseIds =
                        Array.ConvertAll(((string)parameters.id).Split(','), int.Parse);

                    if (multiverseIds.Length > 1)
                    {
                        Card [] cards = await repo.GetCards(multiverseIds);

                        return(Response.AsJson(cards));
                    }
                }
                catch (Exception e)
                {
                    //swallo it, cannot convert parameter to int array
                }


                int id = 0;
                if (int.TryParse((string)parameters.id, out id))
                {
                    Card card = await repo.GetCard((int)parameters.id);

                    return(Response.AsJson(card));
                }
                else
                {
                    Card [] cards = await repo.GetCards((string)parameters.id);

                    return(Response.AsJson(cards));
                }
            };

            Get ["/sets/{id}", true] = async(parameters, ct) =>
            {
                string [] ids = ((string)parameters.id).Split(',');

                if (ids.Length > 1)
                {
                    CardSet[] cardSets = await repo.GetSets(ids);

                    return(Response.AsJson(cardSets));
                }

                CardSet cardSet = await repo.GetSet((string)parameters.id);

                return(Response.AsJson(cardSet));
            };

            Get ["/sets/", true] = async(parameters, ct) =>
            {
                CardSet[] cardset = await repo.GetSets();

                JsonSettings.MaxJsonLength = 1000000;

                return(Response.AsJson(cardset));
            };

            Get ["/sets/{id}/cards/random", true] = async(parameters, ct) =>
            {
                JsonSettings.MaxJsonLength = 100000000;
                Card card = null;

                card = await repo.GetRandomCardInSet((string)parameters.id);

                return(Response.AsJson(card));
            };


            Get ["/sets/{id}/cards/{setNumber}", true] = async(parameters, ct) =>
            {
                Card cards = await repo.GetCardBySetNumber((string)parameters.id,
                                                           (int)parameters.setNumber);

                return(Response.AsJson(cards));
            };

            Get ["/sets/{id}/cards/", true] = async(parameters, ct) =>
            {
                JsonSettings.MaxJsonLength = 100000000;
                int start = 0;
                int end   = 0;

                if (Request.Query.start != null)
                {
                    int.TryParse((string)Request.Query.start, out start);
                }

                if (Request.Query.end != null)
                {
                    int.TryParse((string)Request.Query.end, out end);
                }

                Card[] cards = null;
                if (start > 0 || end > 0)
                {
                    cards = await repo.GetCardsBySet((string)parameters.id, start, end);
                }
                else
                {
                    cards = await repo.GetCardsBySet((string)parameters.id);
                }

                if (Request.Query.Fields != null)
                {
                    var c = cards.AsQueryable()
                            .Select(string.Format("new ({0})", (string)Request.Query.Fields));

                    return(Response.AsJson(c));
                }

                return(Response.AsJson(cards));
            };
        }
Ejemplo n.º 2
0
        public IndexModule (IRepository repository)
        {
            repo = repository;

            Get ["/"] = parameters => 
            {
                return View ["index"];
            };

            Get ["/search/{text}", true] = async (parameters, ct) => 
            {
                Card [] cards = await repo.Search ((string)parameters.text);

                return Response.AsJson (cards);
            };

            Get ["/cards", true] = async (parameters, ct) => 
            {
                JsonSettings.MaxJsonLength =    100000000;
                Card[] cards =                  null;
                cards =                         await repo.GetCards (Request.Query);
                  
                if(Request.Query.Fields != null)
                {
                    var c = cards.AsQueryable()
                        .Select(string.Format("new ({0})",
                            (string)Request.Query.Fields));

                    return Response.AsJson(c);
                }
                    
                return Response.AsJson (cards);
            };

            Get ["/cards/{id}", true] = async (parameters, ct) => 
            {
                try
                {
                    int[] multiverseIds = 
                        Array.ConvertAll(((string)parameters.id).Split(','), int.Parse);

                    if(multiverseIds.Length > 1)
                    {
                        Card [] cards = await repo.GetCards(multiverseIds);

                        return Response.AsJson(cards);
                    }
                }
                catch(Exception e)
                {
                    //swallo it, cannot convert parameter to int array 
                }


                int id = 0; 
                if(int.TryParse((string)parameters.id, out id))
                {
                    Card card = await repo.GetCard ((int)parameters.id);
                    return Response.AsJson (card);
                }
                else
                {
                    Card [] cards = await repo.GetCards ((string)parameters.id);
                    return Response.AsJson (cards);
                }
            };

            Get ["/sets/{id}", true] = async (parameters, ct) =>  
            {
                string [] ids = ((string)parameters.id).Split(',');

                if(ids.Length > 1)
                {
                    CardSet[] cardSets = await repo.GetSets (ids);
                    return Response.AsJson (cardSets);
                }

                CardSet cardSet = await repo.GetSet ((string)parameters.id);
                return Response.AsJson (cardSet);
            };

            Get ["/sets/", true] = async (parameters, ct) => 
            {
                CardSet[] cardset =             await repo.GetSets ();
                JsonSettings.MaxJsonLength =    1000000;

                return Response.AsJson (cardset);
            };

            Get ["/sets/{id}/cards/", true] = async (parameters, ct) => 
            {
                JsonSettings.MaxJsonLength =    100000000;
                int start =                     0; 
                int end =                       0; 

                if(Request.Query.start != null )
                {
                    int.TryParse((string)Request.Query.start, out start);
                }

                if(Request.Query.end != null )
                {
                    int.TryParse((string)Request.Query.end, out end);
                }

                Card[] cards = null;
                if(start > 0 || end > 0)
                {
                    cards = await repo.GetCardsBySet ((string)parameters.id, start, end);
                }
                else
                {
                    cards = await repo.GetCardsBySet ((string)parameters.id);
                }

                if(Request.Query.Fields != null)
                {
                    var c = cards.AsQueryable()
                        .Select(string.Format("new ({0})",(string)Request.Query.Fields));

                    return Response.AsJson(c);
                }

                return Response.AsJson (cards);
            };
        }
Ejemplo n.º 3
0
 public void Test_get_multiple_sets()
 {
     CardSet[] sets = repository.GetSets(new string[] { "all", "arb" }).Result;
     Assert.Greater(sets.Length, 1);
 }
Ejemplo n.º 4
0
        public IndexModule (IRepository repository, Cache cache)
        {
            repo = repository;

            Get ["/"] = parameters => 
            {
                return View ["index"];
            };

            Get ["/search/", true] = async (parameters, ct) => 
            {
                string query = (string)Request.Query.q;
                int limit = 0; 
                int start = 0; 
                string total = "";

                if(Request.Query.limit != null)
                {
                    limit = (int)Request.Query.limit;
                }

                if(Request.Query.start != null)
                {
                    start = (int)Request.Query.start;
                }

                if(Request.Query.total != null)
                {
                    total = (string)Request.Query.total;
                    if(total.ToLower() == "true")
                    {
                        long count = await repo.SearchTotal(query,true);
                        return Response.AsJson (count);
                    }
                }
                    
                Card [] cards = await repo.Search (query,start,limit,true);

                return Response.AsJson (cards);
            };


            Get ["/search/{text}", true] = async (parameters, ct) => 
            {
                int limit = 0; 
                int start = 0; 
               

                if(Request.Query.limit != null)
                {
                    limit = (int)Request.Query.limit;
                }

                if(Request.Query.start != null)
                {
                    start = (int)Request.Query.start;
                }
                    
                Card [] cards = await repo.Search ((string)parameters.text,
                    start, limit, false);

                return Response.AsJson (cards);
            };

            Get ["/cards/types", true] = async (parameters, ct) => 
            {
                string [] types  = await repo.GetCardTypes();
                return Response.AsJson (types.OrderBy(x => x));
            };

            Get ["/cards/subtypes", true] = async (parameters, ct) => 
            {
                string [] types  = await repo.GetCardSubTypes();
                return Response.AsJson (types.OrderBy(x => x));
            };
                
            Get ["/cards/rarity", true] = async (parameters, ct) => 
            {
                string [] rarity  = await repo.GetCardRarity();
                return Response.AsJson (rarity.OrderBy(x => x));
            };

            Get ["/cards/random", true] = async (parameters, ct) => 
            {
                JsonSettings.MaxJsonLength =    100000000;
                Card card =                    null;
            
                card = await repo.GetRandomCard();

                return Response.AsJson (card);
            };

            Get ["/cards", true] = async (parameters, ct) => 
            {
                JsonSettings.MaxJsonLength =    100000000;
                Card[] cards =                  null;
               
                if(Request.Query.Fields != null)
                {
                    string[] fields = ((string)Request.Query.Fields).Split(',');

                    foreach(string field in fields)
                    {
                        if(typeof(Card).GetProperty(field,BindingFlags.IgnoreCase |  
                            BindingFlags.Public | BindingFlags.Instance) == null)
                        {
                            return Response.AsJson(string.Format("Field: {0} is invalid.", field),
                                HttpStatusCode.NotAcceptable);
                        }
                    }

                    cards = await repo.GetCards (Request.Query);

                    var c = cards
                            .AsQueryable()
                            .Select(string.Format("new ({0})",
                            (string)Request.Query.Fields));

                    return Response.AsJson(c);
                }
               
                cards = await repo.GetCards (Request.Query);
                return Response.AsJson (cards);
            };

            Get ["/cards/{id}", true] = async (parameters, ct) => 
            {
                try
                {
                    int[] multiverseIds = 
                        Array.ConvertAll(((string)parameters.id).Split(','), int.Parse);

                    if(multiverseIds.Length > 1)
                    {
                        Card [] cards = await repo.GetCards(multiverseIds);

                        return Response.AsJson(cards);
                    }
                }
                catch(Exception e)
                {
                    //swallo it, cannot convert parameter to int array 
                }


                int id = 0; 
                if(int.TryParse((string)parameters.id, out id))
                {
                    Card card = await repo.GetCard ((int)parameters.id);
                    return Response.AsJson (card);
                }
                else
                {
                    Card [] cards = await repo.GetCards ((string)parameters.id);
                    return Response.AsJson (cards);
                }
            };

            Get ["/sets/{id}", true] = async (parameters, ct) =>  
            {
                string [] ids = ((string)parameters.id).Split(',');

                if(ids.Length > 1)
                {
                    CardSet[] cardSets = await repo.GetSets (ids);
                    return Response.AsJson (cardSets);
                }

                CardSet cardSet = await repo.GetSet ((string)parameters.id);
                return Response.AsJson (cardSet);
            };

            Get ["/sets/", true] = async (parameters, ct) => 
            {
                CardSet[] cardset =             await repo.GetSets ();
                JsonSettings.MaxJsonLength =    1000000;

                return Response.AsJson (cardset);
            };

            Get ["/sets/{id}/cards/random", true] = async (parameters, ct) => 
            {
                JsonSettings.MaxJsonLength =    100000000;
                Card card =                     null;

                card = await repo.GetRandomCardInSet((string)parameters.id);

                return Response.AsJson (card);
            };


            Get ["/sets/{id}/cards/{setNumber}", true] = async (parameters, ct) => 
            {
                Card cards = await repo.GetCardBySetNumber((string)parameters.id,
                    (int)parameters.setNumber);

                return Response.AsJson(cards);
            };

            Get ["/sets/{id}/cards/", true] = async (parameters, ct) => 
            {
                JsonSettings.MaxJsonLength =    100000000;
                int start =                     0; 
                int end =                       0; 

                if(Request.Query.start != null )
                {
                    int.TryParse((string)Request.Query.start, out start);
                }

                if(Request.Query.end != null )
                {
                    int.TryParse((string)Request.Query.end, out end);
                }

                Card[] cards = null;
                if(start > 0 || end > 0)
                {
                    cards = await repo.GetCardsBySet ((string)parameters.id, start, end);
                }
                else
                {
                    cards = await repo.GetCardsBySet ((string)parameters.id);
                }

                if(Request.Query.Fields != null)
                {
                    var c = cards.AsQueryable()
                        .Select(string.Format("new ({0})",(string)Request.Query.Fields));

                    return Response.AsJson(c);
                }

                return Response.AsJson (cards);
            };
        }