Exemple #1
0
        public IUserIdentity GetUserFromIdentifier(Guid identifier, NancyContext context)
        {
            try
            {
                User ssaUser = ssa.Validate(identifier,
                                            context.Request.UserHostAddress);

                Planeswalker user = new Planeswalker
                {
                    UserName  = ssaUser.UserName,
                    AuthToken = ssaUser.AuthToken,
                    Email     = ssaUser.Email,
                    Id        = ssaUser.Id,
                    Claims    = ssaUser.Claims,
                    Roles     = ssaUser.Roles,
                    Profile   = repository.GetProfile(ssaUser.Id)
                };

                return(user);
            }
            catch (Exception e)
            {
                return(null);
            }
        }
Exemple #2
0
        public void Init()
        {
            ssa =  new SuperSimpleAuth ("testing_mtgdb.info", 
                "ae132e62-570f-4ffb-87cc-b9c087b09dfb");

            mtgdb = new Db("http://127.0.0.1:8082");

            repository =  new MongoRepository ("mongodb://localhost", mtgdb, ssa);
            admin = new Admin("http://127.0.0.1:8082");

            //Super simple auth can't delete from here. 
            try
            {
                repository.AddPlaneswalker ("mtgdb_tester", 
                    "test123", "*****@*****.**");
            }
            catch(Exception e)
            {
                System.Console.WriteLine (e.Message);
            }

            User ssaUser = ssa.Authenticate ("mtgdb_tester", 
                "test123", "127.0.0.1");

            mtgdbUser = new Planeswalker 
            {
                UserName = ssaUser.UserName,
                AuthToken = ssaUser.AuthToken,
                Email = ssaUser.Email,
                Id = ssaUser.Id,
                Claims = ssaUser.Claims,
                Roles = ssaUser.Roles,
                Profile = repository.GetProfile (ssaUser.Id)
            };
        }
        public IUserIdentity GetUserFromIdentifier(Guid identifier, NancyContext context)
        {
            try
            {
                User ssaUser = ssa.Validate (identifier,
                    context.Request.UserHostAddress);

                Planeswalker user = new Planeswalker 
                {
                    UserName = ssaUser.UserName,
                    AuthToken = ssaUser.AuthToken,
                    Email = ssaUser.Email,
                    Id = ssaUser.Id,
                    Claims = ssaUser.Claims,
                    Roles = ssaUser.Roles,
                    Profile = repository.GetProfile(ssaUser.Id)
                };

                return user;
            }
            catch(Exception e)
            {
                return null;
            }
                
            return null;
        }
Exemple #4
0
        public bool IsOwner(Planeswalker pw)
        {
            if (this.UserId == pw.Id)
            {
                return(true);
            }

            return(false);
        }
Exemple #5
0
        public Planeswalker UpdatePlaneswalker(Planeswalker planeswalker)
        {
            MongoCollection <Profile> walkers = database.GetCollection <Profile> ("profiles");
            var query = Query <Profile> .EQ(e => e.Id, planeswalker.Id);

            Profile      updateWalker = walkers.FindOne(query);
            Planeswalker user         = null;

            if (updateWalker != null)
            {
                if (updateWalker.Email.ToLower() != planeswalker.Profile.Email.ToLower())
                {
                    ssa.ChangeEmail(planeswalker.AuthToken, planeswalker.Profile.Email);
                }

                if (updateWalker.Name.ToLower() != planeswalker.Profile.Name.ToLower())
                {
                    ssa.ChangeUserName(planeswalker.AuthToken, planeswalker.Profile.Name);
                }

                updateWalker.Email      = planeswalker.Profile.Email;
                updateWalker.ModifiedAt = DateTime.Now;
                updateWalker.Name       = planeswalker.Profile.Name;

                walkers.Save(updateWalker);

                User ssaUser = ssa.Validate(planeswalker.AuthToken);

                user = new Planeswalker
                {
                    UserName  = ssaUser.UserName,
                    AuthToken = ssaUser.AuthToken,
                    Email     = ssaUser.Email,
                    Id        = ssaUser.Id,
                    Claims    = ssaUser.Claims,
                    Roles     = ssaUser.Roles,
                    Profile   = GetProfile(ssaUser.Id)
                };
            }

            return(user);
        }
Exemple #6
0
        public DeckModule()
        {
            this.RequiresAuthentication();

            //viewing public decks of a planeswalker
            Get["/mydecks"] = parameters => {
                DecksModel model = new DecksModel();
                model.ActiveMenu   = "mydecks";
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                model.Decks        = deckbuilder.GetUserDecks(model.Planeswalker.Id).ToList();
                model.Title        = "My Decks";

                return(View["Deck/MyDecks", model]);
            };

            Post["/decks/delete/{id}"] = parameters => {
                Planeswalker planeswalker = (Planeswalker)this.Context.CurrentUser;
                Guid         id           = Guid.Empty;
                Deck         deck         = new Deck();
                if (Guid.TryParse((string)parameters.id, out id))
                {
                    deck = deckbuilder.GetDeck(id);

                    if (deck != null && deck.IsOwner(planeswalker))
                    {
                        deckbuilder.DeleteDeck(deck.Id);
                    }
                }

                return(Response.AsRedirect("/mydecks"));
            };


//            //for deck link sharing and viewing single deck
//            Get["/pw/{planeswalker}/decks/{name}"] = parameters => {
//                DeckModel model =       new DeckModel();
//                model.ActiveMenu =      "mydecks";
//                model.Planeswalker =    (Planeswalker)this.Context.CurrentUser;
//                model.Deck =
//                    deckbuilder.GetDeck(model.Planeswalker.Id,
//                        (string)parameters.name);
//
//                return View["Deck/Deck", model];
//            };
//
//            Get["/decks"] = parameters => {
//                Planeswalker planeswalker = (Planeswalker)this.Context.CurrentUser;
//                Deck [] decks = deckbuilder.GetUserDecks(planeswalker.Id);
//
//                return View["Deck/Decks", decks.ToArray()];
//            };
//
//            //create a deck SPA
//            Post["/decks"] = parameters => {
//                Planeswalker planeswalker =
//                    (Planeswalker)this.Context.CurrentUser;
//
//                int [] cards = null;
//                int [] sideBar = null;
//
//                try
//                {
//                    cards = ((string)Request.Form.Cards).Split(',')
//                        .Select(n => Convert.ToInt32(n))
//                        .ToArray();
//
//                    sideBar = ((string)Request.Form.SideBar).Split(',')
//                        .Select(n => Convert.ToInt32(n))
//                        .ToArray();
//
//                }
//                catch(Exception e)
//                {
//                    throw e;
//                }
//
//                string name = (string)this.Request.Form.Name;
//                string description = (string)this.Request.Form.Description;
//
//                Deck deck = new Deck();
//                deck.Name = name;
//                deck.Description = description;
//                deck.SetCards(cards);
//                deck.SetSideBar(sideBar);
//                deck.UserId = planeswalker.Id;
//
//                deck = deckbuilder.AddDeck(deck);
//
//                return Response.AsJson(deck);
//            };

//            //update a deck SPA
//            Post["/decks/{name}"] = parameters => {
//                Planeswalker planeswalker =     (Planeswalker)this.Context.CurrentUser;
//                string deckName =               (string)parameters.name;
//
//                int [] cards = null;
//                int [] sideBar = null;
//
//                try
//                {
//                    cards = ((string)Request.Form.Cards).Split(',')
//                        .Select(n => Convert.ToInt32(n))
//                        .ToArray();
//
//                    sideBar = ((string)Request.Form.SideBar).Split(',')
//                        .Select(n => Convert.ToInt32(n))
//                        .ToArray();
//
//                }
//                catch(Exception e)
//                {
//                    throw e;
//                }
//
//                string name = (string)this.Request.Form.Name;
//                string description = (string)this.Request.Form.Description;
//
//                Deck deck = deckbuilder.GetDeck(planeswalker.Id, deckName);
//                deck.Name = name;
//                deck.Description = description;
//                deck.SetCards(cards);
//                deck.SetSideBar(sideBar);
//                deck.UserId = planeswalker.Id;
//
//                deck = deckbuilder.UpdateDeck(deck);
//
//                return Response.AsJson(deck);
//            };
//
//            //Delete a deck SPA
//            Post["/decks/delete/{name}"] = parameters => {
//                Planeswalker planeswalker =     (Planeswalker)this.Context.CurrentUser;
//                Deck deck = deckbuilder.GetDeck(planeswalker.Id,
//                    (string)parameters.name);
//
//                try
//                {
//                    deckbuilder.DeleteDeck(deck.Id);
//                }
//                catch(Exception e)
//                {
//                    return Response.AsJson(e.Message,
//                        HttpStatusCode.NotAcceptable);
//                }
//
//                return Response.AsJson("true", HttpStatusCode.Accepted);
//            };
//
//            //Get a deck SPA
//            Get["/decks/{name}"] = parameters => {
//                Planeswalker planeswalker =     (Planeswalker)this.Context.CurrentUser;
//                Deck deck =   deckbuilder.GetDeck(planeswalker.Id,
//                    (string)parameters.name);
//
//                return Response.AsJson(deck);
//            };
//
//            //Get cards for a deck SPA
//            Get["/decks/{name}/cards"] = parameters => {
//                Planeswalker planeswalker =     (Planeswalker)this.Context.CurrentUser;
//                Deck deck =   deckbuilder.GetDeck(planeswalker.Id,
//                    (string)parameters.name);
//
//                return Response.AsJson(deck.GetCards());
//            };
//
//            //Get cards for a deck SPA
//            Get["/decks/{name}/sidebar"] = parameters => {
//                Planeswalker planeswalker =     (Planeswalker)this.Context.CurrentUser;
//                Deck deck =  deckbuilder.GetDeck(planeswalker.Id,
//                    (string)parameters.name);
//
//                return Response.AsJson(deck.GetSideBarCards());
//            };

            //gets the latest version of card at the time of import
            Post["/decks/import"] = parameters => {
                Planeswalker planeswalker = (Planeswalker)this.Context.CurrentUser;
                string       deckName     = (string)this.Request.Form.Name;
                string       description  = (string)this.Request.Form.Description;
                var          file         = this.Request.Files.FirstOrDefault();
                Deck         deck         = null;

                if (file != null)
                {
                    deck             = MtgFile.ImportDec(file.Value);
                    deck.Name        = deckName;
                    deck.Description = description;
                    deck.UserId      = planeswalker.Id;
                }

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

            //testing method
            Post["/col/import"] = parameters => {
                Planeswalker planeswalker = (Planeswalker)this.Context.CurrentUser;
                var          file         = this.Request.Files.FirstOrDefault();

                MtgDb.Info.MtgFile.Item[] items = null;

                if (file != null)
                {
                    items = MtgFile.ImportColl2(file.Value);
                }

                return(Response.AsJson(items));
            };
        }
Exemple #7
0
        public void Update_Planeswalker ()
        {
            mtgdbUser.Profile.Email = "*****@*****.**";
            mtgdbUser = repository.UpdatePlaneswalker(mtgdbUser);
            Assert.AreEqual (mtgdbUser.Email, "*****@*****.**");

            mtgdbUser.Profile.Email = "*****@*****.**";
            mtgdbUser = repository.UpdatePlaneswalker(mtgdbUser);
            Assert.AreEqual (mtgdbUser.Email, "*****@*****.**");

        }
        public Planeswalker UpdatePlaneswalker(Planeswalker planeswalker)
        {
            MongoCollection<Profile> walkers = database.GetCollection<Profile> ("profiles");
            var query = Query<Profile>.EQ (e => e.Id, planeswalker.Id);

            Profile updateWalker = walkers.FindOne(query);
            Planeswalker user = null;

            if (updateWalker != null) 
            {
                if(updateWalker.Email.ToLower() != planeswalker.Profile.Email.ToLower())
                {
                    ssa.ChangeEmail (planeswalker.AuthToken, planeswalker.Profile.Email);
                }

                if(updateWalker.Name.ToLower() != planeswalker.Profile.Name.ToLower())
                {
                    ssa.ChangeUserName (planeswalker.AuthToken, planeswalker.Profile.Name);
                }

                updateWalker.Email = planeswalker.Profile.Email;
                updateWalker.ModifiedAt = DateTime.Now;
                updateWalker.Name = planeswalker.Profile.Name;

                walkers.Save(updateWalker);

                User ssaUser = ssa.Validate (planeswalker.AuthToken);

                user = new Planeswalker 
                {
                    UserName = ssaUser.UserName,
                    AuthToken = ssaUser.AuthToken,
                    Email = ssaUser.Email,
                    Id = ssaUser.Id,
                    Claims = ssaUser.Claims,
                    Roles = ssaUser.Roles,
                    Profile = GetProfile(ssaUser.Id)
                };
            }
           
            return user;
        }
        public LogonModule(SuperSimpleAuth ssa)
        {
            this.ssa = ssa;

            Get["/settings"] = parameters => {
                SettingsModel model = new SettingsModel();

                if (this.Context.CurrentUser == null)
                {
                    return(this.LogoutAndRedirect("/"));
                }

                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                return(View["Logon/Settings", model]);
            };

            Post["/settings"] = parameters => {
                SettingsModel model = this.Bind <SettingsModel>();

                if (this.Context.CurrentUser == null)
                {
                    return(this.LogoutAndRedirect("/"));
                }

                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;


                if (Request.Form.Save != null)
                {
                    model.Planeswalker.Profile.Email = model.Email;
                    model.Planeswalker.Profile.Name  = model.Name;

                    try
                    {
                        model.Planeswalker = repository.UpdatePlaneswalker(model.Planeswalker);
                    }
                    catch (Exception e)
                    {
                        model.Errors.Add("Could not update account");
                        model.Errors.Add(e.Message);
                    }
                }

                if (Request.Form.Delete != null)
                {
                    try
                    {
                        if (model.Yes)
                        {
                            ssa.Disable(model.Planeswalker.AuthToken);
                            repository.RemovePlaneswalker(model.Planeswalker.Id);
                            return(this.LogoutAndRedirect("/"));
                        }
                        else
                        {
                            model.Errors.Add("You must check, 'Yes, I know'. To delete.");
                        }
                    }
                    catch (Exception e)
                    {
                        model.Errors.Add("Account could not be deleted");
                        model.Errors.Add(e.Message);
                    }
                }

                if (Request.Form.ChangePassword != null)
                {
                    if (model.Password != null && model.ConfirmPassword != null)
                    {
                        if (model.Password == model.ConfirmPassword)
                        {
                            try
                            {
                                ssa.ChangePassword(model.Planeswalker.AuthToken, model.Password);
                                model.Messages.Add("Password successfully changed.");
                            }
                            catch (Exception e)
                            {
                                model.Errors.Add("Password cannot be changed.");
                                model.Errors.Add(e.Message);
                            }
                        }
                        else
                        {
                            model.Errors.Add("Password and Confirmation Password do not match.");
                        }
                    }
                    else
                    {
                        model.Errors.Add("Password and Confirmation Password must not be blank.");
                    }
                }

                return(View["Logon/Settings", model]);
            };

            Get["/logon"] = parameters => {
                LogonModel model = new LogonModel();
                model.ActiveMenu  = "signin";
                model.UrlRedirect = (string)Request.Query.Url;

                if (Request.Query.returnUrl != null)
                {
                    model.UrlRedirect = (string)Request.Query.returnUrl;
                }

                return(View["Logon/logon", model]);
            };

            Post["/logon"] = parameters => {
                LogonModel model = this.Bind <LogonModel>();
                model.ActiveMenu = "signin";
                var results = this.Validate(model);

                if (!results.IsValid)
                {
                    model.Errors = ErrorUtility.GetValidationErrors(results);
                    return(View["Logon/Logon", model]);
                }

                model.Errors.Add("Password or/and Username is incorrect.");

                User user = null;

                try
                {
                    user = ssa.Authenticate(model.UserName, model.Secret,
                                            this.Context.Request.UserHostAddress);
                }
                catch (Exception e)
                {
                    model.Errors.Add(e.Message);

                    if (user == null)
                    {
                        return(View["Logon/logon", model]);
                    }
                }

                return(this.LoginAndRedirect(user.AuthToken,
                                             fallbackRedirectUrl: model.UrlRedirect));
            };

            Get ["/register"] = parameters => {
                SignupModel model = new SignupModel();
                model.ActiveMenu = "register";
                return(View["register", model]);
            };

            Post ["/register"] = parameters => {
                SignupModel model  = this.Bind <SignupModel>();
                var         result = this.Validate(model);
                model.ActiveMenu = "register";

                if (!result.IsValid)
                {
                    model.Errors.AddRange(ErrorUtility.GetValidationErrors(result));
                    return(View["Register", model]);
                }

                try
                {
                    repository.AddPlaneswalker(model.UserName, model.Secret, model.Email);
                }
                catch (Exception e)
                {
                    model.Errors.Add(e.Message);
                    return(View["Register", model]);
                }

                LogonModel logon = new LogonModel();
                logon.Messages.Add("You have successfully created an account. Please Sign In.");

                try
                {
                    Email.Send("*****@*****.**",
                               "New Planeswalker alert", model.UserName);
                }
                catch (Exception e)
                {
                    //return null;
                }

                return(View["Logon", logon]);
            };

            Get["/logout"] = parameters => {
                Planeswalker nuser = (Planeswalker)Context.CurrentUser;
                ssa.End(nuser.AuthToken);

                return(this.LogoutAndRedirect((string)Request.Query.Url));
            };

            Get ["/forgot"] = parameters => {
                ForgotModel model = new ForgotModel();
                model.ActiveMenu = "signin";
                return(View["Forgot", model]);
            };

            Post ["/forgot"] = parameters => {
                ForgotModel model = this.Bind <ForgotModel>();
                model.ActiveMenu = "signin";

                string subject = "MtgDb.info: Password reset request.";
                string body    = "You have requested a password reset. You new password is: {0}";

                try
                {
                    string newPass = ssa.Forgot(model.Email);
                    Email.Send(model.Email, subject, string.Format(body, newPass));
                    model.Messages.Add("Your new password has been successfully sent to your email.");
                }
                catch (Exception e)
                {
                    model.Errors.Add(e.Message);
                }

                return(View["Forgot", model]);
            };
        }
Exemple #10
0
        public OpenDbModule()
        {
            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();
                    model.NewCards   = repository.GetNewCards().ToList();
                    model.NewSets    = repository.GetNewSets().ToList();
                    model.SetChanges = repository.GetSetChangeRequests().ToList();
                }
                else
                {
                    model.Changes    = repository.GetChangeRequests(status).ToList();
                    model.NewCards   = repository.GetNewCards(status).ToList();
                    model.NewSets    = repository.GetNewSets(status).ToList();
                    model.SetChanges = repository.GetSetChangeRequests(status).ToList();
                }

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

            Get["/sets/new", true] = async(parameters, ct) => {
                NewSet model = new NewSet();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                return(View["Change/NewSet", model]);
            };

            Post["/sets/new", true] = async(parameters, ct) => {
                NewSet model = this.Bind <NewSet>();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                model.UserId       = model.Planeswalker.Id;

                var result = this.Validate(model);

                if (!result.IsValid)
                {
                    model.Errors = ErrorUtility.GetValidationErrors(result);
                    return(View["Change/NewSet", model]);
                }

                repository.AddSet(model);

                return(Response.AsRedirect(string.Format("/sets/new/{0}",
                                                         model.Id)));
            };

            Get["/sets/new/{id}", true] = async(parameters, ct) => {
                NewSet model = repository.GetSet((Guid)parameters.id);

                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                return(View["Change/NewSetRead", model]);
            };

            Post["/sets/new/{id}", true] = async(parameters, ct) => {
                NewSet model = repository.GetSet((Guid)parameters.id);
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                Admin admin = new Admin(ConfigurationManager.AppSettings.Get("api"));

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

                MtgDbAdminDriver.CardSet newSet =
                    new MtgDbAdminDriver.CardSet()
                {
                    Id          = model.SetId,
                    Name        = model.Name,
                    Description = model.Description,
                    ReleasedAt  = model.ReleasedAt,
                    Type        = model.Type,
                    Block       = model.Block,
                    Common      = model.Common,
                    Uncommon    = model.Uncommon,
                    Rare        = model.Rare,
                    MythicRare  = model.MythicRare,
                    BasicLand   = model.BasicLand
                };

                admin.AddSet(model.Planeswalker.AuthToken, newSet);
                model = repository.UpdateNewSetStatus(model.Id, "Accepted");

                model.Messages.Add("Set has been sucessfully added.");

                return(View["Change/NewSetRead", model]);
            };

            Get["/cards/new/{id}", true] = async(parameters, ct) => {
                NewCard model = repository.GetCard((Guid)parameters.id);
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                return(View["Change/NewCardsRead", model]);
            };

            Post["/cards/new/{id}", true] = async(parameters, ct) => {
                NewCard model = repository.GetCard((Guid)parameters.id);
                Admin   admin = new Admin(ConfigurationManager.AppSettings.Get("api"));
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

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


                string [] colors = null;

                if (model.Colors == null ||
                    model.Colors.Length == 0)
                {
                    colors    = new string[1];
                    colors[0] = "None";
                }
                else
                {
                    colors = model.Colors.Split(',');
                }

                MtgDbAdminDriver.Card newCard =
                    new MtgDbAdminDriver.Card()
                {
                    Id                = model.Mvid,
                    Name              = model.Name,
                    Description       = model.Description,
                    Flavor            = model.Flavor,
                    Power             = model.Power,
                    Toughness         = model.Toughness,
                    Loyalty           = model.Loyalty,
                    Artist            = model.Artist,
                    Type              = model.Type,
                    SubType           = model.SubType,
                    Token             = model.Token,
                    Colors            = colors,
                    CardSetId         = model.CardSetId,
                    ManaCost          = model.ManaCost,
                    ConvertedManaCost = model.ConvertedManaCost,
                    RelatedCardId     = model.RelatedCardId,
                    Rarity            = model.Rarity,
                    SetNumber         = model.SetNumber
                };

                try
                {
                    admin.AddCard(model.Planeswalker.AuthToken, newCard);
                    repository.UpdateNewCardStatus(model.Id, "Accepted");
                    model = repository.GetCard((Guid)parameters.id);
                    model.Messages.Add("Card has been sucessfully added.");
                }
                catch (Exception e)
                {
                    model.Errors.Add(e.Message);
                }

                return(View["Change/NewCardsRead", model]);
            };

            Get["/cards/new", true] = async(parameters, ct) => {
                NewCard model = new NewCard();

                if (Request.Query.SetId != null)
                {
                    model.CardSetId = (string)Request.Query.SetId;
                }

                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                model.Types        = magicdb.GetCardTypes();
                model.SubTypes     = magicdb.GetCardSubTypes();
                model.Rarities     = magicdb.GetCardRarityTypes();

                return(View["Change/NewCard", model]);
            };

            Post["/cards/new", true] = async(parameters, ct) => {
                NewCard model = this.Bind <NewCard>();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                model.UserId       = model.Planeswalker.Id;

                model.Types    = magicdb.GetCardTypes();
                model.SubTypes = magicdb.GetCardSubTypes();
                model.Rarities = magicdb.GetCardRarityTypes();

                var result = this.Validate(model);

                if (!result.IsValid)
                {
                    model.Errors = ErrorUtility.GetValidationErrors(result);
                    return(View["Change/NewCard", model]);
                }

                Guid id = repository.AddCard(model);
                model = repository.GetCard(id);

                return(Response.AsRedirect(string.Format("/cards/new/{0}",
                                                         model.Id)));
            };


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

                if (Request.Query.v != null)
                {
                    int version = 0;
                    if (int.TryParse((string)Request.Query.v, out version))
                    {
                        model.NewVersion = version;
                    }
                }

                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();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                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)));
            };

            Post["/setchange/{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;
                SetChange    change       = null;

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

                try
                {
                    change = repository.GetCardSetChangeRequest(changeId);

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

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

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

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

                    string value = change.GetFieldValue(field);
                    admin.UpdateCardSetField(planeswalker.AuthToken,
                                             change.SetId, field, (string)value);

                    repository.UpdateCardSetChangeStatus(change.Id, "Accepted", field);
                }
                catch (Exception e)
                {
                    throw e;
                }

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

            Get ["/cards/{id}/change"] = parameters => {
                CardChange model = new CardChange();
                Card       card;

                try
                {
                    card  = magicdb.GetCard((int)parameters.id);
                    model = CardChange.MapCard(card);
                    model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                }
                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];
            };

            Get ["/sets/{id}/change"] = parameters => {
                SetChange model = new SetChange();
                CardSet   set;

                try
                {
                    set   = magicdb.GetSet((string)parameters.id);
                    model = SetChange.MapSet(set);
                    model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                }
                catch (Exception e)
                {
                    throw e;
                }

                return(View["Change/CardSet", model]);
            };

            Post ["/sets/{id}/change"] = parameters => {
                SetChange model   = this.Bind <SetChange>();
                CardSet   current = magicdb.GetSet((string)parameters.id);
                model.SetId = current.Id;

                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                model.UserId       = model.Planeswalker.Id;
                SetChange set = null;

                var result = this.Validate(model);

                if (!result.IsValid)
                {
                    model.Errors = ErrorUtility.GetValidationErrors(result);
                    return(View["Change/CardSet", model]);
                }

                try
                {
                    set = repository.GetCardSetChangeRequest(
                        repository.AddCardSetChangeRequest(model)
                        );
                }
                catch (Exception e)
                {
                    model.Errors.Add(e.Message);
                    return(View["Change/CardSet", model]);
                }

                return(Response.AsRedirect(string.Format("/sets/{0}/logs?v={1}",
                                                         set.SetId, set.Version)));

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

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

                if (Request.Query.v != null)
                {
                    int version = 0;
                    if (int.TryParse((string)Request.Query.v, out version))
                    {
                        model.NewVersion = version;
                    }
                }

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

                return(View["Change/SetLogs", model]);
            };

            Get["/sets/{id}/logs/{logid}"] = parameters => {
                SetChange model = new SetChange();
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;

                try
                {
                    model = repository.GetCardSetChangeRequest((Guid)parameters.logid);
                }
                catch (Exception e)
                {
                    model.Errors.Add(e.Message);
                }

                model.ActiveMenu   = "sets";
                model.Planeswalker = (Planeswalker)this.Context.CurrentUser;
                model.SetId        = (string)parameters.id;


                return(View["Change/SetChange", model]);
            };
        }