public HttpResponseMessage RegisterUser(UserRegisterModel user)
        {
            var responseMsg = this.PerformOperation(() =>
            {
                UserDataPersister.CreateUser(user);
                string nickname;
                int id;
                string rank;
                UserLoginModel loginUser = new UserLoginModel()
                {
                    Username = user.Username,
                    AuthKey = user.AuthKey
                };

                var sessionKey = UserDataPersister.LoginUser(loginUser, out nickname, out id,out rank);

                return new UserLoggedModel()
                {
                    Id = id,
                    Nickname = nickname,
                    SessionKey = sessionKey
                };
            });

            return responseMsg;
        }
        public static void CreateUser(UserRegisterModel user)
        {
            ValidateUsername(user.Username);
            ValidateNickname(user.Nickname);
            ValidateAuthKey(user.AuthKey);
            using (CoolBlogEntity context = new CoolBlogEntity())
            {
                var usernameToLower = user.Username.ToLower();
                var nicknameToLower = user.Nickname.ToLower();
                var dbUser = context.Users.FirstOrDefault(u => u.Username == usernameToLower || u.Nickname.ToLower() == nicknameToLower);

                if (dbUser != null)
                {
                    if (dbUser.Username.ToLower() == usernameToLower)
                    {
                        throw new ServerErrorException("Username already exists", "ERR_DUP_USR");
                    }
                    else
                    {
                        throw new ServerErrorException("Nickname already exists", "ERR_DUP_NICK");
                    }
                }

                dbUser = new User()
                {
                    Username = usernameToLower,
                    Nickname = user.Nickname,
                    AuthKey = user.AuthKey,
                    Gender = user.Gender,
                    Email = user.Email,
                    RankId = user.RankId
                };

                context.Users.Add(dbUser);
                context.SaveChanges();
            }
        }