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