Last active
February 3, 2019 23:44
-
-
Save souenzzo/87bc593d4051be214a22808b2e1fc7b5 to your computer and use it in GitHub Desktop.
primeiro hangout da Clojurers https://t.me/clojurebrasil
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Deve estar em `src/clojurebrasil/core.clj` | |
;; Execute `lein repl` na pasta do project.clj e tudo deve funcionar | |
(ns clojurebrasil.core | |
(:require [datomic.api :as d] | |
[cheshire.core :as cheshire] | |
[io.pedestal.http :as http])) | |
(def schema | |
[{:db/doc "id do usuário" | |
:db/ident :user/id | |
:db/index true | |
:db/valueType :db.type/uuid | |
:db/cardinality :db.cardinality/one | |
:db/unique :db.unique/identity | |
:db/id (d/tempid :db.part/db)} | |
{:db/doc "nome do usuário" | |
:db/ident :user/name | |
:db/index true | |
:db/fulltext true | |
:db/valueType :db.type/string | |
:db/cardinality :db.cardinality/one | |
:db/id (d/tempid :db.part/db)} | |
{:db/doc "todos do usuário" | |
:db/ident :user/todos | |
:db/index true | |
:db/valueType :db.type/ref | |
:db/cardinality :db.cardinality/many | |
:db/id (d/tempid :db.part/db)} | |
{:db/doc "o todo esta completo?" | |
:db/ident :todo/done? | |
:db/index true | |
:db/valueType :db.type/boolean | |
:db/cardinality :db.cardinality/one | |
:db/id (d/tempid :db.part/db)} | |
{:db/doc "texto do todo" | |
:db/ident :todo/text | |
:db/index true | |
:db/fulltext true | |
:db/valueType :db.type/string | |
:db/cardinality :db.cardinality/one | |
:db/id (d/tempid :db.part/db)} | |
{:db/doc "headers" | |
:db/ident :audit/headers | |
:db/index true | |
:db/fulltext true | |
:db/valueType :db.type/string | |
:db/cardinality :db.cardinality/one | |
:db/id (d/tempid :db.part/db)} | |
{:db/doc "texto do todo" | |
:db/ident :todo/id | |
:db/index true | |
:db/valueType :db.type/uuid | |
:db/unique :db.unique/identity | |
:db/cardinality :db.cardinality/one | |
:db/id (d/tempid :db.part/db)}]) | |
(def db-uri "datomic:mem://clojurebrasil") | |
(defonce conn (atom nil)) | |
(defn init-db! [] | |
(d/delete-database db-uri) | |
(d/create-database db-uri) | |
(reset! conn (d/connect db-uri)) | |
@(d/transact @conn schema))) | |
(defonce http-srv (atom nil)) | |
(defn db-get-users | |
[db search pattern] | |
(d/q '[:find [(pull ?e pattern) ...] | |
:in $ pattern ?search | |
:where | |
[?e :user/name ?n] | |
[(clojure.string/includes? ?n ?search)]] | |
db pattern (or search ""))) | |
(defn get-users | |
[{{:keys [search]} :query-params}] | |
(prn search) | |
(let [db (d/db @conn) | |
users (db-get-users db search [:user/name :user/id])] | |
{:body users | |
:status 200})) | |
(defn db-create-user | |
[tempid name] | |
[{:db/id tempid | |
:user/id (d/squuid) | |
:user/name name}]) | |
(defn create-user | |
[{:keys [body headers]}] | |
(let [{:keys [name]} (cheshire/parse-string (slurp body) true) | |
temp (d/tempid :db.part/user) | |
tx-data (concat (db-create-user temp name) | |
[{:db/id (d/tempid :db.part/tx) | |
:audit/headers (str headers)}]) | |
{:keys [db-after tempids]} @(d/transact @conn tx-data) | |
id (d/resolve-tempid db-after tempids temp)] | |
{:body (d/pull db-after [:user/name :user/id] id) | |
:status 200})) | |
(def ->json | |
{:name ::->json | |
:leave (fn [ctx] (update-in ctx [:response :body] cheshire/generate-string {:key-fn name}))}) | |
(def routes | |
#{["/user" :get [->json get-users] :route-name ::get-users] | |
["/user" :post [->json create-user] :route-name ::create-user]}) | |
(def service | |
{:env :dev | |
::http/type :jetty | |
::http/routes routes | |
::http/join? false | |
::http/port 8080}) | |
(defn -main | |
"The entry-point for 'lein run-dev'" | |
[& args] | |
(->> service | |
http/default-interceptors | |
http/dev-interceptors | |
http/create-server | |
http/start | |
(reset! http-srv))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defproject clojurebrasil "0.1.0-SNAPSHOT" | |
:dependencies [[org.clojure/clojure "1.9.0"] | |
[io.pedestal/pedestal.service "0.5.3"] | |
[io.pedestal/pedestal.jetty "0.5.3"] | |
[com.datomic/datomic-free "0.9.5656"]] | |
:source-paths ["src"] | |
:main clojurebrasil.core) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment