Exemplo n.º 1
0
 public PosterModel(User usr)
 {
     username = usr.username;
 }
        public ActionResult setUsername(string username)
        {
            //nothing is done if provided username is blank
            if (username != "")
            {
                OnlineChatDBEntities db = new OnlineChatDBEntities();

                //if an existing username is taken by session, set it as inactive
                if (Session["username"] != null && username != Session["username"].ToString())
                    setUsernameInactive(Session["username"].ToString());

                //if username doesn't currently exist, create it in the database
                User target_user = db.Users.Where(u => u.username == username).FirstOrDefault();
                if (target_user == null)
                {
                    target_user = new User();
                    target_user.username = username;
                    target_user.is_session_active = false;
                    db.Users.Add(target_user);
                }

                //check if username is taken, if free, set session variable to username and session trait in db to true
                if (!isUsernameTaken(username, db))
                {
                    HttpContext.Session["username"] = username;
                    target_user.is_session_active = true;

                    db.SaveChanges();

                    return Json(username, JsonRequestBehavior.AllowGet);
                }
            }

            return Json(false, JsonRequestBehavior.AllowGet);
        }