/// <summary> /// Lists the decks that belong to the user. /// </summary> /// <returns>The view.</returns> public ActionResult List() { if (!Models.AccountUtils.isLoggedIn(this)) { return(RedirectToAction("LogIn", "Account")); } var loginHash = Request.Cookies["loginHash"].Value; var accountId = Models.AccountUtils.GetAccountID(loginHash); if (accountId < 0) { return(RedirectToAction("LogOut", "Account")); } var deckDB = new Models.DecksDataContext(); var decks = from d in deckDB.Decks where d.OwnerId == accountId select d; var model = new Models.DeckListViewModel(); model.deckList = new List <Models.Deck>(); foreach (var deck in decks) { model.deckList.Add(deck); } return(View(model)); }
/// <summary> /// GET /Deck/Delete/[id] /// Deletes the specified deck from the database. /// </summary> /// <param name="id">The deck id.</param> /// <returns>A redirect to /Deck/List.</returns> public ActionResult Delete(int id) { if (!Models.AccountUtils.isLoggedIn(this)) { return(RedirectToAction("LogIn", "Account")); } var loginHash = Request.Cookies["loginHash"].Value; var accountId = Models.AccountUtils.GetAccountID(loginHash); if (accountId < 0) { return(RedirectToAction("LogOut", "Account")); } var deckDB = new Models.DecksDataContext(); var decks = from d in deckDB.Decks where (d.OwnerId == accountId) && (d.Id == id) select d; deckDB.Decks.DeleteAllOnSubmit(decks); deckDB.SubmitChanges(); return(RedirectToAction("List")); }
private string cardDbPath = "~/Content/xml/cards.xml"; // The path to the XML Card Database on the server #endregion Fields #region Methods /// <summary> /// GET /Deck/Create/[id] /// The deck editor /// </summary> /// <param name="id">The id of the deck to edit, or -1 to create a new deck.</param> /// <returns>The view.</returns> public ActionResult Create(int id = -1) { if(!Models.AccountUtils.isLoggedIn(this)) return RedirectToAction("LogIn", "Account"); CardDatabase db = new CardDatabase(Server.MapPath(cardDbPath)); var model = new Models.DeckCreateViewModel(); List<string> deckCardList = new List<string>(); if(id >= 0) { var accountDB = new Models.AccountsDataContext(); var loginHash = Request.Cookies["loginHash"].Value; var accountId = (from a in accountDB.Accounts where a.LoginHash == loginHash select a.Id).ToArray(); if(accountId.Length <= 0) return RedirectToAction("LogOut", "Account"); // Check if this account is the owner of the deck var deckDB = new Models.DecksDataContext(); var deck = (from d in deckDB.Decks where (d.Id == id) && (d.OwnerId == accountId[0]) select d).ToArray(); if(deck.Length <= 0) { // This account does not own the deck return RedirectToAction("Create", "Deck", new { id = -1 }); } model.DeckName = deck[0].Name; var cardDB = new Models.CardsDataContext(); var cards = from c in cardDB.Cards where c.DeckId == id select c.CardName; foreach(var card in cards) { deckCardList.Add(card); } } model.deckCards = new List<Core.Card>(); model.deckCards = (from c in deckCardList orderby c select db.GetCardByName(c) ).ToList(); model.sets = db.sets; model.DeckId = id; return View(model); }
/// <summary> /// GET /Deck/Statistics/[id] /// Gives a statistical analysis of a deck. /// </summary> /// <param name="id">The id of the deck to analyze.</param> /// <returns>The view on success, a redirect to /Deck/List on failure.</returns> public ActionResult Statistics(int id = -1) { if (id < 0) { return(RedirectToAction("List")); } if (!Models.AccountUtils.isLoggedIn(this)) { return(RedirectToAction("LogIn", "Account")); } var loginHash = Request.Cookies["loginHash"].Value; var accountId = Models.AccountUtils.GetAccountID(loginHash); if (accountId < 0) { return(RedirectToAction("LogOut", "Account")); } // Check if this account is the owner of the deck var deckDB = new Models.DecksDataContext(); var isOwner = (from d in deckDB.Decks where (d.Id == id) && (d.OwnerId == accountId) select true).ToArray().Length > 0 ? true : false; if (!isOwner) { return(RedirectToAction("List")); } var model = new Models.DeckStatisticsViewModel(); List <string> cardList = new List <string>(); var cardsDB = new Models.CardsDataContext(); var cardsInDeck = from c in cardsDB.Cards where c.DeckId == id select c; foreach (var card in cardsInDeck) { cardList.Add(card.CardName); } model.PopulateDeck(cardList.ToArray(), Server.MapPath(cardDbPath)); model.GenerateStatistics(); return(View(model)); }
/// <summary> /// GET /Deck/Create/[id] /// The deck editor /// </summary> /// <param name="id">The id of the deck to edit, or -1 to create a new deck.</param> /// <returns>The view.</returns> public ActionResult Create(int id = -1) { if (!Models.AccountUtils.isLoggedIn(this)) { return(RedirectToAction("LogIn", "Account")); } CardDatabase db = new CardDatabase(Server.MapPath(cardDbPath)); var model = new Models.DeckCreateViewModel(); List <string> deckCardList = new List <string>(); if (id >= 0) { var accountDB = new Models.AccountsDataContext(); var loginHash = Request.Cookies["loginHash"].Value; var accountId = (from a in accountDB.Accounts where a.LoginHash == loginHash select a.Id).ToArray(); if (accountId.Length <= 0) { return(RedirectToAction("LogOut", "Account")); } // Check if this account is the owner of the deck var deckDB = new Models.DecksDataContext(); var deck = (from d in deckDB.Decks where (d.Id == id) && (d.OwnerId == accountId[0]) select d).ToArray(); if (deck.Length <= 0) { // This account does not own the deck return(RedirectToAction("Create", "Deck", new { id = -1 })); } model.DeckName = deck[0].Name; var cardDB = new Models.CardsDataContext(); var cards = from c in cardDB.Cards where c.DeckId == id select c.CardName; foreach (var card in cards) { deckCardList.Add(card); } } model.deckCards = new List <Core.Card>(); model.deckCards = (from c in deckCardList orderby c select db.GetCardByName(c) ).ToList(); model.sets = db.sets; model.DeckId = id; return(View(model)); }
public ActionResult SaveDeck(string[] cardList, string deckName = "", int deckId = -1) { if (deckName == string.Empty) { return(RedirectToAction("Create", new { deckId = deckId })); } if (!Models.AccountUtils.isLoggedIn(this)) { return(RedirectToAction("LogIn", "Account")); } var loginHash = Request.Cookies["loginHash"].Value; var accountId = Models.AccountUtils.GetAccountID(loginHash); if (accountId < 0) { return(RedirectToAction("LogOut", "Account")); } if (deckId < 0) { // New deck var deckDB = new Models.DecksDataContext(); var newDeck = new Models.Deck { OwnerId = accountId, Name = deckName }; deckDB.Decks.InsertOnSubmit(newDeck); deckDB.SubmitChanges(); var cards = new List <Models.Card>(); Array.ForEach(cardList, c => { cards.Add(new Models.Card { DeckId = newDeck.Id, CardName = c }); }); var cardsDB = new Models.CardsDataContext(); cardsDB.Cards.InsertAllOnSubmit(cards); cardsDB.SubmitChanges(); } else { // Existing deck var deckDB = new Models.DecksDataContext(); var deck = from d in deckDB.Decks where (d.Id == deckId) && (d.OwnerId == accountId) select d; if (deck.ToArray().Length <= 0) { return(RedirectToAction("Create")); } deck.First().Name = deckName; deckDB.SubmitChanges(); var cardsDB = new Models.CardsDataContext(); // Clear the deck before inserting the cards var currentCards = from c in cardsDB.Cards where c.DeckId == deckId select c; cardsDB.Cards.DeleteAllOnSubmit(currentCards); // Add the new cards to the deck var cards = new List <Models.Card>(); Array.ForEach(cardList, c => { cards.Add(new Models.Card { DeckId = deckId, CardName = c }); }); cardsDB.Cards.InsertAllOnSubmit(cards); cardsDB.SubmitChanges(); } return(RedirectToAction("List")); }
/// <summary> /// GET /Deck/Delete/[id] /// Deletes the specified deck from the database. /// </summary> /// <param name="id">The deck id.</param> /// <returns>A redirect to /Deck/List.</returns> public ActionResult Delete(int id) { if(!Models.AccountUtils.isLoggedIn(this)) return RedirectToAction("LogIn", "Account"); var loginHash = Request.Cookies["loginHash"].Value; var accountId = Models.AccountUtils.GetAccountID(loginHash); if(accountId < 0) return RedirectToAction("LogOut", "Account"); var deckDB = new Models.DecksDataContext(); var decks = from d in deckDB.Decks where (d.OwnerId == accountId) && (d.Id == id) select d; deckDB.Decks.DeleteAllOnSubmit(decks); deckDB.SubmitChanges(); return RedirectToAction("List"); }
/// <summary> /// GET /Deck/Statistics/[id] /// Gives a statistical analysis of a deck. /// </summary> /// <param name="id">The id of the deck to analyze.</param> /// <returns>The view on success, a redirect to /Deck/List on failure.</returns> public ActionResult Statistics(int id = -1) { if(id < 0) return RedirectToAction("List"); if(!Models.AccountUtils.isLoggedIn(this)) return RedirectToAction("LogIn", "Account"); var loginHash = Request.Cookies["loginHash"].Value; var accountId = Models.AccountUtils.GetAccountID(loginHash); if(accountId < 0) return RedirectToAction("LogOut", "Account"); // Check if this account is the owner of the deck var deckDB = new Models.DecksDataContext(); var isOwner = (from d in deckDB.Decks where (d.Id == id) && (d.OwnerId == accountId) select true).ToArray().Length > 0 ? true : false; if(!isOwner) return RedirectToAction("List"); var model = new Models.DeckStatisticsViewModel(); List<string> cardList = new List<string>(); var cardsDB = new Models.CardsDataContext(); var cardsInDeck = from c in cardsDB.Cards where c.DeckId == id select c; foreach(var card in cardsInDeck) { cardList.Add(card.CardName); } model.PopulateDeck(cardList.ToArray(), Server.MapPath(cardDbPath)); model.GenerateStatistics(); return View(model); }
public ActionResult SaveDeck(string[] cardList, string deckName = "", int deckId = -1) { if(deckName == string.Empty) { return RedirectToAction("Create", new { deckId = deckId }); } if(!Models.AccountUtils.isLoggedIn(this)) { return RedirectToAction("LogIn", "Account"); } var loginHash = Request.Cookies["loginHash"].Value; var accountId = Models.AccountUtils.GetAccountID(loginHash); if(accountId < 0) return RedirectToAction("LogOut", "Account"); if(deckId < 0) { // New deck var deckDB = new Models.DecksDataContext(); var newDeck = new Models.Deck { OwnerId = accountId, Name = deckName }; deckDB.Decks.InsertOnSubmit(newDeck); deckDB.SubmitChanges(); var cards = new List<Models.Card>(); Array.ForEach(cardList, c => { cards.Add(new Models.Card { DeckId = newDeck.Id, CardName = c }); }); var cardsDB = new Models.CardsDataContext(); cardsDB.Cards.InsertAllOnSubmit(cards); cardsDB.SubmitChanges(); } else { // Existing deck var deckDB = new Models.DecksDataContext(); var deck = from d in deckDB.Decks where (d.Id == deckId) && (d.OwnerId == accountId) select d; if(deck.ToArray().Length <= 0) return RedirectToAction("Create"); deck.First().Name = deckName; deckDB.SubmitChanges(); var cardsDB = new Models.CardsDataContext(); // Clear the deck before inserting the cards var currentCards = from c in cardsDB.Cards where c.DeckId == deckId select c; cardsDB.Cards.DeleteAllOnSubmit(currentCards); // Add the new cards to the deck var cards = new List<Models.Card>(); Array.ForEach(cardList, c => { cards.Add(new Models.Card { DeckId = deckId, CardName = c }); }); cardsDB.Cards.InsertAllOnSubmit(cards); cardsDB.SubmitChanges(); } return RedirectToAction("List"); }
/// <summary> /// Lists the decks that belong to the user. /// </summary> /// <returns>The view.</returns> public ActionResult List() { if(!Models.AccountUtils.isLoggedIn(this)) return RedirectToAction("LogIn", "Account"); var loginHash = Request.Cookies["loginHash"].Value; var accountId = Models.AccountUtils.GetAccountID(loginHash); if(accountId < 0) return RedirectToAction("LogOut", "Account"); var deckDB = new Models.DecksDataContext(); var decks = from d in deckDB.Decks where d.OwnerId == accountId select d; var model = new Models.DeckListViewModel(); model.deckList = new List<Models.Deck>(); foreach(var deck in decks) { model.deckList.Add(deck); } return View(model); }