Example #1
0
        public IActionResult NewPlaylist(Playlist model)

        {
            if (SessionCheck() == 0)
            {
                return(RedirectToAction("Index", "User"));
            }

            if (ModelState.IsValid)
            {
                Playlist exist = _context.Playlists.SingleOrDefault(x => x.PName == model.PName); //query to set exist to check if playlist input already exists in db

                if (exist != null)
                {
                    ModelState.AddModelError("PName", "That playlist name already exists in the database.");
                    return(View("dashboard"));
                }

                else
                {
                    Playlist newPlaylist = new Playlist
                    {
                        PName  = model.PName,
                        UserId = (int)SessionCheck()
                    };

                    _context.Add(newPlaylist);
                    _context.SaveChanges();
                    return(RedirectToAction("Dashboard", "Streamify"));
                }
            }

            else
            {
                ViewBag.Playlists = _context.Playlists.ToList();
                return(View("dashboard"));
            }
        }
Example #2
0
        public IActionResult Register(UserViewModels model)  //checking passed in model against UserViewModel validations
        {
            if (ModelState.IsValid)
            {
                User exist = _context.Users.SingleOrDefault(u => u.Email == model.Reg.Email); //query to set exist to check if email input already exists in db

                if (exist != null)
                {
                    ModelState.AddModelError("Reg.Email", "That email already exists in the database.");
                    return(View("Index"));
                }
                else
                {
                    PasswordHasher <UserViewModels> hasher = new PasswordHasher <UserViewModels>();
                    string hashed  = hasher.HashPassword(model, model.Reg.Password);
                    User   newUser = new User
                    {
                        First    = model.Reg.First,
                        Last     = model.Reg.Last,
                        Email    = model.Reg.Email,
                        Password = hashed,
                    };

                    _context.Add(newUser);
                    _context.SaveChanges();
                    User CurrUser = _context.Users.SingleOrDefault(user => user.Email == newUser.Email); //see the newly created object by grabbing email
                    HttpContext.Session.SetInt32("userid", CurrUser.UserId);                             //setting session id
                    HttpContext.Session.SetString("username", CurrUser.First);                           //seting username to first name of session user


                    return(RedirectToAction("Success", "User"));
                }
            }
            else
            {
                return(View("Index"));
            }
        }