Persist user

This commit is contained in:
kagel 2016-08-22 03:22:39 +03:00
parent f6498069f7
commit baf07a21ec
8 changed files with 87 additions and 34 deletions

View File

@ -0,0 +1,2 @@
ALTER TABLE public.users
ALTER COLUMN id TYPE VARCHAR(20) USING id :: VARCHAR(20);

View File

@ -0,0 +1,2 @@
ALTER TABLE public.users
ALTER COLUMN id TYPE VARCHAR(40) USING id :: VARCHAR(40);

View File

@ -1,21 +1,23 @@
-- :name create-user! :! :n
-- :doc creates a new user record
INSERT INTO users
(id, first_name, last_name, email, pass)
VALUES (:id, :first_name, :last_name, :email, :pass)
(id, login, name, email, token, address, created)
VALUES (:id, :login, :name, :email, :token, :address, :created);
-- :name update-user! :! :n
-- :doc update an existing user record
UPDATE users
SET first_name = :first_name, last_name = :last_name, email = :email
WHERE id = :id
SET login = :login, name = :name, email = :email, token = :token, address = :address
WHERE id = :id;
-- :name update-user-address! :! :n
UPDATE users
SET address = :address
WHERE id = :id;
-- :name get-user :? :1
-- :doc retrieve a user given the id.
SELECT * FROM users
WHERE id = :id
-- :doc retrieve a user given the login.
SELECT *
FROM users
WHERE login = :login;
-- :name delete-user! :! :n
-- :doc delete a user given the id
DELETE FROM users
WHERE id = :id

View File

@ -50,7 +50,9 @@ repl-server
(cond
(some #{"migrate" "rollback"} args)
(do
(mount/start #'commiteth.config/env)
(mount/start
#'commiteth.config/env
#'commiteth.db.core/*db*)
(migrations/migrate args (select-keys env [:database-url]))
(System/exit 0))
:else

View File

@ -0,0 +1,21 @@
(ns commiteth.db.users
(:require [commiteth.db.core :refer [*db*] :as db]
[clojure.java.jdbc :as jdbc])
(:import [java.util Date UUID]))
(defn create-user
[login name email token]
(jdbc/with-db-connection [con-db *db*]
(db/create-user! con-db
{:id (str (UUID/randomUUID))
:login login
:name name
:email email
:token token
:address nil
:created (new Date)})))
(defn exists?
[login]
(jdbc/with-db-connection [con-db *db*]
(some? (db/get-user con-db {:login login}))))

View File

@ -1,5 +1,6 @@
(ns commiteth.github.core
(:require [tentacles.repos :as repos]
[tentacles.users :as users]
[ring.util.codec :as codec]
[clj-http.client :as http])
(:import [java.util UUID]))
@ -26,9 +27,16 @@
:redirect_uri redirect-uri
:state state}}))
(defn- auth-params
[token]
{:oauth-token token
:client-id client-id
:client-token client-secret})
(defn list-repos
[token]
(repos/repos
{:oauth-token token
:client-id client-id
:client-token client-secret}))
(repos/repos (auth-params token)))
(defn get-user
[token]
(users/me (auth-params token)))

View File

@ -2,7 +2,19 @@
(:require [clojure.walk :refer [keywordize-keys]]
[compojure.core :refer [defroutes GET]]
[ring.util.codec :as codec]
[commiteth.github.core :as github]))
[commiteth.github.core :as github]
[commiteth.db.users :as users]
[commiteth.layout :refer [error-page]]
[ring.util.response :as response]))
(defn- create-user
[token]
(let [user (github/get-user token)
{email :email
name :name
login :login} user]
(if-not (users/exists? login)
(users/create-user login name email token))))
(defroutes redirect-routes
(GET "/callback" [code state]
@ -10,6 +22,9 @@
body (keywordize-keys (codec/form-decode (:body resp)))
access-token (:access_token body)]
(if-let [error (:error body)]
(str "Error: " error)
(str "Your access token: " access-token "<br/>"
"Your repositories: " (reduce str (github/list-repos access-token)))))))
(:body
(error-page {:status 401
:title error}))
(do
(create-user access-token)
(response/redirect "/"))))))

View File

@ -20,17 +20,18 @@
(jdbc/db-set-rollback-only! t-conn)
(is (= 1 (db/create-user!
t-conn
{:id "1"
:first_name "Sam"
:last_name "Smith"
:email "sam.smith@example.com"
:pass "pass"})))
(is (= {:id "1"
:first_name "Sam"
:last_name "Smith"
:email "sam.smith@example.com"
:pass "pass"
:admin nil
:last_login nil
:is_active nil}
(db/get-user t-conn {:id "1"})))))
{:id "1"
:login "torvalds"
:name "Linus Torvalds"
:email nil
:token "not null"
:address "address"
:created nil})))
(is (= {:id "1"
:login "torvalds"
:name "Linus Torvalds"
:email nil
:token "not null"
:address "address"
:created nil}
(db/get-user t-conn {:login "torvalds"})))))