public void Add(Panda panda) { using (var ctx = new PandaContext(_dbHelper)) { ctx.Pandas.Add(panda); ctx.SaveChanges(); } }
public IActionResult Register(RegViewModel model) { PasswordHasher <RegViewModel> hasher = new PasswordHasher <RegViewModel>(); //check if user email is already in database and add error if so if (_context.users.Where(user => user.username == model.Email).SingleOrDefault() != null) { ModelState.AddModelError("Email", "Username already in use, please log in or choose another username"); } //if form is valid add new user to database and hash password if (ModelState.IsValid) { User NewUser = new User { name = model.Name, alias = model.Alias, username = model.Email, password = hasher.HashPassword(model, model.Password), created_at = DateTime.Now, updated_at = DateTime.Now, }; //save new user object to add to session User fresh = _context.users.Add(NewUser).Entity; _context.SaveChanges(); HttpContext.Session.SetInt32("id", fresh.userid); HttpContext.Session.SetString("alias", fresh.alias); return(RedirectToAction("Home", "Trash")); } return(View("Entry")); }
public User RegisterUser(string username, string password, string confirmPassword, string email) { Role role; if (!context.Users.Any()) { role = Role.Admin; } else { role = Role.User; } var user = new User() { Username = username, Password = password, Email = email, Role = role }; context.Users.Add(user); context.SaveChanges(); return(user); }
public void Delete(string name) { using (var ctx = new PandaContext(_dbHelper)) { var panda = ctx.Pandas.Single(p => p.Name == name); ctx.Remove(panda); ctx.SaveChanges(); } }
public IActionResult Add(PostViewModel newidea) { // test if user is currently logged in with session if (ActiveUser == null) { TempData["ErrorMessage"] = "You've been redirected because you either tried to access a page without logging in (naughty!) or your session expired (whatchyou waitin' for?!) Please login to proceed!!"; return(RedirectToAction("Entry", "Panda")); } //check if post form is valid and then add new idea information to database if (ModelState.IsValid) { Post newPost = new Post { postmessage = newidea.idea, created_at = DateTime.Now, updated_at = DateTime.Now, userid = HttpContext.Session.GetInt32("id"), }; _context.posts.Add(newPost); _context.SaveChanges(); } return(RedirectToAction("Home")); }