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 -- :name create-user! :! :n
-- :doc creates a new user record -- :doc creates a new user record
INSERT INTO users INSERT INTO users
(id, first_name, last_name, email, pass) (id, login, name, email, token, address, created)
VALUES (:id, :first_name, :last_name, :email, :pass) VALUES (:id, :login, :name, :email, :token, :address, :created);
-- :name update-user! :! :n -- :name update-user! :! :n
-- :doc update an existing user record -- :doc update an existing user record
UPDATE users UPDATE users
SET first_name = :first_name, last_name = :last_name, email = :email SET login = :login, name = :name, email = :email, token = :token, address = :address
WHERE id = :id WHERE id = :id;
-- :name update-user-address! :! :n
UPDATE users
SET address = :address
WHERE id = :id;
-- :name get-user :? :1 -- :name get-user :? :1
-- :doc retrieve a user given the id. -- :doc retrieve a user given the login.
SELECT * FROM users SELECT *
WHERE id = :id 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 (cond
(some #{"migrate" "rollback"} args) (some #{"migrate" "rollback"} args)
(do (do
(mount/start #'commiteth.config/env) (mount/start
#'commiteth.config/env
#'commiteth.db.core/*db*)
(migrations/migrate args (select-keys env [:database-url])) (migrations/migrate args (select-keys env [:database-url]))
(System/exit 0)) (System/exit 0))
:else :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 (ns commiteth.github.core
(:require [tentacles.repos :as repos] (:require [tentacles.repos :as repos]
[tentacles.users :as users]
[ring.util.codec :as codec] [ring.util.codec :as codec]
[clj-http.client :as http]) [clj-http.client :as http])
(:import [java.util UUID])) (:import [java.util UUID]))
@ -26,9 +27,16 @@
:redirect_uri redirect-uri :redirect_uri redirect-uri
:state state}})) :state state}}))
(defn- auth-params
[token]
{:oauth-token token
:client-id client-id
:client-token client-secret})
(defn list-repos (defn list-repos
[token] [token]
(repos/repos (repos/repos (auth-params token)))
{:oauth-token token
:client-id client-id (defn get-user
:client-token client-secret})) [token]
(users/me (auth-params token)))

View File

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

View File

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