public HttpResponseMessage LoginUser([FromBody] UserRegisterLoginModel userModel)
        {
            var responseMessage = this.PerformOperationAndHandleExceptions(
              () =>
              {
                  this.ValidateUsername(userModel.Username);
                  this.ValidateAuthCode(userModel.AuthenticationCode);

                  var context = this.contextFactory.Create();

                  using (context)
                  {
                      var usernameToLower = userModel.Username.ToLower();

                      var user = context.Set<User>()
                          .Where(x => x.Username == usernameToLower && x.AuthenticationCode == userModel.AuthenticationCode).FirstOrDefault();

                      if (user == null)
                      {
                          throw new InvalidOperationException("Invalid username or password!");
                      }

                      if (user.SessionKey == null)
                      {
                          user.SessionKey = this.GenerateSessionKey(user.Id);
                          context.SaveChanges();
                      }

                      UserLoggedModel loggedUser = new UserLoggedModel()
                      {
                          Nickname = user.Nickname,
                          SessionKey = user.SessionKey
                      };

                      var response = this.Request.CreateResponse(HttpStatusCode.OK, loggedUser);
                      return response;
                  }
              });

            return responseMessage;
        }
        public HttpResponseMessage RegisterUser([FromBody] UserRegisterLoginModel userModel)
        {
            var responseMessage = this.PerformOperationAndHandleExceptions(() =>
                {
                    this.ValidateUsername(userModel.Username);
                    this.ValidateNickname(userModel.Nickname);
                    this.ValidateAuthCode(userModel.AuthenticationCode);

                    var context = this.contextFactory.Create();

                    using (context)
                    {
                        var usernameToLower = userModel.Username.ToLower();
                        var nicknameToLower = userModel.Nickname.ToLower();

                        var user = context.Set<User>().FirstOrDefault(
                           usr => usr.Username == usernameToLower
                           || usr.Nickname.ToLower() == nicknameToLower);

                        if (user != null)
                        {
                            throw new InvalidOperationException("User already exists!");
                        }

                        user = new User()
                        {
                            Username = usernameToLower,
                            Nickname = userModel.Nickname,
                            AuthenticationCode = userModel.AuthenticationCode
                        };

                        context.Set<User>().Add(user);
                        context.SaveChanges();

                        user.SessionKey = this.GenerateSessionKey(user.Id);
                        context.SaveChanges();

                        var loggedModel = new UserLoggedModel()
                        {
                            Nickname = user.Nickname,
                            SessionKey = user.SessionKey
                        };

                        var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel);
                        return response;
                    }
                });

            return responseMessage;
        }