public ActionResult Register([Bind(Include = "UserID,Email,Password,FirstName,LastName,IsAdmin")] User user)
        {
            if (ModelState.IsValid)
            {
                using (GOLDBEntities database = new GOLDBEntities())
                {
                    // Retrieve a user with the same username and password.
                    User login = database.Users.FirstOrDefault(u => u.Email == user.Email &&
                                                               u.Password == user.Password);

                    // If successful set the session variables and go to Member page.
                    if (login == null)
                    {
                        Session["Username"] = user.Email;
                        Session["Name"]     = user.FirstName;

                        user.IsAdmin  = false;
                        user.Password = Crypter.Blowfish.Crypt(user.Password);
                        db.Users.Add(user);
                        db.SaveChanges();
                        return(RedirectToAction("Index", "Home"));
                    }
                }
            }
            return(View(user));
        }
        public ActionResult SaveGameFromActiveList
            (int?id)
        {
            //if (Session["UserName"] != null)
            //{
            if (ModelState.IsValid)
            {
                var userGames = Session["ActiveGames"] as UserActiveGames;
                var g         = userGames.findGame(id);
                // make a copy of the object to avoid the circular
                // ref issue
                UserGame newSavedGame = new UserGame(g);
                // check if there is a matching game in the DB

                var existingGame = db.UserGames.
                                   Where(u => u.UserGameID == g.UserGameID).
                                   FirstOrDefault();

                if (existingGame == null)
                {
                    db.UserGames.Add(newSavedGame);
                    string sessionUserName = Session["UserName"].ToString();


                    var user = db.Users.
                               Where(u => u.Email == sessionUserName).
                               First();

                    if (user != null)
                    {
                        user.UserGames.Add(newSavedGame);
                        db.Entry(user).State = EntityState.Modified;
                    }
                }
                // if the game is already saved
                // we need to update the existing the game
                else
                {
                    existingGame.Cells           = g.Cells;
                    db.Entry(existingGame).State = EntityState.Modified;
                }



                db.SaveChanges();

                // the game now needs to be removed from active games

                userGames.removeGame(g);



                return(RedirectToAction("ListSavedGames"));
                //return RedirectToAction("Index");
            }
            //}

            return(View());
        }
Exemplo n.º 3
0
        public ActionResult Create(
            UserTemplate userTemplate)
        {
            // TODO:
            // check the session is still valid
            // find the matching User in the DB
            // associate the Template with the User in the DB
            // by adding the Template to the User object Template
            // Collection

            //if (Session["Name"] != null)
            //{

            if (ModelState.IsValid)
            {
                db.UserTemplates.Add(userTemplate);

                string sessionUserName = Session["UserName"].ToString();


                var user = db.Users.
                           Where(u => u.Email == sessionUserName).
                           First();

                if (user != null)
                {
                    user.UserTemplates.Add(userTemplate);
                    db.Entry(user).State = EntityState.Modified;
                }


                db.SaveChanges();


                return(RedirectToAction("Index"));
            }
            //}

            ViewBag.UserID = new SelectList(db.Users, "UserID", "Email", userTemplate.UserID);
            return(View(userTemplate));
        }