public HttpResponseMessage PostLoginUser(UserModel model)
        {
            return this.PerformOperationAndHandleExceptions(() =>
            {
                var context = new ForumContext();
                using (context)
                {
                    var usernameToLower = model.Username.ToLower();
                    var entity = context.Users.SingleOrDefault(u => u.Username == usernameToLower &&
                        u.AuthCode == model.AuthCode);
                    if (entity == null)
                    {
                        var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                            "Invalid username or password");
                        throw new HttpResponseException(errResponse);
                    }

                    entity.SessionKey = this.GenerateSessionKey(entity.Id);

                    context.SaveChanges();
                    var responseModel = new UserLoggedModel()
                    {
                        Username = entity.Username,
                        SessionKey = entity.SessionKey
                    };

                    var response = this.Request.CreateResponse(HttpStatusCode.Accepted, responseModel);
                    return response;
                }
            });
        }
        public HttpResponseMessage PostRegisterUser(UserFlatModel inputUser)
        {
            HttpResponseMessage responseMessage = this.PerformOperationAndHandleExceptions(
                 () =>
                 {
                     ForumContext context = new ForumContext();

                     using (context)
                     {
                         this.ValidateUsername(inputUser.Username);
                         this.ValidateAuthCode(inputUser.AuthCode);

                         var usernameToLower = inputUser.Username.ToLower();

                         User user = context.Users.FirstOrDefault(
                             usr => usr.Username == usernameToLower);
                         if (user != null)
                         {
                             throw new InvalidOperationException("User already exists");
                         }

                         user = new User()
                         {
                             Username = usernameToLower,
                             AuthCode = inputUser.AuthCode,
                             CreationDate = DateTime.Now
                         };

                         context.Users.Add(user);
                         context.SaveChanges();

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

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

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

            return responseMessage;
        }
        public HttpResponseMessage PostLoginUser(UserFlatModel inputUser)
        {
            HttpResponseMessage responseMessage = this.PerformOperationAndHandleExceptions(
              () =>
              {
                  ForumContext context = new ForumContext();

                  using (context)
                  {
                      this.ValidateUsername(inputUser.Username);
                      this.ValidateAuthCode(inputUser.AuthCode);

                      var usernameToLower = inputUser.Username.ToLower();

                      User user = context.Users.FirstOrDefault(
                          usr => usr.Username == usernameToLower
                          && usr.AuthCode == inputUser.AuthCode);

                      if (user == null)
                      {
                          throw new InvalidOperationException("Invalid username or password");
                      }
                      if (user.SessionKey == null)
                      {
                          user.SessionKey = this.GenerateSessionKey(user.Id);
                          context.SaveChanges();
                      }
                      if (user.IsBanned == true)
                      {
                          throw new ArgumentException("User is banned.");
                      }

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

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

            return responseMessage;
        }