public ActionResult Create([Bind(Include = "idperson,firstname,lastname,gamertag,currencybalance,isactive,email,password,salt,userrole")] tblperson tblperson) { if (ModelState.IsValid) { db.tblperson.Add(tblperson); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(tblperson)); }
public static bool Register(Person regUser) { try { using (var db = new ClonestoneFSEntities()) { if (db.AllPersons.Any(n => n.Email == regUser.Email)) { throw new Exception("UserAlreadyExists"); } //Salt erzeugen string salt = Helper.GenerateSalt(); //Passwort Hashen string hashedAndSaltedPassword = Helper.GenerateHash(regUser.Password + salt); regUser.Password = hashedAndSaltedPassword; regUser.Salt = salt; db.AllPersons.Add(regUser); db.SaveChanges(); } } catch (Exception e) { Writer.LogError(e); } return(true); }
/// <summary> /// Drop current Deck based on DeckID to save it anew, used for the Deckbuilder /// </summary> /// <param name="deckId"></param> public static void DropDeck(int deckId) { using (var db = new ClonestoneFSEntities()) { db.pClearDeckByID(deckId); db.SaveChanges(); } }
public ActionResult DeleteConfirmed(int id) { ClonestoneFSEntities db = new ClonestoneFSEntities(); Person p = db.AllPersons.Find(id); db.AllPersons.Remove(p); db.SaveChanges(); return(RedirectToAction("Index")); }
/// <summary> /// /// </summary> /// <param name="id"></param> /// <param name="deckCards"></param> /// <returns></returns> public static bool UpdateDeckById(int id, List <DeckCard> deckCards) { Deck deck = null; try { using (var db = new ClonestoneFSEntities()) { deck = db.AllDecks.Find(id); if (deck == null) { throw new Exception("DeckNotFound"); } var existingDeckList = deck.AllDeckcards.ToList(); foreach (var dc in existingDeckList) { deck.AllDeckcards.Remove(dc); } db.SaveChanges(); foreach (var dc in deckCards) { var dbDeckCard = new DeckCard(); dbDeckCard.NumCards = dc.NumCards; dbDeckCard.Deck = db.AllDecks.Find(id); dbDeckCard.Card = db.AllCards.Find(dc.Card.ID); db.AllDeckcards.Add(dbDeckCard); } db.SaveChanges(); return(true); } } catch (Exception e) { Writer.LogError(e); return(false); } }
/// <summary> /// /// </summary> /// <param name="email"></param> /// <param name="cards"></param> /// <returns></returns> public static bool AddPersonCardsByEmail(string email, List <Card> cards) { Person person = null; try { using (var db = new ClonestoneFSEntities()) { person = db.AllPersons.Where(u => u.Email == email).FirstOrDefault(); if (person == null) { throw new Exception("UserDoesNotExist"); } /// gehe alle Karten durch die dieser person hinzugefügt werden sollen foreach (var card in cards) { /// ermittle ob die Person diese Karte bereits hat var personCard = person.AllPersonCards.Where(x => x.ID_Card == card.ID).FirstOrDefault(); /// die person hat diese Karte noch NICHT if (personCard == null) { personCard = new PersonCard(); personCard.Person = person; personCard.NumberOfCards = 1; person.AllPersonCards.Add(personCard); } else /// die person hat diese karte schon einmal { personCard.NumberOfCards += 1; /// erhöhe die anzahl dieser karte um eins } } db.SaveChanges(); return(true); } } catch (Exception e) { Debugger.Break(); Writer.LogError(e); return(false); } }
public ActionResult EditPost(Register ro) { ClonestoneFSEntities db = new ClonestoneFSEntities(); Person actPerson = (from p in db.AllPersons where p.Email == User.Identity.Name select p).FirstOrDefault(); actPerson.Firstname = ro.Firstname; actPerson.Lastname = ro.Lastname; actPerson.Gamertag = ro.Gamertag; actPerson.Password = ro.Password; actPerson.Anschrift = ro.Adresse; actPerson.Hausnummer = ro.Hausnummer; actPerson.Ort = ro.Ort; actPerson.PLZ = ro.PLZ; db.SaveChanges(); return(RedirectToAction("Details", "Edit")); }
/// <summary> /// Save current Deck to Database, used for the Deckbuilder /// </summary> /// <param name="deckcardId"></param> /// <param name="deckId"></param> public static void SaveDeckToDatabase(int deckcardId, int deckId) { tblcollection coll = new tblcollection(); tbldeck deck = new tbldeck(); using (var db = new ClonestoneFSEntities()) { var cocaItem = (from c in db.tblcollection where c.idcollectioncard == deckcardId select c).FirstOrDefault(); var deckItem = (from d in db.tbldeck where d.iddeck == deckId select d).FirstOrDefault(); deckItem.tblcollection.Add(cocaItem); db.SaveChanges(); } }
/// <summary> /// /// </summary> /// <param name="email"></param> /// <param name="balanceNew"></param> /// <returns></returns> public static bool BalanceUpdateByEmail(string email, int balanceNew) { var dbUser = GetPersonByEmail(email); dbUser.Currencybalance = balanceNew; try { using (var db = new ClonestoneFSEntities()) { db.Entry(dbUser).State = EntityState.Modified; db.SaveChanges(); return(true); } } catch (Exception e) { Writer.LogError(e); return(false); } }
/// <summary> /// /// </summary> /// <param name="id"></param> /// <param name="name"></param> /// <returns></returns> public static bool AddDeckByUserId(int id, string name) { Person user = null; try { using (var db = new ClonestoneFSEntities()) { user = db.AllPersons.Find(id); Deck deck = new Deck(); deck.Name = name; deck.Person = user; db.AllDecks.Add(deck); db.SaveChanges(); return(true); } } catch (Exception e) { Writer.LogError(e); return(false); } }
public static BuyResult BuyPack(int id, string email) { BuyResult result = BuyResult.Success; if (id <= 0) { throw new ArgumentException("Invalid Value", nameof(id)); } if (string.IsNullOrEmpty(email)) { throw new ArgumentNullException(nameof(email)); } try { using (var context = new ClonestoneFSEntities()) { Person user = context.AllPersons.FirstOrDefault(x => x.Email == email); if (user == null) { throw new ArgumentException("Invalid value", nameof(email)); } Pack cardPack = context.AllPacks.FirstOrDefault(x => x.ID == id); if (cardPack == null) { throw new ArgumentException("Invalid value", nameof(id)); } if (cardPack.IsMoney == false) { if (user.Currencybalance < cardPack.Packprice) { result = BuyResult.NotEnoughMoney; } else { user.Currencybalance -= (int)cardPack.Packprice; Order purchase = new Order() { ID_Pack = id, ID_Person = user.ID, NumberOfPacks = 1, Orderdate = DateTime.Now }; context.AllOrders.Add(purchase); int count = context.AllCards.Count(); //List<Card> userCards = new List<Card>(); /// create cards at random for (int numberOfCard = 0; numberOfCard < cardPack.Cardquantity; numberOfCard++) { /// get a valid idCard (generated by random) Card randomCard = context.AllCards.OrderBy(x => x.ID).Skip(RandomNumberGenerator.Next(0, count)).Take(1).Single(); /// save new card to userCards /// if card is already an userCard /// increase number PersonCard personCard = user.AllPersonCards.Where(x => x.ID == randomCard.ID).FirstOrDefault(); if (personCard != null) { personCard.NumberOfCards++; } else /// else - add new userCard { personCard = new PersonCard() { ID_Person = user.ID, ID_Card = randomCard.ID, NumberOfCards = 1 }; context.AllPersonCards.Add(personCard); } } context.SaveChanges(); } } else if (cardPack.IsMoney == true) { Order purchase = new Order() { ID_Pack = id, ID_Person = user.ID, NumberOfPacks = 1, Orderdate = DateTime.Now }; context.AllOrders.Add(purchase); int currentBalance = UserManager.GetCurrencyBalanceByEmail(user.Email); int sumBalance = ((int)(currentBalance + GetCardPackById(id).DiamondValue)) * (int)purchase.NumberOfPacks; bool isUpdated = UserManager.BalanceUpdateByEmail(user.Email, sumBalance); context.SaveChanges(); } } } catch (Exception ex) { throw ex; } return(result); }
/// <summary> /// Used to execute and confirm orders (card- and goldorders) /// Cardorder: checks if user has enough currency then generates the amount of cards based on cardquantity /// Goldorder: creditcard payment, creditcardnumber get checkt via luhn algorithm and data will be send via HTTPS to the Credit Card company /// </summary> /// <param name="personID"></param> /// <param name="packID"></param> /// <param name="creditCardNumber"></param> public static void ExecuteOrder(int personID, int packID, string creditCardNumber) { using (var db = new ClonestoneFSEntities()) { tblorder order = new tblorder(); tblcollection col = new tblcollection(); Random r = new Random(); order.fkpack = packID; order.fkperson = personID; order.orderdate = DateTime.Now; db.tblorder.Add(order); db.SaveChanges(); int orderID = (from p in db.tblorder orderby p.idorder descending select p.idorder).FirstOrDefault(); int cardq = (from q in db.tblpack where q.idpack == packID select q.cardquantity).FirstOrDefault(); #region Kartenpacks if (cardq != 0) { // Update Person ! try { var updatePerson = (from p in db.tblperson where p.idperson == personID select p); var packValue = (from v in db.tblpack where v.idpack == packID select v.packprice).FirstOrDefault(); foreach (var value in updatePerson) { value.currencybalance -= (int)packValue; } db.SaveChanges(); } catch (Exception e) { Log.Writer.LogError(e); } // Insert Cards ! for (int i = 0; i < cardq; i++) { int rng = r.Next(1, 698); var card = (from c in db.tblcard where c.idcard == rng select c).FirstOrDefault(); if (card != null) { col.fkperson = personID; col.fkorder = orderID; col.fkcard = card.idcard; db.tblcollection.Add(col); db.SaveChanges(); } else { i = i - 1; } } } #endregion #region Goldpacks else { //TODO - ausbessern if (true) { tblperson person = new tblperson(); var updatePerson = (from p in db.tblperson where p.idperson == personID select p); var goldValue = (from g in db.tblpack where g.idpack == packID select g.goldquantity).FirstOrDefault(); foreach (var value in updatePerson) { value.currencybalance += (int)goldValue; } db.SaveChanges(); } else { //was auch immer } } #endregion } }
/// <summary> /// Whole logic for register, /// Generates Hashed Pass and Salt, generates 3 decks for the User, sends a confirmation Mail to the User /// and saves everything to the Database /// </summary> /// <param name="regUser"></param> /// <returns>bool named register</returns> public static bool Register(tblperson regUser) { try { using (var db = new ClonestoneFSEntities()) { if (db.tblperson.Any(n => n.email == regUser.email)) { throw new Exception("UserAlreadyExists"); } //Salt erzeugen string salt = Helper.GenerateSalt(); //Passwort Hashen string hashedAndSaltedPassword = Helper.GenerateHash(regUser.password + salt); regUser.password = hashedAndSaltedPassword; regUser.salt = salt; db.tblperson.Add(regUser); db.SaveChanges(); //Decks Speichern tbldeck deck = new tbldeck(); deck.deckname = "Mage"; deck.fkperson = regUser.idperson; db.tbldeck.Add(deck); tbldeck deck1 = new tbldeck(); deck1.deckname = "Hunter"; deck1.fkperson = regUser.idperson; db.tbldeck.Add(deck1); tbldeck deck2 = new tbldeck(); deck2.deckname = "Rogue"; deck2.fkperson = regUser.idperson; db.tbldeck.Add(deck2); db.SaveChanges(); //TODO - Email Einstellungen korregieren!!! //try //{ // SmtpClient client = new SmtpClient("mail.gmx.net"); // client.Credentials = new NetworkCredential("*****@*****.**", "123user!"); // client.Port = 465; // client.EnableSsl = true; // MailMessage mess = new MailMessage(); // mess.From = new MailAddress("*****@*****.**"); // mess.To.Add($"{regUser.email}"); // mess.Subject = "Registration confirmation!"; // mess.Body = "Welcome to Clonestone, thank you for your registration. As gift you got 1000 Gold from us to start. Have fun!"; // client.Send(mess); //} //catch (Exception e) //{ // Debug.WriteLine(e.Message); //} } } catch (Exception e) { throw; //Writer.LogError(e); } return(true); }