public IActionResult Register(LoginRegUser postData) { User newUser = postData.User; if (ModelState.IsValid) { // Check if email exists in DB if (db.Users.Any(u => u.Email == newUser.Email)) { ModelState.AddModelError("User.Email", "This email is already in use."); return(View("Index")); } // Hash incoming user password PasswordHasher <User> hasher = new PasswordHasher <User>(); newUser.Password = hasher.HashPassword(newUser, newUser.Password); // Add new user to DB db.Users.Add(newUser); db.SaveChanges(); // Add user to session and redirect to Dashboard HttpContext.Session.SetInt32("UserId", newUser.UserId); return(RedirectToAction("Dashboard", "Home")); } return(View("Index")); }
public IActionResult AddCharacter([FromBody] Character newCharacter) { if (ModelState.IsValid) { //Populate Creator, Deity, and Role from session information newCharacter.Creator = db.Users.FirstOrDefault(id => id.UserId == 1); // newCharacter.DeityId = HttpContext.Session.GetObjectFromJson<Deity>("Deity").DeityId; // newCharacter.RoleId = HttpContext.Session.GetObjectFromJson<Role>("Role").RoleId; //Populate Divine Power and Role Power in CharacterPowers table from session // Power divinePowerJson = HttpContext.Session.GetObjectFromJson<Power>("DivinePower"); // Power rolePowerJson = HttpContext.Session.GetObjectFromJson<Power>("RolePower"); CharacterPower divineCharacterPower = new CharacterPower() { Character = newCharacter, PowerId = newCharacter.DivinePower, CurrentLevel = 1, }; CharacterPower roleCharacterPower = new CharacterPower() { Character = newCharacter, PowerId = newCharacter.RolePower, CurrentLevel = 1, }; db.Characters.Add(newCharacter); db.CharacterPowers.Add(divineCharacterPower); db.CharacterPowers.Add(roleCharacterPower); db.SaveChanges(); return(RedirectToAction("Dashboard")); } List <Deity> allDeities = db.Deities.ToList(); List <Role> allRoles = db.Roles.ToList(); List <Power> allPowers = db.Powers .OrderBy(tree => tree.SkillTreeName) .ToList(); CharacterCreator creationView = new CharacterCreator() { Deities = allDeities, Roles = allRoles, Powers = allPowers, }; return(View("CharacterCreation", creationView)); }