public HttpResponseMessage PostLoginUser(UserModel model)
        {
            var responseMsg = this.PerfromOperationAndHandleException(() =>
            {
                using (var dbContext = new AcadBlogContext())
                {
                    this.ValidateUsername(model.Username);
                    this.ValidateAuthCode(model.AuthCode);

                    var usernameAsLowercase = model.Username.ToLower();
                    var existingUser = dbContext.Users.FirstOrDefault(
                        u => u.Username == usernameAsLowercase &&
                        u.AuthCode == model.AuthCode);

                    if (existingUser == null)
                    {
                        throw new InvalidOperationException(
                            "Username or password is invalid.");
                    }

                    if (existingUser.SessionKey == null)
                    {
                        existingUser.SessionKey = this.GenerateSessionKey(existingUser.Id);
                        dbContext.SaveChanges();
                    }

                    var loggedModel = new LoggedUserModel()
                    {
                        Displayname = existingUser.Displayname,
                        SessionKey = existingUser.SessionKey
                    };

                    var response = this.Request.CreateResponse(HttpStatusCode.OK,
                        loggedModel);

                    return response;
                };
            });

            return responseMsg;
        }
        public HttpResponseMessage PostRegisterUser(UserModel model)
        {
            var responseMsg = this.PerfromOperationAndHandleException(() =>
            {
                using (var dbContext = new AcadBlogContext())
                {
                    this.ValidateUsername(model.Username);
                    this.ValidateNickname(model.Displayname);
                    this.ValidateAuthCode(model.AuthCode);

                    var usernameAsLowercase = model.Username.ToLower();
                    var nicknameAsLowercase = model.Displayname.ToLower();

                    var existingUser = dbContext.Users.FirstOrDefault(
                        u => u.Username == usernameAsLowercase ||
                            u.Displayname == nicknameAsLowercase);

                    if (existingUser != null)
                    {
                        throw new InvalidOperationException(
                            "User with the same displayname or username already exists.");
                    }

                    var newUser = new User()
                    {
                        Username = usernameAsLowercase,
                        Displayname = model.Displayname,
                        AuthCode = model.AuthCode,
                    };

                    dbContext.Users.Add(newUser);
                    dbContext.SaveChanges();

                    newUser.SessionKey = this.GenerateSessionKey(newUser.Id);
                    dbContext.SaveChanges();

                    var loggedModel = new LoggedUserModel()
                    {
                        Displayname = model.Displayname,
                        SessionKey = newUser.SessionKey
                    };

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

                    return response;
                };
            });

            return responseMsg;
        }