From 8904df248b1d14b4f36d1d969fe3666f89e501b0 Mon Sep 17 00:00:00 2001 From: Emillyn Muniz Date: Mon, 9 May 2022 12:11:18 -0300 Subject: [PATCH 01/13] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e26cf5f..738dc33 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,4 @@ Projeto bancário para mergulhar no mundo da **programação funcional** com Clojure + Datomic! -Desenvolvido por *[COLOCAR NOME AQUI]* (*[COLOCAR SEU EMAIL AQUI]*). \ No newline at end of file +Desenvolvido por Emillyn Muniz (emillyn_muniz@hotmail.com). From 7e5bd65706ece553ed03b1f0c323c0b8bc003f66 Mon Sep 17 00:00:00 2001 From: emimuniz Date: Mon, 9 May 2022 20:42:54 -0300 Subject: [PATCH 02/13] adicionando novo cliente --- .idea/misc.xml | 6 +++--- .idea/runConfigurations.xml | 10 ---------- .idea/vcs.xml | 6 ++++++ src/yes_she_codes/core.clj | 15 +++++++++++---- test/yes_she_codes/core_test.clj | 11 +++++++++-- yes-she-codes.iml | 4 ++++ 6 files changed, 33 insertions(+), 19 deletions(-) delete mode 100644 .idea/runConfigurations.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index 45a2d32..7d294aa 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -2,12 +2,12 @@ - + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml deleted file mode 100644 index 797acea..0000000 --- a/.idea/runConfigurations.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/yes_she_codes/core.clj b/src/yes_she_codes/core.clj index 54cbf42..cf1dd1b 100644 --- a/src/yes_she_codes/core.clj +++ b/src/yes_she_codes/core.clj @@ -1,6 +1,13 @@ (ns yes-she-codes.core) -(defn foo - "I don't do a whole lot." - [x] - (println x "Hello, World!")) +(defn novo-client + [nome cpf email] + (let [client (conj {:nome nome :cpf cpf :email email})] + [client] )) + +(println (novo-client "Emillyn" "8239283298" "fulano@hotmail.com")) + + + + + diff --git a/test/yes_she_codes/core_test.clj b/test/yes_she_codes/core_test.clj index d6d282f..03d6559 100644 --- a/test/yes_she_codes/core_test.clj +++ b/test/yes_she_codes/core_test.clj @@ -2,6 +2,13 @@ (:require [clojure.test :refer :all] [yes-she-codes.core :refer :all])) +(defn cria-map [nome cpf email] + [{:nome nome :cpf cpf :email email}]) + +(defn cria-cartao [numero cvv validate limite cpf] + {:numero numero :cvv cvv :validate validate :limite limite :cpf cpf}) + (deftest a-test - (testing "FIXME, I fail." - (is (= 0 1)))) + (testing "verifica se cria um novo cliente corretamente" + (is (= [{:nome "Emillyn", :cpf "1829182918", :email "testando"}] ) (cria-map "Emillyn" "1829182918" "testando")))) + diff --git a/yes-she-codes.iml b/yes-she-codes.iml index 254bfec..fcf1436 100644 --- a/yes-she-codes.iml +++ b/yes-she-codes.iml @@ -1,5 +1,9 @@ + + From e798f237e9c3d199c6d0c0fd682e61ddf08d6c62 Mon Sep 17 00:00:00 2001 From: emimuniz Date: Tue, 10 May 2022 12:21:57 -0300 Subject: [PATCH 03/13] Refactor novo-client --- src/yes_she_codes/core.clj | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/yes_she_codes/core.clj b/src/yes_she_codes/core.clj index cf1dd1b..b900f49 100644 --- a/src/yes_she_codes/core.clj +++ b/src/yes_she_codes/core.clj @@ -1,13 +1,17 @@ (ns yes-she-codes.core) -(defn novo-client - [nome cpf email] +(defn novo-cliente + [[nome cpf email]] (let [client (conj {:nome nome :cpf cpf :email email})] - [client] )) - -(println (novo-client "Emillyn" "8239283298" "fulano@hotmail.com")) - + client)) +(defn lista-clients [] + (let [clients [["feiticeira escarlate" "000.111.222-33" "feiticeira.poderosa@vingadoras.com.br"] + ["Viúva Negra" "333.444.555-66" "viuva.casca.grossa@vingadoras.com.br"] + ["Hermione Granger" "666.777.888-99" "hermione.salvadora@hogwarts.com"] + ["Daenerys Targaryen" "999.123.456-78" "mae.dos.dragoes@got.com"]]] + (vec (map novo-cliente clients)))) +(println (lista-clients)) From d3cfaaef900d2099b018c96dc78a444dd58fd88a Mon Sep 17 00:00:00 2001 From: emimuniz Date: Tue, 10 May 2022 20:26:41 -0300 Subject: [PATCH 04/13] criacao da funcao novo-cartao --- src/yes_she_codes/core.clj | 25 +++++++++++++++++++++++-- test/yes_she_codes/core_test.clj | 9 +++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/yes_she_codes/core.clj b/src/yes_she_codes/core.clj index b900f49..8131176 100644 --- a/src/yes_she_codes/core.clj +++ b/src/yes_she_codes/core.clj @@ -1,5 +1,8 @@ (ns yes-she-codes.core) +(def conta-clients + (atom {})) + (defn novo-cliente [[nome cpf email]] (let [client (conj {:nome nome :cpf cpf :email email})] @@ -10,8 +13,26 @@ ["Viúva Negra" "333.444.555-66" "viuva.casca.grossa@vingadoras.com.br"] ["Hermione Granger" "666.777.888-99" "hermione.salvadora@hogwarts.com"] ["Daenerys Targaryen" "999.123.456-78" "mae.dos.dragoes@got.com"]]] - (vec (map novo-cliente clients)))) + (swap! conta-clients assoc-in [:clients] (vec (map novo-cliente clients))))) + + +(lista-clients) +(println @conta-clients) + +(defn get-client + [field value data] + (filter #(= value (field %)) data)) + +(defn add-cartao [conta] + (swap! conta-clients update :clients conj conta)) +(defn novo-cartao + [numero cvv validate limite cpf] + (let [client (into {} (get-client :cpf cpf (:clients @conta-clients)))] + (let [cartao {:conta {:numero numero, :cvv cvv, :validate validate :limite limite}}] + (let [conta (conj client cartao)] + (add-cartao conta))))) -(println (lista-clients)) +(println (get-client :cpf "000.111.222-33" (:clients @conta-clients))) +(println (novo-cartao 12819289182918 233 "2022/12" 1000.0 "000.111.222-33")) diff --git a/test/yes_she_codes/core_test.clj b/test/yes_she_codes/core_test.clj index 03d6559..287b550 100644 --- a/test/yes_she_codes/core_test.clj +++ b/test/yes_she_codes/core_test.clj @@ -6,9 +6,14 @@ [{:nome nome :cpf cpf :email email}]) (defn cria-cartao [numero cvv validate limite cpf] - {:numero numero :cvv cvv :validate validate :limite limite :cpf cpf}) + {:numero numero :cvv cvv :validate validate :limite limite :client cpf}) (deftest a-test (testing "verifica se cria um novo cliente corretamente" - (is (= [{:nome "Emillyn", :cpf "1829182918", :email "testando"}] ) (cria-map "Emillyn" "1829182918" "testando")))) + (is (= [{:nome "Emillyn", :cpf "1829182918", :email "testando"}] ) (cria-map "Emillyn" "1829182918" "testando"))) + + (testing "verifica se cria um novo cartao corretamente" + (is (= {:numero 12819289182918, :cvv 233, :validate "2022/12" :limite 1000.0 :cpf "238923928"} ) + (cria-cartao 12819289182918 233 "2022/12" 1000.0 "238923928"))) + ) From 81d48db69d12f70dfc8153d330f4b1888089c909 Mon Sep 17 00:00:00 2001 From: emimuniz Date: Wed, 11 May 2022 12:27:57 -0300 Subject: [PATCH 05/13] criacao da funcao nova-compra --- src/yes_she_codes/core.clj | 18 +++++++++++++++--- test/yes_she_codes/core_test.clj | 5 ++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/yes_she_codes/core.clj b/src/yes_she_codes/core.clj index 8131176..34e6de8 100644 --- a/src/yes_she_codes/core.clj +++ b/src/yes_she_codes/core.clj @@ -15,12 +15,11 @@ ["Daenerys Targaryen" "999.123.456-78" "mae.dos.dragoes@got.com"]]] (swap! conta-clients assoc-in [:clients] (vec (map novo-cliente clients))))) - (lista-clients) (println @conta-clients) (defn get-client - [field value data] + [field value data] (filter #(= value (field %)) data)) (defn add-cartao [conta] @@ -29,10 +28,23 @@ (defn novo-cartao [numero cvv validate limite cpf] (let [client (into {} (get-client :cpf cpf (:clients @conta-clients)))] - (let [cartao {:conta {:numero numero, :cvv cvv, :validate validate :limite limite}}] + (let [cartao {:cartao {:numero numero, :cvv cvv, :validate validate :limite limite}}] (let [conta (conj client cartao)] (add-cartao conta))))) +(defn nova-compra + [data valor estabelecimento categoria cartao] + (let [client (into {} (get-client #(:numero (:cartao %)) cartao (:clients @conta-clients)))] + (let [compra {:compras {:data data, :valor valor, :estabelecimento estabelecimento :categoria categoria}}] + (let [conta (assoc-in client [:cartao] (conj (:cartao client) compra))] + conta)))) + (println (get-client :cpf "000.111.222-33" (:clients @conta-clients))) (println (novo-cartao 12819289182918 233 "2022/12" 1000.0 "000.111.222-33")) + +(println (get-client (comp :numero :cartao) 12819289182918 (:clients @conta-clients))) +(println (get-client #(:numero (:cartao %)) 12819289182918 (:clients @conta-clients))) + +(println (nova-compra "01/2012" 1000 "Americanas" "Alimentacao" 12819289182918)) +(println (nova-compra "01/2013" 1000 "Ifood" "Alimentacao" 12819289182918)) \ No newline at end of file diff --git a/test/yes_she_codes/core_test.clj b/test/yes_she_codes/core_test.clj index 287b550..754f4b2 100644 --- a/test/yes_she_codes/core_test.clj +++ b/test/yes_she_codes/core_test.clj @@ -10,10 +10,9 @@ (deftest a-test (testing "verifica se cria um novo cliente corretamente" - (is (= [{:nome "Emillyn", :cpf "1829182918", :email "testando"}] ) (cria-map "Emillyn" "1829182918" "testando"))) + (is (= [{:nome "Emillyn", :cpf "1829182918", :email "testando"}]) (cria-map "Emillyn" "1829182918" "testando"))) (testing "verifica se cria um novo cartao corretamente" (is (= {:numero 12819289182918, :cvv 233, :validate "2022/12" :limite 1000.0 :cpf "238923928"} ) - (cria-cartao 12819289182918 233 "2022/12" 1000.0 "238923928"))) - ) + (cria-cartao 12819289182918 233 "2022/12" 1000.0 "238923928")))) From fbb9e4fb954936657009a503ef748492e4cbbd81 Mon Sep 17 00:00:00 2001 From: emimuniz Date: Wed, 11 May 2022 17:14:50 -0300 Subject: [PATCH 06/13] refatorando codigo & adicionando calculando total-gasto-compras --- src/yes_she_codes/core.clj | 79 ++++++++++++++++++++++---------------- src/yes_she_codes/db.clj | 25 ++++++++++++ 2 files changed, 70 insertions(+), 34 deletions(-) create mode 100644 src/yes_she_codes/db.clj diff --git a/src/yes_she_codes/core.clj b/src/yes_she_codes/core.clj index 34e6de8..b1641eb 100644 --- a/src/yes_she_codes/core.clj +++ b/src/yes_she_codes/core.clj @@ -1,50 +1,61 @@ -(ns yes-she-codes.core) +(ns yes-she-codes.core + (:require [yes-she-codes.db :as l.db])) -(def conta-clients - (atom {})) +(def conta-clients {}) + +(def cartoes-clients {}) + +(def compras-clients {}) (defn novo-cliente [[nome cpf email]] (let [client (conj {:nome nome :cpf cpf :email email})] client)) -(defn lista-clients [] - (let [clients [["feiticeira escarlate" "000.111.222-33" "feiticeira.poderosa@vingadoras.com.br"] - ["Viúva Negra" "333.444.555-66" "viuva.casca.grossa@vingadoras.com.br"] - ["Hermione Granger" "666.777.888-99" "hermione.salvadora@hogwarts.com"] - ["Daenerys Targaryen" "999.123.456-78" "mae.dos.dragoes@got.com"]]] - (swap! conta-clients assoc-in [:clients] (vec (map novo-cliente clients))))) +(defn novo-cartao + [[numero cvv validate limite cpf]] + (let [client (conj {:numero numero, :cvv cvv, :validate validate :limite limite :cpf cpf})] + client)) + +(defn nova-compra + [[data valor estabelecimento categoria cartao]] + (let [client (conj {:data data, :valor valor, :estabelecimento estabelecimento :categoria categoria :cartao cartao})] + client)) -(lista-clients) -(println @conta-clients) -(defn get-client - [field value data] - (filter #(= value (field %)) data)) +(defn parametros-clients [] + (let [clients (l.db/param-clientes)] + (assoc-in conta-clients [:clients] (vec (map novo-cliente clients))))) + + +(defn parametros-cartoes [] + (let [cartoes (l.db/param-cartoes)] + (assoc-in cartoes-clients [:cartoes] (vec (map novo-cartao cartoes))))) + +(defn parametros-compras [] + (let [compras (l.db/param-compras)] + (assoc-in compras-clients [:compras] (vec (map nova-compra compras))))) + +(defn lista-clientes [] + (:clients (parametros-clients))) + +(defn lista-cartoes [] + (:cartoes (parametros-cartoes))) + +(defn lista-compras [] + (:compras (parametros-compras))) + +(println (lista-compras)) + +(defn total-gasto [compras] + (reduce + (map #(:valor %) compras))) + + +(println (total-gasto (:compras (parametros-compras)))) -(defn add-cartao [conta] - (swap! conta-clients update :clients conj conta)) -(defn novo-cartao - [numero cvv validate limite cpf] - (let [client (into {} (get-client :cpf cpf (:clients @conta-clients)))] - (let [cartao {:cartao {:numero numero, :cvv cvv, :validate validate :limite limite}}] - (let [conta (conj client cartao)] - (add-cartao conta))))) -(defn nova-compra - [data valor estabelecimento categoria cartao] - (let [client (into {} (get-client #(:numero (:cartao %)) cartao (:clients @conta-clients)))] - (let [compra {:compras {:data data, :valor valor, :estabelecimento estabelecimento :categoria categoria}}] - (let [conta (assoc-in client [:cartao] (conj (:cartao client) compra))] - conta)))) -(println (get-client :cpf "000.111.222-33" (:clients @conta-clients))) -(println (novo-cartao 12819289182918 233 "2022/12" 1000.0 "000.111.222-33")) -(println (get-client (comp :numero :cartao) 12819289182918 (:clients @conta-clients))) -(println (get-client #(:numero (:cartao %)) 12819289182918 (:clients @conta-clients))) -(println (nova-compra "01/2012" 1000 "Americanas" "Alimentacao" 12819289182918)) -(println (nova-compra "01/2013" 1000 "Ifood" "Alimentacao" 12819289182918)) \ No newline at end of file diff --git a/src/yes_she_codes/db.clj b/src/yes_she_codes/db.clj new file mode 100644 index 0000000..2591176 --- /dev/null +++ b/src/yes_she_codes/db.clj @@ -0,0 +1,25 @@ +(ns yes-she-codes.db) + + +(def clientes [["feiticeira escarlate" "000.111.222-33" "feiticeira.poderosa@vingadoras.com.br"] + ["Viúva Negra" "333.444.555-66" "viuva.casca.grossa@vingadoras.com.br"] + ["Hermione Granger" "666.777.888-99" "hermione.salvadora@hogwarts.com"] + ["Daenerys Targaryen" "999.123.456-78" "mae.dos.dragoes@got.com"]]) + +(def cartoes [["1234 1234 1234 1234" 111 1000 "12/2012" "000.111.222-33"] + ["4321 4321 4321 4321" 222 2000 "12/2012" "333.444.555-66"] + ["1598 1598 1598 1598" 333 3000 "12/2012" "666.777.888-99"] + ["6655 6655 6655 6655" 444 4000 "12/2012" "666.777.888-99"] + ["3939 3939 3939 3939" 555 5000 "12/2012" "999.123.456-78"]]) + +(def compras [["2022-01-01" 129.90 "Outback" "Alimentaçao" "1234 1234 1234 1234"] + ["2022-01-01" 129.90 "Outback" "Alimentaçao" "1234 1234 1234 1234"] + ["2022-01-01" 129.90 "Outback" "Alimentaçao" "1234 1234 1234 1234"] + ["2022-01-01" 129.90 "Outback" "Alimentaçao" "1234 1234 1234 1234"]]) + + +(defn param-clientes [] clientes) + +(defn param-cartoes [] cartoes) + +(defn param-compras [] compras) \ No newline at end of file From e0bb25e1de9a6d4ab511fa0131460710b6ceab88 Mon Sep 17 00:00:00 2001 From: emimuniz Date: Thu, 12 May 2022 14:38:07 -0300 Subject: [PATCH 07/13] Conclusao dos Desafios Inciantes --- src/yes_she_codes/core.clj | 54 +++++++++++++++++++++++++++++++++++++- src/yes_she_codes/db.clj | 14 +++++++--- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/src/yes_she_codes/core.clj b/src/yes_she_codes/core.clj index b1641eb..b1b696b 100644 --- a/src/yes_she_codes/core.clj +++ b/src/yes_she_codes/core.clj @@ -45,14 +45,66 @@ (defn lista-compras [] (:compras (parametros-compras))) -(println (lista-compras)) (defn total-gasto [compras] (reduce + (map #(:valor %) compras))) +(defn filtra-compras + [field value data] + (filter #(= value (field %)) data)) + +(defn filtra-compras-data + [field value data] + (filter #(= (re-matches (re-pattern (str "[0-9]{4}-" value "-[0-9]{2}")) (field %)) (field %)) data)) + + +(defn buscar-por-mes + [mes compras] + (filtra-compras-data :data mes compras)) + + +(defn total-gasto-mes + [mes cartao compras] + (let [client-cartao (filtra-compras :cartao cartao compras)] + (let [data (filtra-compras-data :data mes client-cartao)] + (total-gasto data)))) + + +(defn buscar-por-estabelecimento + [estabelecimento compras] + (filtra-compras :estabelecimento estabelecimento compras)) + +(defn filtrar-intervalo-compras + [minimo maximo compras] + (filter #(and (> (compare (% :data) minimo) 0) (< (compare (% :data) maximo) 0)) compras)) + + +(defn total-compras-por-categoria + [[categoria values]] + {:categoria categoria + :R$ (total-gasto values)}) + + +(defn agrupar-por-categoria [compras] + (map total-compras-por-categoria + (group-by :categoria compras))) + + + +(println (lista-compras)) (println (total-gasto (:compras (parametros-compras)))) +(println (buscar-por-mes "01" (:compras (parametros-compras)))) + +(println "total-gasto-mes" (total-gasto-mes "01" "1234 1234 1234 1234" (:compras (parametros-compras)))) + +(println (agrupar-por-categoria (:compras (parametros-compras)))) + +(println (buscar-por-estabelecimento "Alura" (:compras (parametros-compras)))) + +(println (filtrar-intervalo-compras "2022-01-01" "2022-03-01" (:compras (parametros-compras)))) + diff --git a/src/yes_she_codes/db.clj b/src/yes_she_codes/db.clj index 2591176..76dfc2e 100644 --- a/src/yes_she_codes/db.clj +++ b/src/yes_she_codes/db.clj @@ -12,10 +12,16 @@ ["6655 6655 6655 6655" 444 4000 "12/2012" "666.777.888-99"] ["3939 3939 3939 3939" 555 5000 "12/2012" "999.123.456-78"]]) -(def compras [["2022-01-01" 129.90 "Outback" "Alimentaçao" "1234 1234 1234 1234"] - ["2022-01-01" 129.90 "Outback" "Alimentaçao" "1234 1234 1234 1234"] - ["2022-01-01" 129.90 "Outback" "Alimentaçao" "1234 1234 1234 1234"] - ["2022-01-01" 129.90 "Outback" "Alimentaçao" "1234 1234 1234 1234"]]) +(def compras [["2022-01-01" 129.90 "Outback" "Alimentacao" "1234 1234 1234 1234"] + ["2022-01-01" 129.90 "Dentista" "Saude" "1234 1234 1234 1234"] + ["2022-02-01" 129.90 "Cinema" "Lazer" "1234 1234 1234 1234"] + ["2022-04-01" 129.90 "Show" "Lazer" "1234 1234 1234 1234"] + ["2022-04-01" 129.90 "Posto de Gasolina" "Automovel" "1234 1234 1234 1234"] + ["2022-04-01" 129.90 "Ifood" "Alimentacao" "1234 1234 1234 1234"] + ["2022-04-01" 129.90 "Alura" "Educacao" "1234 1234 1234 1234"] + ["2022-04-01" 129.90 "Tok&Stock" "Casa" "1234 1234 1234 1234"] + ["2022-04-01" 129.90 "Leroy Merlin" "Casa" "1234 1234 1234 1234"] + ["2022-04-01" 129.90 "Madero" "Alimentacao" "1234 1234 1234 1234"]]) (defn param-clientes [] clientes) From d4968bc23e1f4c31491f1fb96fcdd705f5381d67 Mon Sep 17 00:00:00 2001 From: emimuniz Date: Thu, 12 May 2022 21:27:44 -0300 Subject: [PATCH 08/13] reforando codigo --- .../libraries/Leiningen__clj_tuple_0_2_2.xml | 9 ++++ .../Leiningen__clojure_java_time_0_3_2.xml | 9 ++++ project.clj | 2 +- src/yes_she_codes/core.clj | 53 +++++++++---------- src/yes_she_codes/db.clj | 25 +++++---- yes-she-codes.iml | 2 + 6 files changed, 59 insertions(+), 41 deletions(-) create mode 100644 .idea/libraries/Leiningen__clj_tuple_0_2_2.xml create mode 100644 .idea/libraries/Leiningen__clojure_java_time_0_3_2.xml diff --git a/.idea/libraries/Leiningen__clj_tuple_0_2_2.xml b/.idea/libraries/Leiningen__clj_tuple_0_2_2.xml new file mode 100644 index 0000000..732be9a --- /dev/null +++ b/.idea/libraries/Leiningen__clj_tuple_0_2_2.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__clojure_java_time_0_3_2.xml b/.idea/libraries/Leiningen__clojure_java_time_0_3_2.xml new file mode 100644 index 0000000..8624dd1 --- /dev/null +++ b/.idea/libraries/Leiningen__clojure_java_time_0_3_2.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/project.clj b/project.clj index 490c9ec..1e18cc5 100644 --- a/project.clj +++ b/project.clj @@ -3,5 +3,5 @@ :url "http://example.com/FIXME" :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0" :url "https://www.eclipse.org/legal/epl-2.0/"} - :dependencies [[org.clojure/clojure "1.10.1"]] + :dependencies [[org.clojure/clojure "1.10.1"] [clojure.java-time "0.3.2"]] :repl-options {:init-ns yes-she-codes.core}) diff --git a/src/yes_she_codes/core.clj b/src/yes_she_codes/core.clj index b1b696b..e930d0f 100644 --- a/src/yes_she_codes/core.clj +++ b/src/yes_she_codes/core.clj @@ -1,11 +1,6 @@ (ns yes-she-codes.core - (:require [yes-she-codes.db :as l.db])) - -(def conta-clients {}) - -(def cartoes-clients {}) - -(def compras-clients {}) + (:require [yes-she-codes.db :as l.db]) + (:require [java-time :as t])) (defn novo-cliente [[nome cpf email]] @@ -13,37 +8,38 @@ client)) (defn novo-cartao - [[numero cvv validate limite cpf]] - (let [client (conj {:numero numero, :cvv cvv, :validate validate :limite limite :cpf cpf})] + [[numero cvv limite validate cpf]] + (let [client (conj {:numero numero, :cvv cvv, :limite limite :validate validate :cpf cpf})] client)) (defn nova-compra [[data valor estabelecimento categoria cartao]] - (let [client (conj {:data data, :valor valor, :estabelecimento estabelecimento :categoria categoria :cartao cartao})] - client)) + (let [formatData (t/format "dd/MM/yyyy" (t/local-date data))] + (let [client (conj {:data formatData , :valor valor, :estabelecimento estabelecimento :categoria categoria :cartao cartao})] + client))) (defn parametros-clients [] (let [clients (l.db/param-clientes)] - (assoc-in conta-clients [:clients] (vec (map novo-cliente clients))))) + (vec (map novo-cliente clients)))) (defn parametros-cartoes [] (let [cartoes (l.db/param-cartoes)] - (assoc-in cartoes-clients [:cartoes] (vec (map novo-cartao cartoes))))) + (vec (map novo-cartao cartoes)))) (defn parametros-compras [] (let [compras (l.db/param-compras)] - (assoc-in compras-clients [:compras] (vec (map nova-compra compras))))) + (vec (map nova-compra compras)))) (defn lista-clientes [] - (:clients (parametros-clients))) + (parametros-clients)) (defn lista-cartoes [] - (:cartoes (parametros-cartoes))) + (parametros-cartoes)) (defn lista-compras [] - (:compras (parametros-compras))) + (parametros-compras)) (defn total-gasto [compras] @@ -55,8 +51,9 @@ (defn filtra-compras-data [field value data] - (filter #(= (re-matches (re-pattern (str "[0-9]{4}-" value "-[0-9]{2}")) (field %)) (field %)) data)) - + (let [pattern (re-pattern (str "[0-9]{2}/" value "/[0-9]{4}")) + match-month #(re-matches pattern (field %))] + (filter #(= (match-month %) (field %)) data))) (defn buscar-por-mes [mes compras] @@ -76,13 +73,13 @@ (defn filtrar-intervalo-compras [minimo maximo compras] - (filter #(and (> (compare (% :data) minimo) 0) (< (compare (% :data) maximo) 0)) compras)) + (filter #(and (> (compare (% :valor) minimo) 0) (< (compare (% :valor) maximo) 0)) compras)) (defn total-compras-por-categoria [[categoria values]] {:categoria categoria - :R$ (total-gasto values)}) + :R$ (format "%.2f" (total-gasto values))}) (defn agrupar-por-categoria [compras] @@ -93,17 +90,19 @@ (println (lista-compras)) -(println (total-gasto (:compras (parametros-compras)))) +(println (lista-cartoes)) + +(println (total-gasto (parametros-compras))) -(println (buscar-por-mes "01" (:compras (parametros-compras)))) +(println (buscar-por-mes "01" (parametros-compras))) -(println "total-gasto-mes" (total-gasto-mes "01" "1234 1234 1234 1234" (:compras (parametros-compras)))) +(println "total-gasto-mes" (total-gasto-mes "01" "1234 1234 1234 1234" (parametros-compras))) -(println (agrupar-por-categoria (:compras (parametros-compras)))) +(println (agrupar-por-categoria (parametros-compras))) -(println (buscar-por-estabelecimento "Alura" (:compras (parametros-compras)))) +(println (buscar-por-estabelecimento "Alura" (parametros-compras))) -(println (filtrar-intervalo-compras "2022-01-01" "2022-03-01" (:compras (parametros-compras)))) +(println (filtrar-intervalo-compras 10 130 (parametros-compras))) diff --git a/src/yes_she_codes/db.clj b/src/yes_she_codes/db.clj index 76dfc2e..affd394 100644 --- a/src/yes_she_codes/db.clj +++ b/src/yes_she_codes/db.clj @@ -6,22 +6,21 @@ ["Hermione Granger" "666.777.888-99" "hermione.salvadora@hogwarts.com"] ["Daenerys Targaryen" "999.123.456-78" "mae.dos.dragoes@got.com"]]) -(def cartoes [["1234 1234 1234 1234" 111 1000 "12/2012" "000.111.222-33"] - ["4321 4321 4321 4321" 222 2000 "12/2012" "333.444.555-66"] - ["1598 1598 1598 1598" 333 3000 "12/2012" "666.777.888-99"] - ["6655 6655 6655 6655" 444 4000 "12/2012" "666.777.888-99"] - ["3939 3939 3939 3939" 555 5000 "12/2012" "999.123.456-78"]]) +(def cartoes [["1234 1234 1234 1234" 111 1000 "2012-12" "000.111.222-33"] + ["4321 4321 4321 4321" 222 2000 "2012-12" "333.444.555-66"] + ["1598 1598 1598 1598" 333 3000 "2012-12" "666.777.888-99"] + ["6655 6655 6655 6655" 444 4000 "2012-12" "666.777.888-99"] + ["3939 3939 3939 3939" 555 5000 "2012-12" "999.123.456-78"]]) (def compras [["2022-01-01" 129.90 "Outback" "Alimentacao" "1234 1234 1234 1234"] - ["2022-01-01" 129.90 "Dentista" "Saude" "1234 1234 1234 1234"] - ["2022-02-01" 129.90 "Cinema" "Lazer" "1234 1234 1234 1234"] - ["2022-04-01" 129.90 "Show" "Lazer" "1234 1234 1234 1234"] - ["2022-04-01" 129.90 "Posto de Gasolina" "Automovel" "1234 1234 1234 1234"] - ["2022-04-01" 129.90 "Ifood" "Alimentacao" "1234 1234 1234 1234"] - ["2022-04-01" 129.90 "Alura" "Educacao" "1234 1234 1234 1234"] - ["2022-04-01" 129.90 "Tok&Stock" "Casa" "1234 1234 1234 1234"] + ["2022-01-01" 80.90 "Dentista" "Saude" "1234 1234 1234 1234"] + ["2022-02-01" 40.90 "Cinema" "Lazer" "1234 1234 1234 1234"] + ["2022-04-01" 60.90 "Show" "Lazer" "1234 1234 1234 1234"] + ["2022-04-01" 10.90 "Ifood" "Alimentacao" "1234 1234 1234 1234"] + ["2022-04-01" 90.90 "Alura" "Educacao" "1234 1234 1234 1234"] + ["2022-04-01" 400.00 "Tok&Stock" "Casa" "1234 1234 1234 1234"] ["2022-04-01" 129.90 "Leroy Merlin" "Casa" "1234 1234 1234 1234"] - ["2022-04-01" 129.90 "Madero" "Alimentacao" "1234 1234 1234 1234"]]) + ["2022-04-01" 150.90 "Madero" "Alimentacao" "1234 1234 1234 1234"]]) (defn param-clientes [] clientes) diff --git a/yes-she-codes.iml b/yes-she-codes.iml index fcf1436..d6c2b94 100644 --- a/yes-she-codes.iml +++ b/yes-she-codes.iml @@ -17,7 +17,9 @@ + + From a1fad3b8f6182129605e9f0767a239e75a6cb0b3 Mon Sep 17 00:00:00 2001 From: emimuniz Date: Tue, 17 May 2022 13:41:41 -0300 Subject: [PATCH 09/13] refatorando --- .DS_Store | Bin 0 -> 6148 bytes .../Leiningen__org_clojure_data_csv_1_0_1.xml | 9 ++++ cartoes.csv | 6 +++ clientes.csv | 5 +++ compras.csv | 20 +++++++++ dados/cartoes.csv | 6 +++ dados/clientes.csv | 5 +++ dados/compras.csv | 20 +++++++++ project.clj | 5 ++- src/yes_she_codes/core.clj | 30 +++---------- src/yes_she_codes/leituracsv.clj | 40 ++++++++++++++++++ yes-she-codes.iml | 1 + 12 files changed, 122 insertions(+), 25 deletions(-) create mode 100644 .DS_Store create mode 100644 .idea/libraries/Leiningen__org_clojure_data_csv_1_0_1.xml create mode 100644 cartoes.csv create mode 100644 clientes.csv create mode 100644 compras.csv create mode 100644 dados/cartoes.csv create mode 100644 dados/clientes.csv create mode 100644 dados/compras.csv create mode 100644 src/yes_she_codes/leituracsv.clj diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..9ac88cd0d9acf4a3f00f87ec4e4151c863fac82f GIT binary patch literal 6148 zcmeHK!Ab)$5KY>uS&G<$P>;EI>tXj|y$DOygEwJC4=Qz+Eq0-9q`S3fUD?mjKk^U! z9cPlXv{b>9h@FASn@naB@^;B2j4|FB#U5h~#+U#_tT@noAvlh@Bo*sH zKt)Tjg&|6&sLhyh~YUopUIN5N4Cwq$GT(&n(%O3)in6pSkk&Qie8r5Iwd d6z_m)0l!28(6?9`1P=&Z1T+oQ5Cgx;z$cFrP|*MY literal 0 HcmV?d00001 diff --git a/.idea/libraries/Leiningen__org_clojure_data_csv_1_0_1.xml b/.idea/libraries/Leiningen__org_clojure_data_csv_1_0_1.xml new file mode 100644 index 0000000..fc1afa0 --- /dev/null +++ b/.idea/libraries/Leiningen__org_clojure_data_csv_1_0_1.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/cartoes.csv b/cartoes.csv new file mode 100644 index 0000000..5005205 --- /dev/null +++ b/cartoes.csv @@ -0,0 +1,6 @@ +NÚMERO,CVV,VALIDADE,LIMITE,CLIENTE +1234 1234 1234 1234,111,2023-01,1.000,000.111.222-33 +4321 4321 4321 4321,222,2024-02,2.000,333.444.555-66 +1598 1598 1598 1598,333,2021-03,3.000,666.777.888-99 +6655 6655 6655 6655,444,2025-04,4.000,666.777.888-99 +3939 3939 3939 3939,555,2026-05,5.000,999.123.456-78 \ No newline at end of file diff --git a/clientes.csv b/clientes.csv new file mode 100644 index 0000000..991313c --- /dev/null +++ b/clientes.csv @@ -0,0 +1,5 @@ +NOME,CPF,EMAIL +Feiticeira Escarlate,000.111.222-33,feiticeira.poderosa@vingadoras.com.br +Viúva Negra,333.444.555-66,viuva.casca.grossa@vingadoras.com.br +Hermione Granger,666.777.888-99,hermione.salvadora@hogwarts.com +Daenerys Targaryen,999.123.456-78,mae.dos.dragoes@got.com \ No newline at end of file diff --git a/compras.csv b/compras.csv new file mode 100644 index 0000000..8929a12 --- /dev/null +++ b/compras.csv @@ -0,0 +1,20 @@ +DATA,VALOR,ESTABELECIMENTO,CATEGORIA,CARTÃO +2022-01-01,129.90,Outback,Alimentação,1234 1234 1234 1234 +2022-01-02,260.00,Dentista,Saúde,1234 1234 1234 1234 +2022-02-01,20.00,Cinema,Lazer,1234 1234 1234 1234 +2022-01-10,150.00,Show,Lazer,4321 4321 4321 4321 +2022-02-10,289.99,Posto de gasolina,Automóvel,4321 4321 4321 4321 +2022-02-20,79.90,iFood,Alimentação,4321 4321 4321 4321 +2022-03-01,85.00,Alura,Educação,4321 4321 4321 4321 +2022-01-30,85.00,Alura,Educação,1598 1598 1598 1598 +2022-01-31,350.00,Tok&Stok,Casa,1598 1598 1598 1598 +2022-02-01,400.00,Leroy Merlin,Casa,1598 1598 1598 1598 +2022-03-01,50.00,Madero,Alimentação,6655 6655 6655 6655 +2022-03-01,70.00,Teatro,Lazer,6655 6655 6655 6655 +2022-03-04,250.00,Hospital,Saúde,6655 6655 6655 6655 +2022-04-10,130.00,Drogaria,Saúde,6655 6655 6655 6655 +2022-03-10,100.00,Show de pagode,Lazer,3939 3939 3939 3939 +2022-03-11,25.90,Dogão,Alimentação,3939 3939 3939 3939 +2022-03-12,215.87,Praia,Lazer,3939 3939 3939 3939 +2022-04-01,976.88,Oficina,Automóvel,3939 3939 3939 3939 +2022-04-10,85.00,Alura,Educação,3939 3939 3939 3939 \ No newline at end of file diff --git a/dados/cartoes.csv b/dados/cartoes.csv new file mode 100644 index 0000000..5005205 --- /dev/null +++ b/dados/cartoes.csv @@ -0,0 +1,6 @@ +NÚMERO,CVV,VALIDADE,LIMITE,CLIENTE +1234 1234 1234 1234,111,2023-01,1.000,000.111.222-33 +4321 4321 4321 4321,222,2024-02,2.000,333.444.555-66 +1598 1598 1598 1598,333,2021-03,3.000,666.777.888-99 +6655 6655 6655 6655,444,2025-04,4.000,666.777.888-99 +3939 3939 3939 3939,555,2026-05,5.000,999.123.456-78 \ No newline at end of file diff --git a/dados/clientes.csv b/dados/clientes.csv new file mode 100644 index 0000000..991313c --- /dev/null +++ b/dados/clientes.csv @@ -0,0 +1,5 @@ +NOME,CPF,EMAIL +Feiticeira Escarlate,000.111.222-33,feiticeira.poderosa@vingadoras.com.br +Viúva Negra,333.444.555-66,viuva.casca.grossa@vingadoras.com.br +Hermione Granger,666.777.888-99,hermione.salvadora@hogwarts.com +Daenerys Targaryen,999.123.456-78,mae.dos.dragoes@got.com \ No newline at end of file diff --git a/dados/compras.csv b/dados/compras.csv new file mode 100644 index 0000000..8929a12 --- /dev/null +++ b/dados/compras.csv @@ -0,0 +1,20 @@ +DATA,VALOR,ESTABELECIMENTO,CATEGORIA,CARTÃO +2022-01-01,129.90,Outback,Alimentação,1234 1234 1234 1234 +2022-01-02,260.00,Dentista,Saúde,1234 1234 1234 1234 +2022-02-01,20.00,Cinema,Lazer,1234 1234 1234 1234 +2022-01-10,150.00,Show,Lazer,4321 4321 4321 4321 +2022-02-10,289.99,Posto de gasolina,Automóvel,4321 4321 4321 4321 +2022-02-20,79.90,iFood,Alimentação,4321 4321 4321 4321 +2022-03-01,85.00,Alura,Educação,4321 4321 4321 4321 +2022-01-30,85.00,Alura,Educação,1598 1598 1598 1598 +2022-01-31,350.00,Tok&Stok,Casa,1598 1598 1598 1598 +2022-02-01,400.00,Leroy Merlin,Casa,1598 1598 1598 1598 +2022-03-01,50.00,Madero,Alimentação,6655 6655 6655 6655 +2022-03-01,70.00,Teatro,Lazer,6655 6655 6655 6655 +2022-03-04,250.00,Hospital,Saúde,6655 6655 6655 6655 +2022-04-10,130.00,Drogaria,Saúde,6655 6655 6655 6655 +2022-03-10,100.00,Show de pagode,Lazer,3939 3939 3939 3939 +2022-03-11,25.90,Dogão,Alimentação,3939 3939 3939 3939 +2022-03-12,215.87,Praia,Lazer,3939 3939 3939 3939 +2022-04-01,976.88,Oficina,Automóvel,3939 3939 3939 3939 +2022-04-10,85.00,Alura,Educação,3939 3939 3939 3939 \ No newline at end of file diff --git a/project.clj b/project.clj index 1e18cc5..1b9553b 100644 --- a/project.clj +++ b/project.clj @@ -3,5 +3,8 @@ :url "http://example.com/FIXME" :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0" :url "https://www.eclipse.org/legal/epl-2.0/"} - :dependencies [[org.clojure/clojure "1.10.1"] [clojure.java-time "0.3.2"]] + :dependencies [[org.clojure/clojure "1.10.1"] + [clojure.java-time "0.3.2"] + [org.clojure/data.csv "1.0.1"]] + :repl-options {:init-ns yes-she-codes.core}) diff --git a/src/yes_she_codes/core.clj b/src/yes_she_codes/core.clj index e930d0f..776619c 100644 --- a/src/yes_she_codes/core.clj +++ b/src/yes_she_codes/core.clj @@ -9,14 +9,14 @@ (defn novo-cartao [[numero cvv limite validate cpf]] - (let [client (conj {:numero numero, :cvv cvv, :limite limite :validate validate :cpf cpf})] - client)) + (let [formatValidate (t/format "MM/yyyy" (t/year-month validate))] + (let [client (conj {:numero numero, :cvv cvv, :limite limite :validate formatValidate :cpf cpf})] + client))) (defn nova-compra [[data valor estabelecimento categoria cartao]] (let [formatData (t/format "dd/MM/yyyy" (t/local-date data))] - (let [client (conj {:data formatData , :valor valor, :estabelecimento estabelecimento :categoria categoria :cartao cartao})] - client))) + (conj {:data formatData, :valor valor, :estabelecimento estabelecimento :categoria categoria :cartao cartao}))) (defn parametros-clients [] @@ -79,30 +79,12 @@ (defn total-compras-por-categoria [[categoria values]] {:categoria categoria - :R$ (format "%.2f" (total-gasto values))}) + :R$ (format "%.2f" (total-gasto values))}) (defn agrupar-por-categoria [compras] (map total-compras-por-categoria - (group-by :categoria compras))) - - - -(println (lista-compras)) - -(println (lista-cartoes)) - -(println (total-gasto (parametros-compras))) - -(println (buscar-por-mes "01" (parametros-compras))) - -(println "total-gasto-mes" (total-gasto-mes "01" "1234 1234 1234 1234" (parametros-compras))) - -(println (agrupar-por-categoria (parametros-compras))) - -(println (buscar-por-estabelecimento "Alura" (parametros-compras))) - -(println (filtrar-intervalo-compras 10 130 (parametros-compras))) + (group-by :categoria compras))) diff --git a/src/yes_she_codes/leituracsv.clj b/src/yes_she_codes/leituracsv.clj new file mode 100644 index 0000000..593c0a1 --- /dev/null +++ b/src/yes_she_codes/leituracsv.clj @@ -0,0 +1,40 @@ +(ns yes-she-codes.leituracsv) + +(defn novo-cliente + [nome cpf email] + (let [client (conj {:nome nome :cpf cpf :email email})] + client)) + +(defn novo-cartao + [numero cvv limite validate cpf] + (let [formatValidate (t/format "MM/yyyy" (t/year-month validate))] + (let [client (conj {:numero numero, :cvv cvv, :limite limite :validate formatValidate :cpf cpf})] + client))) + +(defn nova-compra + [data valor estabelecimento categoria cartao] + (let [formatData (t/format "dd/MM/yyyy" (t/local-date data))] + (conj {:data formatData, :valor valor, :estabelecimento estabelecimento :categoria categoria :cartao cartao}))) + +(defn processa-csv [caminho-arquivo funcao-mapeamento] + (->> (slurp caminho-arquivo) + clojure.string/split-lines + rest + (map #(clojure.string/split % #",")) + (mapv funcao-mapeamento))) + + +(defn lista-clientes [] + (processa-csv "dados/clientes.csv" (fn [[nome cpf email]] + (novo-cliente nome cpf email)))) + +(defn lista-cartoes [] + (processa-csv "dados/cartoes.csv" (fn [[numero cvv validade limite cliente]] + (novo-cartao numero cvv validade limite cliente)))) + + +(defn lista-compras [] + (processa-csv "dados/compras.csv" (fn [[data valor estabelecimento categoria cartao]] + (nova-compra data valor estabelecimento categoria cartao)))) + + diff --git a/yes-she-codes.iml b/yes-she-codes.iml index d6c2b94..f80a96f 100644 --- a/yes-she-codes.iml +++ b/yes-she-codes.iml @@ -23,6 +23,7 @@ + \ No newline at end of file From b2c346a9480e7f33844e51070ef54bc393e2bee0 Mon Sep 17 00:00:00 2001 From: emimuniz Date: Fri, 20 May 2022 11:09:13 -0300 Subject: [PATCH 10/13] criacao da lista atom e update inserir compra --- src/yes_she_codes/core.clj | 117 +++++++++++++++++----------------- src/yes_she_codes/db.clj | 10 +-- src/yes_she_codes/semana1.clj | 102 +++++++++++++++++++++++++++++ src/yes_she_codes/semana2.clj | 64 +++++++++++++++++++ 4 files changed, 230 insertions(+), 63 deletions(-) create mode 100644 src/yes_she_codes/semana1.clj create mode 100644 src/yes_she_codes/semana2.clj diff --git a/src/yes_she_codes/core.clj b/src/yes_she_codes/core.clj index 776619c..038da82 100644 --- a/src/yes_she_codes/core.clj +++ b/src/yes_she_codes/core.clj @@ -1,91 +1,92 @@ -(ns yes-she-codes.core - (:require [yes-she-codes.db :as l.db]) - (:require [java-time :as t])) +(ns yes-she-codes.core) -(defn novo-cliente - [[nome cpf email]] - (let [client (conj {:nome nome :cpf cpf :email email})] - client)) +(defn str->long [valor] + (Long/parseLong (clojure.string/replace valor #" " ""))) -(defn novo-cartao - [[numero cvv limite validate cpf]] - (let [formatValidate (t/format "MM/yyyy" (t/year-month validate))] - (let [client (conj {:numero numero, :cvv cvv, :limite limite :validate formatValidate :cpf cpf})] - client))) -(defn nova-compra - [[data valor estabelecimento categoria cartao]] - (let [formatData (t/format "dd/MM/yyyy" (t/local-date data))] - (conj {:data formatData, :valor valor, :estabelecimento estabelecimento :categoria categoria :cartao cartao}))) +(defn novo-cliente [nome cpf email] + {:nome nome + :cpf cpf + :email email}) -(defn parametros-clients [] - (let [clients (l.db/param-clientes)] - (vec (map novo-cliente clients)))) +(defn novo-cartao [numero cvv validade limite cliente] + {:numero (str->long numero) + :cvv (str->long cvv) + :email validade + :limite (bigdec limite) + :cliente cliente}) -(defn parametros-cartoes [] - (let [cartoes (l.db/param-cartoes)] - (vec (map novo-cartao cartoes)))) +(defn nova-compra [data valor estabelecimento categoria cartao] + {:data data + :valor (bigdec valor) + :estabelecimento estabelecimento + :categoria categoria + :cartao (str->long cartao)}) + + +(defn processa-csv [caminho-arquivo funcao-mapeamento] + (->> (slurp caminho-arquivo) + clojure.string/split-lines + rest + (map #(clojure.string/split % #",")) + (mapv funcao-mapeamento))) -(defn parametros-compras [] - (let [compras (l.db/param-compras)] - (vec (map nova-compra compras)))) (defn lista-clientes [] - (parametros-clients)) + (processa-csv "dados/clientes.csv" (fn [[nome cpf email]] + (novo-cliente nome cpf email)))) (defn lista-cartoes [] - (parametros-cartoes)) + (processa-csv "dados/cartoes.csv" (fn [[numero cvv validade limite cliente]] + (novo-cartao numero cvv validade limite cliente)))) + (defn lista-compras [] - (parametros-compras)) + (processa-csv "dados/compras.csv" (fn [[data valor estabelecimento categoria cartao]] + (nova-compra data valor estabelecimento categoria cartao)))) (defn total-gasto [compras] - (reduce + (map #(:valor %) compras))) + (reduce + (map :valor compras))) + + +(defn mes-da-data [data] + (second (re-matches #"\d{4}-(\d{2})-\d{2}" data))) -(defn filtra-compras - [field value data] - (filter #(= value (field %)) data)) -(defn filtra-compras-data - [field value data] - (let [pattern (re-pattern (str "[0-9]{2}/" value "/[0-9]{4}")) - match-month #(re-matches pattern (field %))] - (filter #(= (match-month %) (field %)) data))) +(defn filtra-compras [predicado compras] + (vec (filter predicado compras))) -(defn buscar-por-mes - [mes compras] - (filtra-compras-data :data mes compras)) +(defn filtra-compras-no-mes [mes compras] + (filtra-compras #(= mes (mes-da-data (:data %))) + compras)) -(defn total-gasto-mes - [mes cartao compras] - (let [client-cartao (filtra-compras :cartao cartao compras)] - (let [data (filtra-compras-data :data mes client-cartao)] - (total-gasto data)))) +(defn filtra-compras-no-estabelecimento [estabelecimento compras] + (filtra-compras #(= estabelecimento (:estabelecimento %)) + compras)) -(defn buscar-por-estabelecimento - [estabelecimento compras] - (filtra-compras :estabelecimento estabelecimento compras)) -(defn filtrar-intervalo-compras - [minimo maximo compras] - (filter #(and (> (compare (% :valor) minimo) 0) (< (compare (% :valor) maximo) 0)) compras)) +;(defn total-gasto-no-mes [mes compras] +; (total-gasto (filtra-compras-no-mes mes compras))) -(defn total-compras-por-categoria - [[categoria values]] - {:categoria categoria - :R$ (format "%.2f" (total-gasto values))}) +(def total-gasto-no-mes-com-composition (comp total-gasto filtra-compras-no-mes)) +(defn filtra-compras-por-valor [minimo maximo compras] + (filtra-compras #(and (>= (:valor %) minimo) + (<= (:valor %) maximo)) + compras)) -(defn agrupar-por-categoria [compras] - (map total-compras-por-categoria - (group-by :categoria compras))) +(defn agrupa-gastos-por-categoria [compras] + (vec (map (fn [[categoria compras-da-categoria]] + {:categoria categoria + :total-gasto (total-gasto compras-da-categoria)}) + (group-by :categoria compras)))) diff --git a/src/yes_she_codes/db.clj b/src/yes_she_codes/db.clj index affd394..cb766a8 100644 --- a/src/yes_she_codes/db.clj +++ b/src/yes_she_codes/db.clj @@ -15,12 +15,12 @@ (def compras [["2022-01-01" 129.90 "Outback" "Alimentacao" "1234 1234 1234 1234"] ["2022-01-01" 80.90 "Dentista" "Saude" "1234 1234 1234 1234"] ["2022-02-01" 40.90 "Cinema" "Lazer" "1234 1234 1234 1234"] - ["2022-04-01" 60.90 "Show" "Lazer" "1234 1234 1234 1234"] - ["2022-04-01" 10.90 "Ifood" "Alimentacao" "1234 1234 1234 1234"] + ["2022-04-01" 60.90 "Show" "Lazer" "3939 3939 3939 3939"] + ["2022-04-01" 10.90 "Ifood" "Alimentacao" "3939 3939 3939 3939"] ["2022-04-01" 90.90 "Alura" "Educacao" "1234 1234 1234 1234"] - ["2022-04-01" 400.00 "Tok&Stock" "Casa" "1234 1234 1234 1234"] - ["2022-04-01" 129.90 "Leroy Merlin" "Casa" "1234 1234 1234 1234"] - ["2022-04-01" 150.90 "Madero" "Alimentacao" "1234 1234 1234 1234"]]) + ["2022-04-01" 400.00 "Tok&Stock" "Casa" "6655 6655 6655 6655"] + ["2022-04-01" 129.90 "Leroy Merlin" "Casa" "6655 6655 6655 6655"] + ["2022-04-01" 150.90 "Madero" "Alimentacao" "3939 3939 3939 3939"]]) (defn param-clientes [] clientes) diff --git a/src/yes_she_codes/semana1.clj b/src/yes_she_codes/semana1.clj new file mode 100644 index 0000000..c161feb --- /dev/null +++ b/src/yes_she_codes/semana1.clj @@ -0,0 +1,102 @@ +(ns yes-she-codes.semana1 + (:require [yes-she-codes.db :as l.db] + [java-time :as t])) + +(defn novo-cliente + [[nome cpf email]] + (let [client (conj {:nome nome :cpf cpf :email email})] + client)) + +(defn novo-cartao + [[numero cvv limite validate cpf]] + (let [formatValidate (t/format "MM/yyyy" (t/year-month validate)) + client (conj {:numero numero, :cvv cvv, :limite limite :validate formatValidate :cpf cpf})] + client)) + +(defn nova-compra + [[data valor estabelecimento categoria cartao]] + (let [formatData (t/format "dd/MM/yyyy" (t/local-date data))] + (conj {:data formatData, :valor valor, :estabelecimento estabelecimento :categoria categoria :cartao cartao}))) + + +(defn parametros-clients [] + (let [clients (l.db/param-clientes)] + (vec (map novo-cliente clients)))) + + +(defn parametros-cartoes [] + (let [cartoes (l.db/param-cartoes)] + (vec (map novo-cartao cartoes)))) + +(defn parametros-compras [] + (let [compras (l.db/param-compras)] + (vec (map nova-compra compras)))) + +(defn lista-clientes [] + (parametros-clients)) + +(defn lista-cartoes [] + (parametros-cartoes)) + +(defn lista-compras [] + (parametros-compras)) + +(defn total-gasto [compras] + (reduce + (map #(:valor %) compras))) + +(defn filtra-data + [field value data] + (filter #(= value (field %)) data)) + +(defn filtra-compras-data + [field value data] + (let [pattern (re-pattern (str "[0-9]{2}/" value "/[0-9]{4}")) + match-month #(re-matches pattern (field %))] + (filter #(= (match-month %) (field %)) data))) + +(defn buscar-por-mes + [mes compras] + (filtra-compras-data :data mes compras)) + + +(defn total-gasto-mes + [mes cartao compras] + (let [client-cartao (filtra-data :cartao cartao compras) + data (filtra-compras-data :data mes client-cartao)] + (total-gasto data))) + + +(defn buscar-por-estabelecimento + [estabelecimento compras] + (filtra-data :estabelecimento estabelecimento compras)) + +(defn filtrar-intervalo-compras + [minimo maximo compras] + (filter #(and (> (compare (% :valor) minimo) 0) (< (compare (% :valor) maximo) 0)) compras)) + + +(defn total-compras-por-categoria + [[categoria values]] + {:categoria categoria + :R$ (format "%.2f" (total-gasto values))}) + + +(defn agrupar-por-categoria [compras] + (map total-compras-por-categoria + (group-by :categoria compras))) + +(defn agregate-cartao [cpf] + {:cartao (into {} (filtra-data :cpf cpf (lista-cartoes)))}) + +(defn agregate-compras [cartao] + (vec (filtra-data :cartao cartao (lista-compras)))) + +(defn agregate-client-cpf [cpf] + (let [client (into {} (filtra-data :cpf cpf (lista-clientes))) + cartao (agregate-cartao cpf) + compras (agregate-compras (:numero (:cartao cartao))) + conta (assoc-in cartao [:cartao :compras] compras)] + (conj client conta))) + +(defn agregate-clients [clients] + (map (fn [client] (agregate-client-cpf (:cpf client))) clients)) diff --git a/src/yes_she_codes/semana2.clj b/src/yes_she_codes/semana2.clj new file mode 100644 index 0000000..321fee6 --- /dev/null +++ b/src/yes_she_codes/semana2.clj @@ -0,0 +1,64 @@ +(ns yes-she-codes.semana2 + (:require [yes-she-codes.semana1 :as y.s1] + [yes-she-codes.db :as y.db] + [java-time :as t])) + +(def repositorio-de-compras (atom [])) + +(defrecord Compra [^Long id ^String data ^BigDecimal valor ^String estabelecimento ^String categoria ^Long Cartao]) + +(defn nova-compra + [[data valor estabelecimento categoria cartao] index] + (let [formatData (t/format "dd/MM/yyyy" (t/local-date data))] + (Compra. index formatData valor estabelecimento categoria cartao))) + +(defn parametros-compras [] + (let [compras (y.db/param-compras)] + (map-indexed (fn [index compra] (nova-compra compra (inc index))) compras))) + +(defn proximo-id [entidades] + (if-not (empty? entidades) + (+ 1 (apply max (map :id entidades))) 1)) + +(defn insere-compra [record compras] + (let [id (proximo-id compras)] + (conj compras (assoc record :id id)))) + +;entender porque ao enviar um objeto vazio nao fica o namespace +(defn insere-compra! [record compras-atom] + (let [compras (insere-compra record compras-atom)] + (swap! repositorio-de-compras conj compras))) + +(defn lista-compra! [repositorio-de-compras] + (deref repositorio-de-compras)) + +(defn pesquisa-compra-por-id [id compras] + (y.s1/filtra-data :id id compras)) + +(defn pesquisa-compra-por-id! [id compras] + (y.s1/filtra-data :id id compras)) + +(defn exclui-compra [id compras] + (remove #(= (:id %) id) compras)) + +;entender o motivo de nao funcionar com o filtro-daata +;porque foi transformado em uma lista +(defn exclui-compra! [id compras] + (swap! compras (remove #(= (:id %) id) compras))) + +(println (parametros-compras)) +(println (Compra. nil "12/12/1221" 121.0 "Extra" "Alimentacao" "218928192819")) +(println "Inserindo nova compra" + (insere-compra (Compra. nil "12/12/1221" 121.0 "Extra" "Alimentacao" "218928192819") (parametros-compras))) + +(println (insere-compra! (Compra. nil "12/12/1221" 121.0 "Extra" "Alimentacao" "218928192819") (parametros-compras))) +(println (insere-compra! (Compra. nil "12/12/1221" 121.0 "Extra" "Alimentacao" "218928192819") (parametros-compras))) + +(println @repositorio-de-compras) +(println "lista compra" (lista-compra! repositorio-de-compras)) +(println (pesquisa-compra-por-id 1 (parametros-compras))) +(println "Atom" (pesquisa-compra-por-id! 1 @repositorio-de-compras)) +(println "Exclui" (exclui-compra 1 (parametros-compras))) +;(println "Exclui" (exclui-compra! 1 repositorio-de-compras)) +;(println (insere-compra! (Compra. nil "12/12/1221" 121.0 "Extra" "Alimentacao" "218928192819") {})) +;(println @repositorio-de-compras) \ No newline at end of file From 1b2d1e5a4b30bac68d985515fddd3f2d8102b57d Mon Sep 17 00:00:00 2001 From: emimuniz Date: Tue, 24 May 2022 18:19:59 -0300 Subject: [PATCH 11/13] desafios semana2 --- cartoes.csv | 6 ---- clientes.csv | 5 --- compras.csv | 20 ----------- src/yes_she_codes/semana1.clj | 2 +- src/yes_she_codes/semana2.clj | 66 ++++++++++++++++------------------- src/yes_she_codes/semana3.clj | 4 +++ 6 files changed, 36 insertions(+), 67 deletions(-) delete mode 100644 cartoes.csv delete mode 100644 clientes.csv delete mode 100644 compras.csv create mode 100644 src/yes_she_codes/semana3.clj diff --git a/cartoes.csv b/cartoes.csv deleted file mode 100644 index 5005205..0000000 --- a/cartoes.csv +++ /dev/null @@ -1,6 +0,0 @@ -NÚMERO,CVV,VALIDADE,LIMITE,CLIENTE -1234 1234 1234 1234,111,2023-01,1.000,000.111.222-33 -4321 4321 4321 4321,222,2024-02,2.000,333.444.555-66 -1598 1598 1598 1598,333,2021-03,3.000,666.777.888-99 -6655 6655 6655 6655,444,2025-04,4.000,666.777.888-99 -3939 3939 3939 3939,555,2026-05,5.000,999.123.456-78 \ No newline at end of file diff --git a/clientes.csv b/clientes.csv deleted file mode 100644 index 991313c..0000000 --- a/clientes.csv +++ /dev/null @@ -1,5 +0,0 @@ -NOME,CPF,EMAIL -Feiticeira Escarlate,000.111.222-33,feiticeira.poderosa@vingadoras.com.br -Viúva Negra,333.444.555-66,viuva.casca.grossa@vingadoras.com.br -Hermione Granger,666.777.888-99,hermione.salvadora@hogwarts.com -Daenerys Targaryen,999.123.456-78,mae.dos.dragoes@got.com \ No newline at end of file diff --git a/compras.csv b/compras.csv deleted file mode 100644 index 8929a12..0000000 --- a/compras.csv +++ /dev/null @@ -1,20 +0,0 @@ -DATA,VALOR,ESTABELECIMENTO,CATEGORIA,CARTÃO -2022-01-01,129.90,Outback,Alimentação,1234 1234 1234 1234 -2022-01-02,260.00,Dentista,Saúde,1234 1234 1234 1234 -2022-02-01,20.00,Cinema,Lazer,1234 1234 1234 1234 -2022-01-10,150.00,Show,Lazer,4321 4321 4321 4321 -2022-02-10,289.99,Posto de gasolina,Automóvel,4321 4321 4321 4321 -2022-02-20,79.90,iFood,Alimentação,4321 4321 4321 4321 -2022-03-01,85.00,Alura,Educação,4321 4321 4321 4321 -2022-01-30,85.00,Alura,Educação,1598 1598 1598 1598 -2022-01-31,350.00,Tok&Stok,Casa,1598 1598 1598 1598 -2022-02-01,400.00,Leroy Merlin,Casa,1598 1598 1598 1598 -2022-03-01,50.00,Madero,Alimentação,6655 6655 6655 6655 -2022-03-01,70.00,Teatro,Lazer,6655 6655 6655 6655 -2022-03-04,250.00,Hospital,Saúde,6655 6655 6655 6655 -2022-04-10,130.00,Drogaria,Saúde,6655 6655 6655 6655 -2022-03-10,100.00,Show de pagode,Lazer,3939 3939 3939 3939 -2022-03-11,25.90,Dogão,Alimentação,3939 3939 3939 3939 -2022-03-12,215.87,Praia,Lazer,3939 3939 3939 3939 -2022-04-01,976.88,Oficina,Automóvel,3939 3939 3939 3939 -2022-04-10,85.00,Alura,Educação,3939 3939 3939 3939 \ No newline at end of file diff --git a/src/yes_she_codes/semana1.clj b/src/yes_she_codes/semana1.clj index c161feb..9e143be 100644 --- a/src/yes_she_codes/semana1.clj +++ b/src/yes_she_codes/semana1.clj @@ -99,4 +99,4 @@ (conj client conta))) (defn agregate-clients [clients] - (map (fn [client] (agregate-client-cpf (:cpf client))) clients)) + (map (fn [client] (agregate-client-cpf (:cpf client))) clients)) \ No newline at end of file diff --git a/src/yes_she_codes/semana2.clj b/src/yes_she_codes/semana2.clj index 321fee6..ce7bff1 100644 --- a/src/yes_she_codes/semana2.clj +++ b/src/yes_she_codes/semana2.clj @@ -5,29 +5,33 @@ (def repositorio-de-compras (atom [])) -(defrecord Compra [^Long id ^String data ^BigDecimal valor ^String estabelecimento ^String categoria ^Long Cartao]) - -(defn nova-compra - [[data valor estabelecimento categoria cartao] index] - (let [formatData (t/format "dd/MM/yyyy" (t/local-date data))] - (Compra. index formatData valor estabelecimento categoria cartao))) - -(defn parametros-compras [] - (let [compras (y.db/param-compras)] - (map-indexed (fn [index compra] (nova-compra compra (inc index))) compras))) +(defrecord Compra [^Long id ^String data ^BigDecimal valor ^String estabelecimento ^String categoria ^Long cartao]) (defn proximo-id [entidades] (if-not (empty? entidades) (+ 1 (apply max (map :id entidades))) 1)) -(defn insere-compra [record compras] +(defn insere-compra [compras record] (let [id (proximo-id compras)] (conj compras (assoc record :id id)))) -;entender porque ao enviar um objeto vazio nao fica o namespace -(defn insere-compra! [record compras-atom] - (let [compras (insere-compra record compras-atom)] - (swap! repositorio-de-compras conj compras))) +(defn insere-compra! [compra] + (swap! repositorio-de-compras insere-compra compra)) + +(defn carrega-compra + [compras] + (->> compras + (map (fn [[data valor estabelecimento categoria cartao]] + (let [formatData (t/format "dd/MM/yyyy" (t/local-date data))] + (Compra. nil formatData valor estabelecimento categoria cartao)))) + (map insere-compra!))) + +(defn parametros-compras [] + (let [compras (y.db/param-compras)] + (flatten (carrega-compra compras)))) + +(defn lista-compra [] + (parametros-compras)) (defn lista-compra! [repositorio-de-compras] (deref repositorio-de-compras)) @@ -41,24 +45,16 @@ (defn exclui-compra [id compras] (remove #(= (:id %) id) compras)) -;entender o motivo de nao funcionar com o filtro-daata -;porque foi transformado em uma lista (defn exclui-compra! [id compras] - (swap! compras (remove #(= (:id %) id) compras))) - -(println (parametros-compras)) -(println (Compra. nil "12/12/1221" 121.0 "Extra" "Alimentacao" "218928192819")) -(println "Inserindo nova compra" - (insere-compra (Compra. nil "12/12/1221" 121.0 "Extra" "Alimentacao" "218928192819") (parametros-compras))) - -(println (insere-compra! (Compra. nil "12/12/1221" 121.0 "Extra" "Alimentacao" "218928192819") (parametros-compras))) -(println (insere-compra! (Compra. nil "12/12/1221" 121.0 "Extra" "Alimentacao" "218928192819") (parametros-compras))) - -(println @repositorio-de-compras) -(println "lista compra" (lista-compra! repositorio-de-compras)) -(println (pesquisa-compra-por-id 1 (parametros-compras))) -(println "Atom" (pesquisa-compra-por-id! 1 @repositorio-de-compras)) -(println "Exclui" (exclui-compra 1 (parametros-compras))) -;(println "Exclui" (exclui-compra! 1 repositorio-de-compras)) -;(println (insere-compra! (Compra. nil "12/12/1221" 121.0 "Extra" "Alimentacao" "218928192819") {})) -;(println @repositorio-de-compras) \ No newline at end of file + (swap! compras (remove #(= (:id %) id) compras) compras)) + +(println "Parametros Compras" (parametros-compras)) +;(println (Compra. nil "12/12/1221" 121.0 "Extra" "Alimentacao" "218928192819")) + +(println "Repositorio de compras" @repositorio-de-compras) +(println "lista compra" (lista-compra)) +(println "lista compra!" (lista-compra! repositorio-de-compras)) +(println "lista id" (pesquisa-compra-por-id 1 (parametros-compras))) +(println "Atom" (pesquisa-compra-por-id! 1 (deref repositorio-de-compras))) +;(println "Exclui" (exclui-compra 1 (parametros-compras))) +;(println "Exclui!" (exclui-compra! 1 repositorio-de-compras)) diff --git a/src/yes_she_codes/semana3.clj b/src/yes_she_codes/semana3.clj new file mode 100644 index 0000000..10450a5 --- /dev/null +++ b/src/yes_she_codes/semana3.clj @@ -0,0 +1,4 @@ +(ns yes-she-codes.semana3) + +(def Cliente + {:id s/Num, :nome s/Str}) From b93f6c762b8e68da53af5f482a72b0ab9e1bf527 Mon Sep 17 00:00:00 2001 From: emimuniz Date: Fri, 27 May 2022 12:02:46 -0300 Subject: [PATCH 12/13] Desafios da Semana 3 --- .idea/compiler.xml | 11 ++ .idea/jarRepositories.xml | 25 +++ .../Leiningen__prismatic_schema_1_1_12.xml | 9 + .../Maven__clj_tuple_clj_tuple_0_2_2.xml | 13 ++ .../libraries/Maven__cljdev_cljdev_0_10_2.xml | 13 ++ ...jure_java_time_clojure_java_time_0_3_2.xml | 13 ++ .../Maven__org_clojure_clojure_1_10_1.xml | 13 ++ ...n__org_clojure_core_specs_alpha_0_2_44.xml | 13 ++ .../Maven__org_clojure_data_csv_1_0_1.xml | 13 ++ .../Maven__org_clojure_spec_alpha_0_2_176.xml | 13 ++ .../Maven__prismatic_schema_1_1_12.xml | 13 ++ .idea/misc.xml | 7 + project.clj | 5 +- src/yes_she_codes/core.clj | 187 +++++++++--------- src/yes_she_codes/csv/leitor_csv.clj | 52 +++++ .../yes_she_codes/dados}/cartoes.csv | 0 .../yes_she_codes/dados}/clientes.csv | 0 .../yes_she_codes/dados}/compras.csv | 0 src/yes_she_codes/dominio/cartao.clj | 3 + src/yes_she_codes/dominio/cliente.clj | 5 + src/yes_she_codes/dominio/compra.clj | 61 ++++++ .../persistencia/banco_de_dados_atomos.clj | 118 +++++++++++ src/yes_she_codes/semana2.clj | 7 +- src/yes_she_codes/semana3.clj | 69 ++++++- src/yes_she_codes/util.clj | 33 ++++ test/yes_she_codes/core_test.clj | 2 +- test/yes_she_codes/semana3_test.clj | 97 +++++++++ yes-she-codes.iml | 25 ++- 28 files changed, 703 insertions(+), 117 deletions(-) create mode 100644 .idea/jarRepositories.xml create mode 100644 .idea/libraries/Leiningen__prismatic_schema_1_1_12.xml create mode 100644 .idea/libraries/Maven__clj_tuple_clj_tuple_0_2_2.xml create mode 100644 .idea/libraries/Maven__cljdev_cljdev_0_10_2.xml create mode 100644 .idea/libraries/Maven__clojure_java_time_clojure_java_time_0_3_2.xml create mode 100644 .idea/libraries/Maven__org_clojure_clojure_1_10_1.xml create mode 100644 .idea/libraries/Maven__org_clojure_core_specs_alpha_0_2_44.xml create mode 100644 .idea/libraries/Maven__org_clojure_data_csv_1_0_1.xml create mode 100644 .idea/libraries/Maven__org_clojure_spec_alpha_0_2_176.xml create mode 100644 .idea/libraries/Maven__prismatic_schema_1_1_12.xml create mode 100644 src/yes_she_codes/csv/leitor_csv.clj rename {dados => src/yes_she_codes/dados}/cartoes.csv (100%) rename {dados => src/yes_she_codes/dados}/clientes.csv (100%) rename {dados => src/yes_she_codes/dados}/compras.csv (100%) create mode 100644 src/yes_she_codes/dominio/cartao.clj create mode 100644 src/yes_she_codes/dominio/cliente.clj create mode 100644 src/yes_she_codes/dominio/compra.clj create mode 100644 src/yes_she_codes/persistencia/banco_de_dados_atomos.clj create mode 100644 src/yes_she_codes/util.clj create mode 100644 test/yes_she_codes/semana3_test.clj diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 9deeaef..c230597 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -14,5 +14,16 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..eff57b1 --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__prismatic_schema_1_1_12.xml b/.idea/libraries/Leiningen__prismatic_schema_1_1_12.xml new file mode 100644 index 0000000..5d02782 --- /dev/null +++ b/.idea/libraries/Leiningen__prismatic_schema_1_1_12.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__clj_tuple_clj_tuple_0_2_2.xml b/.idea/libraries/Maven__clj_tuple_clj_tuple_0_2_2.xml new file mode 100644 index 0000000..48f5c4a --- /dev/null +++ b/.idea/libraries/Maven__clj_tuple_clj_tuple_0_2_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__cljdev_cljdev_0_10_2.xml b/.idea/libraries/Maven__cljdev_cljdev_0_10_2.xml new file mode 100644 index 0000000..dc8631a --- /dev/null +++ b/.idea/libraries/Maven__cljdev_cljdev_0_10_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__clojure_java_time_clojure_java_time_0_3_2.xml b/.idea/libraries/Maven__clojure_java_time_clojure_java_time_0_3_2.xml new file mode 100644 index 0000000..fbebe96 --- /dev/null +++ b/.idea/libraries/Maven__clojure_java_time_clojure_java_time_0_3_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_clojure_clojure_1_10_1.xml b/.idea/libraries/Maven__org_clojure_clojure_1_10_1.xml new file mode 100644 index 0000000..7a1cc20 --- /dev/null +++ b/.idea/libraries/Maven__org_clojure_clojure_1_10_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_clojure_core_specs_alpha_0_2_44.xml b/.idea/libraries/Maven__org_clojure_core_specs_alpha_0_2_44.xml new file mode 100644 index 0000000..ad517a6 --- /dev/null +++ b/.idea/libraries/Maven__org_clojure_core_specs_alpha_0_2_44.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_clojure_data_csv_1_0_1.xml b/.idea/libraries/Maven__org_clojure_data_csv_1_0_1.xml new file mode 100644 index 0000000..e16a3ae --- /dev/null +++ b/.idea/libraries/Maven__org_clojure_data_csv_1_0_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_clojure_spec_alpha_0_2_176.xml b/.idea/libraries/Maven__org_clojure_spec_alpha_0_2_176.xml new file mode 100644 index 0000000..d793899 --- /dev/null +++ b/.idea/libraries/Maven__org_clojure_spec_alpha_0_2_176.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__prismatic_schema_1_1_12.xml b/.idea/libraries/Maven__prismatic_schema_1_1_12.xml new file mode 100644 index 0000000..7349ea0 --- /dev/null +++ b/.idea/libraries/Maven__prismatic_schema_1_1_12.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 7d294aa..3b2f5ed 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -7,6 +7,13 @@ + + + diff --git a/project.clj b/project.clj index 1b9553b..ae30710 100644 --- a/project.clj +++ b/project.clj @@ -5,6 +5,7 @@ :url "https://www.eclipse.org/legal/epl-2.0/"} :dependencies [[org.clojure/clojure "1.10.1"] [clojure.java-time "0.3.2"] - [org.clojure/data.csv "1.0.1"]] + [org.clojure/data.csv "1.0.1"] + [prismatic/schema "1.1.12"]] - :repl-options {:init-ns yes-she-codes.core}) + :repl-options {:init-ns yes-she-codes.core}) \ No newline at end of file diff --git a/src/yes_she_codes/core.clj b/src/yes_she_codes/core.clj index 038da82..eed8404 100644 --- a/src/yes_she_codes/core.clj +++ b/src/yes_she_codes/core.clj @@ -1,95 +1,92 @@ -(ns yes-she-codes.core) - -(defn str->long [valor] - (Long/parseLong (clojure.string/replace valor #" " ""))) - - -(defn novo-cliente [nome cpf email] - {:nome nome - :cpf cpf - :email email}) - - -(defn novo-cartao [numero cvv validade limite cliente] - {:numero (str->long numero) - :cvv (str->long cvv) - :email validade - :limite (bigdec limite) - :cliente cliente}) - - -(defn nova-compra [data valor estabelecimento categoria cartao] - {:data data - :valor (bigdec valor) - :estabelecimento estabelecimento - :categoria categoria - :cartao (str->long cartao)}) - - -(defn processa-csv [caminho-arquivo funcao-mapeamento] - (->> (slurp caminho-arquivo) - clojure.string/split-lines - rest - (map #(clojure.string/split % #",")) - (mapv funcao-mapeamento))) - - -(defn lista-clientes [] - (processa-csv "dados/clientes.csv" (fn [[nome cpf email]] - (novo-cliente nome cpf email)))) - -(defn lista-cartoes [] - (processa-csv "dados/cartoes.csv" (fn [[numero cvv validade limite cliente]] - (novo-cartao numero cvv validade limite cliente)))) - - -(defn lista-compras [] - (processa-csv "dados/compras.csv" (fn [[data valor estabelecimento categoria cartao]] - (nova-compra data valor estabelecimento categoria cartao)))) - - -(defn total-gasto [compras] - (reduce + (map :valor compras))) - - -(defn mes-da-data [data] - (second (re-matches #"\d{4}-(\d{2})-\d{2}" data))) - - -(defn filtra-compras [predicado compras] - (vec (filter predicado compras))) - - -(defn filtra-compras-no-mes [mes compras] - (filtra-compras #(= mes (mes-da-data (:data %))) - compras)) - - -(defn filtra-compras-no-estabelecimento [estabelecimento compras] - (filtra-compras #(= estabelecimento (:estabelecimento %)) - compras)) - - -;(defn total-gasto-no-mes [mes compras] -; (total-gasto (filtra-compras-no-mes mes compras))) - - -(def total-gasto-no-mes-com-composition (comp total-gasto filtra-compras-no-mes)) - -(defn filtra-compras-por-valor [minimo maximo compras] - (filtra-compras #(and (>= (:valor %) minimo) - (<= (:valor %) maximo)) - compras)) - - -(defn agrupa-gastos-por-categoria [compras] - (vec (map (fn [[categoria compras-da-categoria]] - {:categoria categoria - :total-gasto (total-gasto compras-da-categoria)}) - (group-by :categoria compras)))) - - - - - - +(ns yes-she-codes.core + (:require [yes-she-codes.util :as y.util] + [yes-she-codes.persistencia.banco-de-dados-atomos :as y.bd] + [yes-she-codes.dominio.cliente :as y.cliente] + [yes-she-codes.dominio.cartao :as y.cartao] + [yes-she-codes.dominio.compra :as y.compra] + [java-time :as time]) + (:use [clojure pprint])) + +; Faz carga dos CSVs no banco (átomos) +(y.bd/carrega-banco-de-dados!) + + +; TESTE DAS LÓGICAS DE CLIENTES +(println "========= LISTAGEM DE CLIENTES ==========") +(pprint (y.bd/lista-clientes!)) + +(println "\n========= INSERE CLIENTE DE ID 5 ==========") +(y.bd/insere-cliente! (y.cliente/->Cliente nil + "Mulher Maravilha" + "321.321.321-32" + "maisfortedaliga@ligadajustica.dc.br")) + +(println "\n========= PESQUISA CLIENTE DE ID 5 ==========") +(pprint (y.bd/pesquisa-cliente-por-id! 5)) + +(println "\n========= EXCLUIR CLIENTE DE ID 5 ==========") +(y.bd/exclui-cliente! 5) + + +; TESTE DAS LÓGICAS DE CARTÕES +(println "\n========= LISTAGEM DE CARTÕES ==========") +(pprint (y.bd/lista-cartoes!)) + +(println "\n========= INSERE CARTÃO DE ID 6 ==========") +(y.bd/insere-cartao! (y.cartao/->Cartao nil + 1111222233334444 + 999 + (time/year-month "2025-05") + 1500M + "000.111.222-33")) + +(println "\n========= PESQUISA CARTÃO DE ID 6 ==========") +(pprint (y.bd/pesquisa-cartao-por-id! 6)) + +(println "\n========= EXCLUIR CLIENTE DE ID 5 ==========") +(y.bd/exclui-cartao! 6) + + +; TESTE DAS LÓGICAS DE COMPRAS +(println "\n========= LISTAGEM DE COMPRAS ==========") +(pprint (y.bd/lista-compras!)) + +(println "\n========= INSERE COMPRA DE ID 20 ==========") +(y.bd/insere-compra! (y.compra/->Compra nil + (time/local-date "2022-05-20") + 123.99M + "Carrefour" + "Alimentação" + 3939393939393939)) + +(println "\n========= PESQUISA COMPRA DE ID 20 ==========") +(pprint (y.bd/pesquisa-compra-por-id! 20)) + +(println "\n========= EXCLUIR COMPRA DE ID 20 ==========") +(y.bd/exclui-compra! 20) + +(println "\n========= INSPECIONANDO COMPRAS DA Viúva Negra (cartão 4321432143214321)") +(def compras-da-viuva (y.compra/filtra-compras-por-cartao 4321432143214321 + (y.bd/lista-compras!))) + +(println "\n========= TOTAL GASTO EM TODAS AS COMPRAS =========") +(pprint (y.compra/total-gasto compras-da-viuva)) + +(println "\n========= TOTAL GASTO EM FEVEREIRO =========") +(pprint (y.compra/total-gasto-no-mes 2 compras-da-viuva)) +(pprint (y.compra/filtra-compras-no-mes 2 compras-da-viuva)) + +(println "\n========= COMPRAS AGRUPADAS POR CATEGORIA =========") +(pprint (y.compra/agrupa-gastos-por-categoria compras-da-viuva)) + + +(println "\n========= TENTA INSERIR COMPRA INVÁLIDA ==========") +(try + (y.bd/insere-compra! (y.compra/->Compra nil + (time/local-date "2022-05-20") + -100M + "Carrefour" + "Alimentação" + 3939393939393939)) + (catch Exception e + (println "Validação deu certo"))) \ No newline at end of file diff --git a/src/yes_she_codes/csv/leitor_csv.clj b/src/yes_she_codes/csv/leitor_csv.clj new file mode 100644 index 0000000..8693ee6 --- /dev/null +++ b/src/yes_she_codes/csv/leitor_csv.clj @@ -0,0 +1,52 @@ +(ns yes-she-codes.csv.leitor-csv + (:require [java-time :as time] + [yes-she-codes.util :as y.util] + [yes-she-codes.dominio.compra :as y.compra] + [yes-she-codes.dominio.cliente :as y.cliente] + [yes-she-codes.dominio.cartao :as y.cartao])) + +(defn- converte-valores-na-linha [funcoes-de-conversao linha] + (map #(%1 %2) funcoes-de-conversao linha)) + + +(defn- processa-csv [caminho-arquivo] + (->> (slurp caminho-arquivo) + clojure.string/split-lines + rest + (map #(clojure.string/split % #",")))) + +(def csv->cartao [y.util/str->long + y.util/str->long + time/year-month + bigdec + identity]) + +(def csv->compra [time/local-date + bigdec + identity + identity + y.util/str->long]) + + +(def cria-cliente-sem-id (partial y.cliente/->Cliente nil)) +(def cria-cartao-sem-id (partial y.cartao/->Cartao nil)) +(def cria-compra-sem-id (partial y.compra/->Compra nil)) + + +(defn processa-arquivo-de-clientes! [] + (->> (processa-csv "dados/clientes.csv") + (map #(apply cria-cliente-sem-id %)))) + + +(defn processa-arquivo-de-cartoes! [] + (->> (processa-csv "dados/cartoes.csv") + (map #(converte-valores-na-linha csv->cartao %)) + (map #(apply cria-cartao-sem-id %)) + vec)) + +(defn processa-arquivo-de-compras! [] + (->> (processa-csv "dados/compras.csv") + (map #(converte-valores-na-linha csv->compra %)) + (map #(apply cria-compra-sem-id %)) + vec)) + diff --git a/dados/cartoes.csv b/src/yes_she_codes/dados/cartoes.csv similarity index 100% rename from dados/cartoes.csv rename to src/yes_she_codes/dados/cartoes.csv diff --git a/dados/clientes.csv b/src/yes_she_codes/dados/clientes.csv similarity index 100% rename from dados/clientes.csv rename to src/yes_she_codes/dados/clientes.csv diff --git a/dados/compras.csv b/src/yes_she_codes/dados/compras.csv similarity index 100% rename from dados/compras.csv rename to src/yes_she_codes/dados/compras.csv diff --git a/src/yes_she_codes/dominio/cartao.clj b/src/yes_she_codes/dominio/cartao.clj new file mode 100644 index 0000000..7fe44c6 --- /dev/null +++ b/src/yes_she_codes/dominio/cartao.clj @@ -0,0 +1,3 @@ +(ns yes-she-codes.dominio.cartao) + +(defrecord Cartao [id numero cvv validade limite cliente]) \ No newline at end of file diff --git a/src/yes_she_codes/dominio/cliente.clj b/src/yes_she_codes/dominio/cliente.clj new file mode 100644 index 0000000..e1b2bb6 --- /dev/null +++ b/src/yes_she_codes/dominio/cliente.clj @@ -0,0 +1,5 @@ +(ns yes-she-codes.dominio.cliente) + +(defrecord Cliente [id nome cpf email]) + + diff --git a/src/yes_she_codes/dominio/compra.clj b/src/yes_she_codes/dominio/compra.clj new file mode 100644 index 0000000..e0d0fda --- /dev/null +++ b/src/yes_she_codes/dominio/compra.clj @@ -0,0 +1,61 @@ +(ns yes-she-codes.dominio.compra + (:require [yes-she-codes.util :as util] + [java-time :as time])) + + +(defrecord Compra [id + data + valor + estabelecimento + categoria + cartao]) + + +(defn compra-valida? [compra] + (let [valida-data (time/after? (time/local-date) (get compra :data)) + valida-valor (and (number? (get compra :valor)) (>= (get compra :valor) 0)) + valida-estabelecimento (>= (count (get compra :estabelecimento)) 2) + categoria (get compra :categoria) + valida-categoria (or (= "Alimentação" categoria) + (= "Automóvel" categoria) + (= "Casa" categoria) + (= "Educação" categoria) + (= "Lazer" categoria) + (= "Saúde" categoria))] + + (and valida-data valida-valor valida-estabelecimento valida-categoria))) + + +(defn filtra-compras [predicado compras] + (vec (filter predicado compras))) + + +(defn filtra-compras-no-mes [mes compras] + (filtra-compras #(= mes (util/mes-da-data (:data %))) compras)) + + +(defn filtra-compras-no-estabelecimento [estabelecimento compras] + (filtra-compras #(= estabelecimento (:estabelecimento %)) compras)) + + +(defn filtra-compras-por-valor [minimo maximo compras] + (filtra-compras #(and (>= (:valor %) minimo) + (<= (:valor %) maximo)) + compras)) + +(defn filtra-compras-por-cartao [cartao compras] + (filtra-compras #(= cartao (:cartao %)) compras)) + + +(defn total-gasto [compras] + (reduce + (map :valor compras))) + + +(def total-gasto-no-mes (comp total-gasto filtra-compras-no-mes)) + + +(defn agrupa-gastos-por-categoria [compras] + (vec (map (fn [[categoria compras-da-categoria]] + {:categoria categoria + :total-gasto (total-gasto compras-da-categoria)}) + (group-by :categoria compras)))) \ No newline at end of file diff --git a/src/yes_she_codes/persistencia/banco_de_dados_atomos.clj b/src/yes_she_codes/persistencia/banco_de_dados_atomos.clj new file mode 100644 index 0000000..973025e --- /dev/null +++ b/src/yes_she_codes/persistencia/banco_de_dados_atomos.clj @@ -0,0 +1,118 @@ +(ns yes-she-codes.persistencia.banco-de-dados-atomos + (:require [yes-she-codes.dominio.compra :as y.compra] + [yes-she-codes.util :as y.util] + [yes-she-codes.csv.leitor-csv :as y.csv])) + +;;;;; Átomos que persistem cada uma das entidades +(def repositorio-de-clientes (atom [])) +(def repositorio-de-cartoes (atom [])) +(def repositorio-de-compras (atom [])) + + + +;;;;; Lógicas genéricas de persistência de entidades +(defn- insere-entidade [entidades entidade] + (let [id-da-entidade (y.util/proximo-id entidades) + entidade-com-id (assoc entidade :id id-da-entidade)] + (conj entidades entidade-com-id))) + + +(defn- pesquisa-por-id [entidades id] + (first (filter #(= id (:id %)) entidades))) + + +(defn- deve-remover? [id entidade] + (->> entidade + :id + (= id))) + + +(defn- exclui-entidade [entidades id] + (remove #(deve-remover? id %) entidades)) + + +(defn- carrega-registros! [insere registros] + (doseq [registro registros] + (insere registro))) + + + +;;;;; Lógicas de persistência de COMPRAS +(defn insere-compra! [compra] + (if (y.compra/compra-valida? compra) + (swap! repositorio-de-compras insere-entidade compra) + (throw (ex-info "Compra inválida!" {:compra compra})))) + + +(defn- carrega-compras-no-atomo! [] + (carrega-registros! insere-compra! (y.csv/processa-arquivo-de-compras!))) + + +(defn exclui-compra! [id] + (swap! repositorio-de-compras exclui-entidade id)) + + +(defn pesquisa-compra-por-id! [id] + (pesquisa-por-id @repositorio-de-compras id)) + + +(defn lista-compras! [] + @repositorio-de-compras) + + + +;;;;; Lógicas de persistência de CLIENTES +(defn insere-cliente! [cliente] + (swap! repositorio-de-clientes insere-entidade cliente)) + + +(defn- carrega-clientes-no-atomo! [] + (carrega-registros! insere-cliente! (y.csv/processa-arquivo-de-clientes!))) + + +(defn pesquisa-cliente-por-id! [id] + (pesquisa-por-id @repositorio-de-clientes id)) + + +(defn lista-clientes! [] + @repositorio-de-clientes) + + +(defn exclui-cliente! [id] + (swap! repositorio-de-clientes exclui-entidade id)) + + + +;;;;; Lógicas de persistência de CARTÕES +(defn insere-cartao! [cartao] + (swap! repositorio-de-cartoes insere-entidade cartao)) + + +(defn- carrega-cartoes-no-atomo! [] + (carrega-registros! insere-cartao! (y.csv/processa-arquivo-de-cartoes!))) + + +(defn pesquisa-cartao-por-id! [id] + (pesquisa-por-id @repositorio-de-cartoes id)) + + +(defn lista-cartoes! [] + @repositorio-de-cartoes) + + +(defn exclui-cartao! [id] + (swap! repositorio-de-cartoes exclui-entidade id)) + + + +;;;;; Limpa e carrega registros nos átomos +(defn limpa-banco-de-dados! [] + (doseq [atomo [repositorio-de-clientes repositorio-de-cartoes repositorio-de-compras]] + (swap! atomo (fn [entidades] + [])))) + +(defn carrega-banco-de-dados! [] + (limpa-banco-de-dados!) + (carrega-clientes-no-atomo!) + (carrega-cartoes-no-atomo!) + (carrega-compras-no-atomo!)) \ No newline at end of file diff --git a/src/yes_she_codes/semana2.clj b/src/yes_she_codes/semana2.clj index ce7bff1..2731a71 100644 --- a/src/yes_she_codes/semana2.clj +++ b/src/yes_she_codes/semana2.clj @@ -15,6 +15,7 @@ (let [id (proximo-id compras)] (conj compras (assoc record :id id)))) + (defn insere-compra! [compra] (swap! repositorio-de-compras insere-compra compra)) @@ -28,7 +29,7 @@ (defn parametros-compras [] (let [compras (y.db/param-compras)] - (flatten (carrega-compra compras)))) + (carrega-compra compras))) (defn lista-compra [] (parametros-compras)) @@ -37,7 +38,7 @@ (deref repositorio-de-compras)) (defn pesquisa-compra-por-id [id compras] - (y.s1/filtra-data :id id compras)) + (y.s1/filtra-data :id id (first compras))) (defn pesquisa-compra-por-id! [id compras] (y.s1/filtra-data :id id compras)) @@ -48,7 +49,7 @@ (defn exclui-compra! [id compras] (swap! compras (remove #(= (:id %) id) compras) compras)) -(println "Parametros Compras" (parametros-compras)) +;(println "Parametros Compras" (parametros-compras)) ;(println (Compra. nil "12/12/1221" 121.0 "Extra" "Alimentacao" "218928192819")) (println "Repositorio de compras" @repositorio-de-compras) diff --git a/src/yes_she_codes/semana3.clj b/src/yes_she_codes/semana3.clj index 10450a5..9d306f5 100644 --- a/src/yes_she_codes/semana3.clj +++ b/src/yes_she_codes/semana3.clj @@ -1,4 +1,67 @@ -(ns yes-she-codes.semana3) +(ns yes-she-codes.semana3 + (:use clojure.pprint) + (:require [schema.core :as s] + [clojure.string :as str])) + +(s/set-fn-validation! true) + +(defn validate-email [email] + (re-matches #".+\@.+\..+" email)) + +(defn validate-cpf [cpf] + (re-matches #"\d{3}[\.]?\d{3}[\.]?\d{3}[-]?\d{2}" cpf)) + +(defn validate-string [string] + (>= (count string) 2)) + +(def ClienteSchema + "Schema de um paciente" + {:nome (s/constrained s/Str validate-string) , + :cpf (s/constrained s/Str validate-cpf), + :email (s/constrained s/Str validate-email)}) + +(defn validate-numero [numero] + (and (pos? numero) (< numero 1e+16))) + +(defn validate-cvv [cvv] + (and (pos? cvv) (< cvv 999))) + +(defn validate-data-cartao [validate] + (re-matches #"\d{2}[/]?\d{4}" validate)) + +(def CartaoSchema + {:numero (s/constrained s/Num validate-numero) , + :cvv (s/constrained s/Num validate-cvv) , + :validate (s/constrained s/Str validate-data-cartao), + :limite (s/constrained s/Num pos?), + :cliente (s/constrained s/Str validate-cpf)}) + +(defn validate-data [data] + (re-matches #"[0-9]{2}[/]?[0-9]{2}[/]?[0-9]{4}" data)) + +(defn validate-categoria [categoria] + (let [lista-categoria ["Alimentação", "Automovel", "Casa", "Educação", "Lazer", "Saúde"]] + (str/includes? lista-categoria categoria))) + +(def CompraSchema + {:data (s/constrained s/Str validate-data) , + :valor (s/constrained s/Num pos?) , + :estabelecimento (s/constrained s/Str validate-string), + :categoria (s/constrained s/Str validate-categoria), + :cartao (s/constrained s/Num validate-numero)}) + + +(s/defn novo-cliente :- ClienteSchema + [nome :- s/Str, cpf :- s/Str, email :- s/Str] + {:nome nome, :cpf cpf, :email email}) + +(s/defn novo-cartao :- CartaoSchema + [numero :- s/Num, cvv :- s/Num, validate :- s/Str, limite :- s/Num, cliente :- s/Str] + {:numero numero, :cvv cvv, :validate validate, :limite limite, :cliente cliente}) + +(s/defn nova-compra :- CompraSchema + [data :- s/Str, valor :- s/Num, estabelecimento :- s/Str, categoria :- s/Str, cartao :- s/Num] + {:data data, :valor valor, :estabelecimento estabelecimento, :categoria categoria :cartao cartao}) + + -(def Cliente - {:id s/Num, :nome s/Str}) diff --git a/src/yes_she_codes/util.clj b/src/yes_she_codes/util.clj new file mode 100644 index 0000000..bb888a7 --- /dev/null +++ b/src/yes_she_codes/util.clj @@ -0,0 +1,33 @@ +(ns yes-she-codes.util + (:require [java-time :as time])) + +(defn str->long [valor] + (Long/parseLong (clojure.string/replace valor #" " ""))) + + +(defprotocol ExtratorDeMes + (mes-da-data [data] + "Dada uma data, extrai o valor do mês como um inteiro de 1 a 12")) + +(extend-type java.lang.String ExtratorDeMes + (mes-da-data [data] + (->> data + (re-matches #"\d{4}-(\d{2})-\d{2}") + second + Long/valueOf))) + +(extend-type java.time.LocalDate ExtratorDeMes + (mes-da-data [data] + (.getValue (time/month data)))) + +(defn data-valida? [data] + (re-matches #"\d{4}-(\d{2})-\d{2}" data)) + + +(defn proximo-id [entidades] + (if-not (empty? entidades) + (+ 1 (apply max (map :id entidades))) + 1)) + + + diff --git a/test/yes_she_codes/core_test.clj b/test/yes_she_codes/core_test.clj index 754f4b2..e959f21 100644 --- a/test/yes_she_codes/core_test.clj +++ b/test/yes_she_codes/core_test.clj @@ -13,6 +13,6 @@ (is (= [{:nome "Emillyn", :cpf "1829182918", :email "testando"}]) (cria-map "Emillyn" "1829182918" "testando"))) (testing "verifica se cria um novo cartao corretamente" - (is (= {:numero 12819289182918, :cvv 233, :validate "2022/12" :limite 1000.0 :cpf "238923928"} ) + (is (= {:numero 12819289182918, :cvv 233, :validate "13/2222" :limite 1000.0 :cpf "238923928"} ) (cria-cartao 12819289182918 233 "2022/12" 1000.0 "238923928")))) diff --git a/test/yes_she_codes/semana3_test.clj b/test/yes_she_codes/semana3_test.clj new file mode 100644 index 0000000..594188d --- /dev/null +++ b/test/yes_she_codes/semana3_test.clj @@ -0,0 +1,97 @@ +(ns yes-she-codes.semana3-test + (:require [clojure.test :refer :all] + [yes-she-codes.semana3 :refer :all] + [schema.core :as s])) + +(s/set-fn-validation! true) + +(deftest validate-schema + (testing "verifica cliente schema valido" + (is (= (s/validate ClienteSchema + {:nome "Viúva Negra", :cpf "333.444.555-66", :email "viuva.casca.grossa@vingadoras.com.br"}) + {:nome "Viúva Negra", :cpf "333.444.555-66", :email "viuva.casca.grossa@vingadoras.com.br"}))) + + + (testing "verifica cliente schema invalido (nome não informado)" + (is (thrown? clojure.lang.ExceptionInfo + (s/validate ClienteSchema + {:nome nil, :cpf "333.444.555-66", :email "viuva.casca.grossa@vingadoras.com.br"})))) + + (testing "verifica cliente schema invalido (cpf invalido)" + (is (thrown? clojure.lang.ExceptionInfo + (s/validate ClienteSchema + {:nome "Viúva Negra", :cpf "333.444.555213", :email "viuva.casca.grossa@vingadoras.com.br"})))) + + (testing "verifica cliente schema invalido (email invalido) " + (is (thrown? clojure.lang.ExceptionInfo + (s/validate ClienteSchema + {:nome "Viúva Negra", :cpf "333.444.555-66", :email "viuva.casca.grossa"})))) + + (testing "verifica cartao schema valido" + (is (= (s/validate CartaoSchema + {:numero 4321432143214321, :cvv 222, :validate "02/2222", :limite 2000.0, :cliente "333.444.555-66"}) + {:numero 4321432143214321, :cvv 222, :validate "02/2222", :limite 2000.0, :cliente "333.444.555-66"}))) + + (testing "verifica cartao schema invalido (numero cartao negativo)" + (is (thrown? clojure.lang.ExceptionInfo + (s/validate CartaoSchema + {:numero -10, :cvv 222, :validate "02/2222", :limite 2000.0, :cliente "333.444.555-66"})))) + + (testing "verifica cartao schema invalido (cvv maior que 999)" + (is (thrown? clojure.lang.ExceptionInfo + (s/validate CartaoSchema + {:numero 4321432143214321, :cvv 1000, :validate "02/2222", :limite 2000.0, :cliente "333.444.555-66"})))) + + (testing "verifica cartao schema invalido (cvv maior que 999)" + (is (thrown? clojure.lang.ExceptionInfo + (s/validate CartaoSchema + {:numero 4321432143214321, :cvv 1000, :validate "02/2222", :limite 2000.0, :cliente "333.444.555-66"})))) + + (testing "verifica cartao schema invalido (validate incorreta)" + (is (thrown? clojure.lang.ExceptionInfo + (s/validate CartaoSchema + {:numero 4321432143214321, :cvv 1000, :validate "2002/22", :limite 2000.0, :cliente "333.444.555-66"})))) + + (testing "verifica cartao schema invalido (limite negativo)" + (is (thrown? clojure.lang.ExceptionInfo + (s/validate CartaoSchema + {:numero 4321432143214321, :cvv 1000, :validate "2002/22", :limite 2000.0, :cliente "333.444.555-66"})))) + + (testing "verifica cartao schema invalido (cpf cliente incorreto)" + (is (thrown? clojure.lang.ExceptionInfo + (s/validate CartaoSchema + {:numero 4321432143214321, :cvv 1000, :validate "2002/22", :limite 20000, :cliente "333.444"})))) + + (testing "verifica compra schema valido" + (is (= (s/validate CompraSchema + {:data "22/05/2009", :valor 100M, :estabelecimento "Amazon", :categoria "Casa" :cartao 1234123412341234}) + {:data "22/05/2009", :valor 100M, :estabelecimento "Amazon", :categoria "Casa" :cartao 1234123412341234}))) + + (testing "verifica compra schema invalido (data incorreta)" + (is (thrown? clojure.lang.ExceptionInfo + (s/validate CompraSchema + {:data "200", :valor 100M, :estabelecimento "Amazon", :categoria "Casa" :cartao 1234123412341234})))) + + (testing "verifica compra schema invalido (valor negativo)" + (is (thrown? clojure.lang.ExceptionInfo + (s/validate CompraSchema + {:data "22/05/2009", :valor -200, :estabelecimento "Amazon", :categoria "Casa" :cartao 1234123412341234})))) + + (testing "verifica compra schema invalido (estabelecimento string vazia)" + (is (thrown? clojure.lang.ExceptionInfo + (s/validate CompraSchema + {:data "22/05/2009", :valor 100M, :estabelecimento "", :categoria "Casa" :cartao 1234123412341234})))) + + (testing "verifica compra schema invalido (estabelecimento string vazia)" + (is (thrown? clojure.lang.ExceptionInfo + (s/validate CompraSchema + {:data "22/05/2009", :valor 100M, :estabelecimento "Amazon", :categoria "Mato" :cartao 1234123412341234})))) + + (testing "verifica compra schema invalido (numero cartao incorreto)" + (is (thrown? clojure.lang.ExceptionInfo + (s/validate CompraSchema + {:data "22/05/2009", :valor 100M, :estabelecimento "", :categoria "Casa" :cartao 12}))))) + + + + diff --git a/yes-she-codes.iml b/yes-she-codes.iml index f80a96f..94a9408 100644 --- a/yes-she-codes.iml +++ b/yes-she-codes.iml @@ -1,29 +1,28 @@ - + - + - - + - + - - - - - - - - + + + + + + + + \ No newline at end of file From 53159d6a0dfefdc6e49e92368c13968782d195e3 Mon Sep 17 00:00:00 2001 From: Emillyn Silva Date: Mon, 13 Jun 2022 00:45:17 -0300 Subject: [PATCH 13/13] refactor code --- .DS_Store | Bin 6148 -> 6148 bytes ..._com_datomic_datomic_lucene_core_3_3_0.xml | 9 ++ ...ngen__com_datomic_datomic_pro_1_0_6397.xml | 9 ++ ...omic_memcache_asg_java_client_1_1_0_33.xml | 9 ++ ...ngen__com_datomic_query_support_0_8_28.xml | 9 ++ ..._com_google_code_findbugs_jsr305_3_0_2.xml | 9 ++ ...rorprone_error_prone_annotations_2_7_1.xml | 9 ++ ...__com_google_guava_failureaccess_1_0_1.xml | 9 ++ ...gen__com_google_guava_guava_31_0_1_jre.xml | 9 ++ ...9_0_empty_to_avoid_conflict_with_guava.xml | 9 ++ ...m_google_j2objc_j2objc_annotations_1_3.xml | 9 ++ .../Leiningen__com_h2database_h2_1_3_171.xml | 9 ++ .../Leiningen__commons_beanutils_1_9_4.xml | 9 ++ .../Leiningen__commons_codec_1_15.xml | 9 ++ .../Leiningen__commons_collections_3_2_2.xml | 9 ++ ...en__io_netty_netty_buffer_4_1_73_Final.xml | 9 ++ ...gen__io_netty_netty_codec_4_1_73_Final.xml | 9 ++ ...io_netty_netty_codec_http_4_1_73_Final.xml | 9 ++ ...o_netty_netty_codec_socks_4_1_73_Final.xml | 9 ++ ...en__io_netty_netty_common_4_1_73_Final.xml | 9 ++ ...n__io_netty_netty_handler_4_1_73_Final.xml | 9 ++ ...netty_netty_handler_proxy_4_1_73_Final.xml | 9 ++ ...__io_netty_netty_resolver_4_1_73_Final.xml | 9 ++ ...ty_netty_tcnative_classes_2_0_46_Final.xml | 9 ++ ..._io_netty_netty_transport_4_1_73_Final.xml | 9 ++ ...y_transport_classes_epoll_4_1_73_Final.xml | 9 ++ ..._transport_classes_kqueue_4_1_73_Final.xml | 9 ++ ...native_epoll_4_1_73_Final_linux_x86_64.xml | 9 ++ ..._native_kqueue_4_1_73_Final_osx_x86_64.xml | 9 ++ ...nsport_native_unix_common_4_1_73_Final.xml | 9 ++ ...n__jakarta_json_jakarta_json_api_1_1_6.xml | 9 ++ ...apache_activemq_artemis_commons_2_19_1.xml | 9 ++ ...he_activemq_artemis_core_client_2_19_1.xml | 9 ++ ...apache_httpcomponents_httpclient_4_5_9.xml | 9 ++ ..._apache_httpcomponents_httpcore_4_4_11.xml | 9 ++ ..._org_apache_johnzon_johnzon_core_0_9_5.xml | 9 ++ ..._org_apache_tomcat_tomcat_jdbc_7_0_109.xml | 9 ++ ..._org_apache_tomcat_tomcat_juli_7_0_109.xml | 9 ++ ...g_checkerframework_checker_qual_3_12_0.xml | 9 ++ ...iningen__org_clojure_tools_cli_1_0_206.xml | 9 ++ ...odehaus_janino_commons_compiler_3_0_12.xml | 9 ++ ...aus_janino_commons_compiler_jdk_3_0_12.xml | 9 ++ ...Leiningen__org_fressian_fressian_0_6_6.xml | 9 ++ ...boss_logging_jboss_logging_3_4_2_Final.xml | 9 ++ ...ingen__org_slf4j_jcl_over_slf4j_1_7_32.xml | 9 ++ ...iningen__org_slf4j_jul_to_slf4j_1_7_32.xml | 9 ++ ...gen__org_slf4j_log4j_over_slf4j_1_7_32.xml | 9 ++ .../Leiningen__org_slf4j_slf4j_api_1_7_32.xml | 9 ++ .../Leiningen__org_slf4j_slf4j_nop_1_7_36.xml | 9 ++ .idea/misc.xml | 2 +- .../yes_she_codes/dados => dados}/cartoes.csv | 2 +- .../dados => dados}/clientes.csv | 0 .../yes_she_codes/dados => dados}/compras.csv | 0 project.clj | 13 +- src/.DS_Store | Bin 0 -> 6148 bytes src/yes_she_codes/core.clj | 112 ++++---------- src/yes_she_codes/csv/leitor_csv.clj | 25 ++- src/yes_she_codes/db.clj | 30 ---- src/yes_she_codes/db/atom.clj | 7 + src/yes_she_codes/db/cartao.clj | 9 ++ src/yes_she_codes/db/cliente.clj | 10 ++ src/yes_she_codes/db/compra.clj | 13 ++ src/yes_she_codes/db/datomic.clj | 46 ++++++ src/yes_she_codes/dominio/cartao.clj | 28 +++- src/yes_she_codes/dominio/cliente.clj | 19 ++- src/yes_she_codes/dominio/compra.clj | 87 ++++------- src/yes_she_codes/leituracsv.clj | 40 ----- src/yes_she_codes/logic.clj | 48 ++++++ .../persistencia/banco_de_dados_atomos.clj | 118 --------------- src/yes_she_codes/persistencia/cartao.clj | 21 +++ src/yes_she_codes/persistencia/cliente.clj | 37 +++++ src/yes_she_codes/persistencia/compra.clj | 114 ++++++++++++++ src/yes_she_codes/persistencia/datomic.clj | 142 ++++++++++++++++++ src/yes_she_codes/semana1.clj | 102 ------------- src/yes_she_codes/semana2.clj | 61 -------- src/yes_she_codes/semana3.clj | 67 --------- src/yes_she_codes/util.clj | 33 ---- test/yes_she_codes/core_test.clj | 2 +- 78 files changed, 1007 insertions(+), 613 deletions(-) create mode 100644 .idea/libraries/Leiningen__com_datomic_datomic_lucene_core_3_3_0.xml create mode 100644 .idea/libraries/Leiningen__com_datomic_datomic_pro_1_0_6397.xml create mode 100644 .idea/libraries/Leiningen__com_datomic_memcache_asg_java_client_1_1_0_33.xml create mode 100644 .idea/libraries/Leiningen__com_datomic_query_support_0_8_28.xml create mode 100644 .idea/libraries/Leiningen__com_google_code_findbugs_jsr305_3_0_2.xml create mode 100644 .idea/libraries/Leiningen__com_google_errorprone_error_prone_annotations_2_7_1.xml create mode 100644 .idea/libraries/Leiningen__com_google_guava_failureaccess_1_0_1.xml create mode 100644 .idea/libraries/Leiningen__com_google_guava_guava_31_0_1_jre.xml create mode 100644 .idea/libraries/Leiningen__com_google_guava_listenablefuture_9999_0_empty_to_avoid_conflict_with_guava.xml create mode 100644 .idea/libraries/Leiningen__com_google_j2objc_j2objc_annotations_1_3.xml create mode 100644 .idea/libraries/Leiningen__com_h2database_h2_1_3_171.xml create mode 100644 .idea/libraries/Leiningen__commons_beanutils_1_9_4.xml create mode 100644 .idea/libraries/Leiningen__commons_codec_1_15.xml create mode 100644 .idea/libraries/Leiningen__commons_collections_3_2_2.xml create mode 100644 .idea/libraries/Leiningen__io_netty_netty_buffer_4_1_73_Final.xml create mode 100644 .idea/libraries/Leiningen__io_netty_netty_codec_4_1_73_Final.xml create mode 100644 .idea/libraries/Leiningen__io_netty_netty_codec_http_4_1_73_Final.xml create mode 100644 .idea/libraries/Leiningen__io_netty_netty_codec_socks_4_1_73_Final.xml create mode 100644 .idea/libraries/Leiningen__io_netty_netty_common_4_1_73_Final.xml create mode 100644 .idea/libraries/Leiningen__io_netty_netty_handler_4_1_73_Final.xml create mode 100644 .idea/libraries/Leiningen__io_netty_netty_handler_proxy_4_1_73_Final.xml create mode 100644 .idea/libraries/Leiningen__io_netty_netty_resolver_4_1_73_Final.xml create mode 100644 .idea/libraries/Leiningen__io_netty_netty_tcnative_classes_2_0_46_Final.xml create mode 100644 .idea/libraries/Leiningen__io_netty_netty_transport_4_1_73_Final.xml create mode 100644 .idea/libraries/Leiningen__io_netty_netty_transport_classes_epoll_4_1_73_Final.xml create mode 100644 .idea/libraries/Leiningen__io_netty_netty_transport_classes_kqueue_4_1_73_Final.xml create mode 100644 .idea/libraries/Leiningen__io_netty_netty_transport_native_epoll_4_1_73_Final_linux_x86_64.xml create mode 100644 .idea/libraries/Leiningen__io_netty_netty_transport_native_kqueue_4_1_73_Final_osx_x86_64.xml create mode 100644 .idea/libraries/Leiningen__io_netty_netty_transport_native_unix_common_4_1_73_Final.xml create mode 100644 .idea/libraries/Leiningen__jakarta_json_jakarta_json_api_1_1_6.xml create mode 100644 .idea/libraries/Leiningen__org_apache_activemq_artemis_commons_2_19_1.xml create mode 100644 .idea/libraries/Leiningen__org_apache_activemq_artemis_core_client_2_19_1.xml create mode 100644 .idea/libraries/Leiningen__org_apache_httpcomponents_httpclient_4_5_9.xml create mode 100644 .idea/libraries/Leiningen__org_apache_httpcomponents_httpcore_4_4_11.xml create mode 100644 .idea/libraries/Leiningen__org_apache_johnzon_johnzon_core_0_9_5.xml create mode 100644 .idea/libraries/Leiningen__org_apache_tomcat_tomcat_jdbc_7_0_109.xml create mode 100644 .idea/libraries/Leiningen__org_apache_tomcat_tomcat_juli_7_0_109.xml create mode 100644 .idea/libraries/Leiningen__org_checkerframework_checker_qual_3_12_0.xml create mode 100644 .idea/libraries/Leiningen__org_clojure_tools_cli_1_0_206.xml create mode 100644 .idea/libraries/Leiningen__org_codehaus_janino_commons_compiler_3_0_12.xml create mode 100644 .idea/libraries/Leiningen__org_codehaus_janino_commons_compiler_jdk_3_0_12.xml create mode 100644 .idea/libraries/Leiningen__org_fressian_fressian_0_6_6.xml create mode 100644 .idea/libraries/Leiningen__org_jboss_logging_jboss_logging_3_4_2_Final.xml create mode 100644 .idea/libraries/Leiningen__org_slf4j_jcl_over_slf4j_1_7_32.xml create mode 100644 .idea/libraries/Leiningen__org_slf4j_jul_to_slf4j_1_7_32.xml create mode 100644 .idea/libraries/Leiningen__org_slf4j_log4j_over_slf4j_1_7_32.xml create mode 100644 .idea/libraries/Leiningen__org_slf4j_slf4j_api_1_7_32.xml create mode 100644 .idea/libraries/Leiningen__org_slf4j_slf4j_nop_1_7_36.xml rename {src/yes_she_codes/dados => dados}/cartoes.csv (80%) rename {src/yes_she_codes/dados => dados}/clientes.csv (100%) rename {src/yes_she_codes/dados => dados}/compras.csv (100%) create mode 100644 src/.DS_Store delete mode 100644 src/yes_she_codes/db.clj create mode 100644 src/yes_she_codes/db/atom.clj create mode 100644 src/yes_she_codes/db/cartao.clj create mode 100644 src/yes_she_codes/db/cliente.clj create mode 100644 src/yes_she_codes/db/compra.clj create mode 100644 src/yes_she_codes/db/datomic.clj delete mode 100644 src/yes_she_codes/leituracsv.clj create mode 100644 src/yes_she_codes/logic.clj delete mode 100644 src/yes_she_codes/persistencia/banco_de_dados_atomos.clj create mode 100644 src/yes_she_codes/persistencia/cartao.clj create mode 100644 src/yes_she_codes/persistencia/cliente.clj create mode 100644 src/yes_she_codes/persistencia/compra.clj create mode 100644 src/yes_she_codes/persistencia/datomic.clj delete mode 100644 src/yes_she_codes/semana1.clj delete mode 100644 src/yes_she_codes/semana2.clj delete mode 100644 src/yes_she_codes/semana3.clj delete mode 100644 src/yes_she_codes/util.clj diff --git a/.DS_Store b/.DS_Store index 9ac88cd0d9acf4a3f00f87ec4e4151c863fac82f..b3e25714f02e252e14e4277835d3fa229fcc2a1e 100644 GIT binary patch delta 115 zcmZoMXfc=|#>B`mu~2NHo)%AWK~heB5(5LniKK#wh07-!h_J9TGZZrvF(glxVw%Kn zY;I7iqfl*VWMQDAU}iLVHB)qu~2NHo)#BFaZ-74K~heB(q=}cm5h@OL|C|48B!P$fjFO`c(NGd zBz{A4gIXPhYC}Uq3mpY>gUQ<&&BT$^lm!>%<>cq3GcYhP0uA}kvYDNOp95$Gkn^2+ UGQWr=2P04lD9x}rKx7Lu0GpT}2mk;8 diff --git a/.idea/libraries/Leiningen__com_datomic_datomic_lucene_core_3_3_0.xml b/.idea/libraries/Leiningen__com_datomic_datomic_lucene_core_3_3_0.xml new file mode 100644 index 0000000..1cad0df --- /dev/null +++ b/.idea/libraries/Leiningen__com_datomic_datomic_lucene_core_3_3_0.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__com_datomic_datomic_pro_1_0_6397.xml b/.idea/libraries/Leiningen__com_datomic_datomic_pro_1_0_6397.xml new file mode 100644 index 0000000..a748468 --- /dev/null +++ b/.idea/libraries/Leiningen__com_datomic_datomic_pro_1_0_6397.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__com_datomic_memcache_asg_java_client_1_1_0_33.xml b/.idea/libraries/Leiningen__com_datomic_memcache_asg_java_client_1_1_0_33.xml new file mode 100644 index 0000000..99d376c --- /dev/null +++ b/.idea/libraries/Leiningen__com_datomic_memcache_asg_java_client_1_1_0_33.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__com_datomic_query_support_0_8_28.xml b/.idea/libraries/Leiningen__com_datomic_query_support_0_8_28.xml new file mode 100644 index 0000000..469a9eb --- /dev/null +++ b/.idea/libraries/Leiningen__com_datomic_query_support_0_8_28.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__com_google_code_findbugs_jsr305_3_0_2.xml b/.idea/libraries/Leiningen__com_google_code_findbugs_jsr305_3_0_2.xml new file mode 100644 index 0000000..28105f4 --- /dev/null +++ b/.idea/libraries/Leiningen__com_google_code_findbugs_jsr305_3_0_2.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__com_google_errorprone_error_prone_annotations_2_7_1.xml b/.idea/libraries/Leiningen__com_google_errorprone_error_prone_annotations_2_7_1.xml new file mode 100644 index 0000000..a9122df --- /dev/null +++ b/.idea/libraries/Leiningen__com_google_errorprone_error_prone_annotations_2_7_1.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__com_google_guava_failureaccess_1_0_1.xml b/.idea/libraries/Leiningen__com_google_guava_failureaccess_1_0_1.xml new file mode 100644 index 0000000..2560344 --- /dev/null +++ b/.idea/libraries/Leiningen__com_google_guava_failureaccess_1_0_1.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__com_google_guava_guava_31_0_1_jre.xml b/.idea/libraries/Leiningen__com_google_guava_guava_31_0_1_jre.xml new file mode 100644 index 0000000..0e6f3e1 --- /dev/null +++ b/.idea/libraries/Leiningen__com_google_guava_guava_31_0_1_jre.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__com_google_guava_listenablefuture_9999_0_empty_to_avoid_conflict_with_guava.xml b/.idea/libraries/Leiningen__com_google_guava_listenablefuture_9999_0_empty_to_avoid_conflict_with_guava.xml new file mode 100644 index 0000000..5e968e3 --- /dev/null +++ b/.idea/libraries/Leiningen__com_google_guava_listenablefuture_9999_0_empty_to_avoid_conflict_with_guava.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__com_google_j2objc_j2objc_annotations_1_3.xml b/.idea/libraries/Leiningen__com_google_j2objc_j2objc_annotations_1_3.xml new file mode 100644 index 0000000..4764a14 --- /dev/null +++ b/.idea/libraries/Leiningen__com_google_j2objc_j2objc_annotations_1_3.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__com_h2database_h2_1_3_171.xml b/.idea/libraries/Leiningen__com_h2database_h2_1_3_171.xml new file mode 100644 index 0000000..7b30dbb --- /dev/null +++ b/.idea/libraries/Leiningen__com_h2database_h2_1_3_171.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__commons_beanutils_1_9_4.xml b/.idea/libraries/Leiningen__commons_beanutils_1_9_4.xml new file mode 100644 index 0000000..1d7e879 --- /dev/null +++ b/.idea/libraries/Leiningen__commons_beanutils_1_9_4.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__commons_codec_1_15.xml b/.idea/libraries/Leiningen__commons_codec_1_15.xml new file mode 100644 index 0000000..e0a1077 --- /dev/null +++ b/.idea/libraries/Leiningen__commons_codec_1_15.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__commons_collections_3_2_2.xml b/.idea/libraries/Leiningen__commons_collections_3_2_2.xml new file mode 100644 index 0000000..4d0d129 --- /dev/null +++ b/.idea/libraries/Leiningen__commons_collections_3_2_2.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__io_netty_netty_buffer_4_1_73_Final.xml b/.idea/libraries/Leiningen__io_netty_netty_buffer_4_1_73_Final.xml new file mode 100644 index 0000000..48dce7b --- /dev/null +++ b/.idea/libraries/Leiningen__io_netty_netty_buffer_4_1_73_Final.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__io_netty_netty_codec_4_1_73_Final.xml b/.idea/libraries/Leiningen__io_netty_netty_codec_4_1_73_Final.xml new file mode 100644 index 0000000..822f289 --- /dev/null +++ b/.idea/libraries/Leiningen__io_netty_netty_codec_4_1_73_Final.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__io_netty_netty_codec_http_4_1_73_Final.xml b/.idea/libraries/Leiningen__io_netty_netty_codec_http_4_1_73_Final.xml new file mode 100644 index 0000000..0a99932 --- /dev/null +++ b/.idea/libraries/Leiningen__io_netty_netty_codec_http_4_1_73_Final.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__io_netty_netty_codec_socks_4_1_73_Final.xml b/.idea/libraries/Leiningen__io_netty_netty_codec_socks_4_1_73_Final.xml new file mode 100644 index 0000000..3a0175b --- /dev/null +++ b/.idea/libraries/Leiningen__io_netty_netty_codec_socks_4_1_73_Final.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__io_netty_netty_common_4_1_73_Final.xml b/.idea/libraries/Leiningen__io_netty_netty_common_4_1_73_Final.xml new file mode 100644 index 0000000..8df7b0a --- /dev/null +++ b/.idea/libraries/Leiningen__io_netty_netty_common_4_1_73_Final.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__io_netty_netty_handler_4_1_73_Final.xml b/.idea/libraries/Leiningen__io_netty_netty_handler_4_1_73_Final.xml new file mode 100644 index 0000000..3b05ec0 --- /dev/null +++ b/.idea/libraries/Leiningen__io_netty_netty_handler_4_1_73_Final.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__io_netty_netty_handler_proxy_4_1_73_Final.xml b/.idea/libraries/Leiningen__io_netty_netty_handler_proxy_4_1_73_Final.xml new file mode 100644 index 0000000..80ebf88 --- /dev/null +++ b/.idea/libraries/Leiningen__io_netty_netty_handler_proxy_4_1_73_Final.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__io_netty_netty_resolver_4_1_73_Final.xml b/.idea/libraries/Leiningen__io_netty_netty_resolver_4_1_73_Final.xml new file mode 100644 index 0000000..81c8ded --- /dev/null +++ b/.idea/libraries/Leiningen__io_netty_netty_resolver_4_1_73_Final.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__io_netty_netty_tcnative_classes_2_0_46_Final.xml b/.idea/libraries/Leiningen__io_netty_netty_tcnative_classes_2_0_46_Final.xml new file mode 100644 index 0000000..a0c0629 --- /dev/null +++ b/.idea/libraries/Leiningen__io_netty_netty_tcnative_classes_2_0_46_Final.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__io_netty_netty_transport_4_1_73_Final.xml b/.idea/libraries/Leiningen__io_netty_netty_transport_4_1_73_Final.xml new file mode 100644 index 0000000..44c806b --- /dev/null +++ b/.idea/libraries/Leiningen__io_netty_netty_transport_4_1_73_Final.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__io_netty_netty_transport_classes_epoll_4_1_73_Final.xml b/.idea/libraries/Leiningen__io_netty_netty_transport_classes_epoll_4_1_73_Final.xml new file mode 100644 index 0000000..df9cb87 --- /dev/null +++ b/.idea/libraries/Leiningen__io_netty_netty_transport_classes_epoll_4_1_73_Final.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__io_netty_netty_transport_classes_kqueue_4_1_73_Final.xml b/.idea/libraries/Leiningen__io_netty_netty_transport_classes_kqueue_4_1_73_Final.xml new file mode 100644 index 0000000..8dcc40b --- /dev/null +++ b/.idea/libraries/Leiningen__io_netty_netty_transport_classes_kqueue_4_1_73_Final.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__io_netty_netty_transport_native_epoll_4_1_73_Final_linux_x86_64.xml b/.idea/libraries/Leiningen__io_netty_netty_transport_native_epoll_4_1_73_Final_linux_x86_64.xml new file mode 100644 index 0000000..536bd33 --- /dev/null +++ b/.idea/libraries/Leiningen__io_netty_netty_transport_native_epoll_4_1_73_Final_linux_x86_64.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__io_netty_netty_transport_native_kqueue_4_1_73_Final_osx_x86_64.xml b/.idea/libraries/Leiningen__io_netty_netty_transport_native_kqueue_4_1_73_Final_osx_x86_64.xml new file mode 100644 index 0000000..18c77f8 --- /dev/null +++ b/.idea/libraries/Leiningen__io_netty_netty_transport_native_kqueue_4_1_73_Final_osx_x86_64.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__io_netty_netty_transport_native_unix_common_4_1_73_Final.xml b/.idea/libraries/Leiningen__io_netty_netty_transport_native_unix_common_4_1_73_Final.xml new file mode 100644 index 0000000..b6d2473 --- /dev/null +++ b/.idea/libraries/Leiningen__io_netty_netty_transport_native_unix_common_4_1_73_Final.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__jakarta_json_jakarta_json_api_1_1_6.xml b/.idea/libraries/Leiningen__jakarta_json_jakarta_json_api_1_1_6.xml new file mode 100644 index 0000000..210bdb7 --- /dev/null +++ b/.idea/libraries/Leiningen__jakarta_json_jakarta_json_api_1_1_6.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__org_apache_activemq_artemis_commons_2_19_1.xml b/.idea/libraries/Leiningen__org_apache_activemq_artemis_commons_2_19_1.xml new file mode 100644 index 0000000..1c0744f --- /dev/null +++ b/.idea/libraries/Leiningen__org_apache_activemq_artemis_commons_2_19_1.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__org_apache_activemq_artemis_core_client_2_19_1.xml b/.idea/libraries/Leiningen__org_apache_activemq_artemis_core_client_2_19_1.xml new file mode 100644 index 0000000..a58e577 --- /dev/null +++ b/.idea/libraries/Leiningen__org_apache_activemq_artemis_core_client_2_19_1.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__org_apache_httpcomponents_httpclient_4_5_9.xml b/.idea/libraries/Leiningen__org_apache_httpcomponents_httpclient_4_5_9.xml new file mode 100644 index 0000000..b13d0eb --- /dev/null +++ b/.idea/libraries/Leiningen__org_apache_httpcomponents_httpclient_4_5_9.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__org_apache_httpcomponents_httpcore_4_4_11.xml b/.idea/libraries/Leiningen__org_apache_httpcomponents_httpcore_4_4_11.xml new file mode 100644 index 0000000..64ee84d --- /dev/null +++ b/.idea/libraries/Leiningen__org_apache_httpcomponents_httpcore_4_4_11.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__org_apache_johnzon_johnzon_core_0_9_5.xml b/.idea/libraries/Leiningen__org_apache_johnzon_johnzon_core_0_9_5.xml new file mode 100644 index 0000000..f230f3f --- /dev/null +++ b/.idea/libraries/Leiningen__org_apache_johnzon_johnzon_core_0_9_5.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__org_apache_tomcat_tomcat_jdbc_7_0_109.xml b/.idea/libraries/Leiningen__org_apache_tomcat_tomcat_jdbc_7_0_109.xml new file mode 100644 index 0000000..a107f00 --- /dev/null +++ b/.idea/libraries/Leiningen__org_apache_tomcat_tomcat_jdbc_7_0_109.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__org_apache_tomcat_tomcat_juli_7_0_109.xml b/.idea/libraries/Leiningen__org_apache_tomcat_tomcat_juli_7_0_109.xml new file mode 100644 index 0000000..1c81a84 --- /dev/null +++ b/.idea/libraries/Leiningen__org_apache_tomcat_tomcat_juli_7_0_109.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__org_checkerframework_checker_qual_3_12_0.xml b/.idea/libraries/Leiningen__org_checkerframework_checker_qual_3_12_0.xml new file mode 100644 index 0000000..66fbcca --- /dev/null +++ b/.idea/libraries/Leiningen__org_checkerframework_checker_qual_3_12_0.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__org_clojure_tools_cli_1_0_206.xml b/.idea/libraries/Leiningen__org_clojure_tools_cli_1_0_206.xml new file mode 100644 index 0000000..8960d71 --- /dev/null +++ b/.idea/libraries/Leiningen__org_clojure_tools_cli_1_0_206.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__org_codehaus_janino_commons_compiler_3_0_12.xml b/.idea/libraries/Leiningen__org_codehaus_janino_commons_compiler_3_0_12.xml new file mode 100644 index 0000000..02b197f --- /dev/null +++ b/.idea/libraries/Leiningen__org_codehaus_janino_commons_compiler_3_0_12.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__org_codehaus_janino_commons_compiler_jdk_3_0_12.xml b/.idea/libraries/Leiningen__org_codehaus_janino_commons_compiler_jdk_3_0_12.xml new file mode 100644 index 0000000..c9ee325 --- /dev/null +++ b/.idea/libraries/Leiningen__org_codehaus_janino_commons_compiler_jdk_3_0_12.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__org_fressian_fressian_0_6_6.xml b/.idea/libraries/Leiningen__org_fressian_fressian_0_6_6.xml new file mode 100644 index 0000000..c46ebc9 --- /dev/null +++ b/.idea/libraries/Leiningen__org_fressian_fressian_0_6_6.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__org_jboss_logging_jboss_logging_3_4_2_Final.xml b/.idea/libraries/Leiningen__org_jboss_logging_jboss_logging_3_4_2_Final.xml new file mode 100644 index 0000000..4f49837 --- /dev/null +++ b/.idea/libraries/Leiningen__org_jboss_logging_jboss_logging_3_4_2_Final.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__org_slf4j_jcl_over_slf4j_1_7_32.xml b/.idea/libraries/Leiningen__org_slf4j_jcl_over_slf4j_1_7_32.xml new file mode 100644 index 0000000..7a97ca2 --- /dev/null +++ b/.idea/libraries/Leiningen__org_slf4j_jcl_over_slf4j_1_7_32.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__org_slf4j_jul_to_slf4j_1_7_32.xml b/.idea/libraries/Leiningen__org_slf4j_jul_to_slf4j_1_7_32.xml new file mode 100644 index 0000000..589ccfb --- /dev/null +++ b/.idea/libraries/Leiningen__org_slf4j_jul_to_slf4j_1_7_32.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__org_slf4j_log4j_over_slf4j_1_7_32.xml b/.idea/libraries/Leiningen__org_slf4j_log4j_over_slf4j_1_7_32.xml new file mode 100644 index 0000000..981b675 --- /dev/null +++ b/.idea/libraries/Leiningen__org_slf4j_log4j_over_slf4j_1_7_32.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__org_slf4j_slf4j_api_1_7_32.xml b/.idea/libraries/Leiningen__org_slf4j_slf4j_api_1_7_32.xml new file mode 100644 index 0000000..c77c5e4 --- /dev/null +++ b/.idea/libraries/Leiningen__org_slf4j_slf4j_api_1_7_32.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Leiningen__org_slf4j_slf4j_nop_1_7_36.xml b/.idea/libraries/Leiningen__org_slf4j_slf4j_nop_1_7_36.xml new file mode 100644 index 0000000..53cc203 --- /dev/null +++ b/.idea/libraries/Leiningen__org_slf4j_slf4j_nop_1_7_36.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 3b2f5ed..51a1670 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -14,7 +14,7 @@ - + \ No newline at end of file diff --git a/src/yes_she_codes/dados/cartoes.csv b/dados/cartoes.csv similarity index 80% rename from src/yes_she_codes/dados/cartoes.csv rename to dados/cartoes.csv index 5005205..76113b0 100644 --- a/src/yes_she_codes/dados/cartoes.csv +++ b/dados/cartoes.csv @@ -3,4 +3,4 @@ NÚMERO,CVV,VALIDADE,LIMITE,CLIENTE 4321 4321 4321 4321,222,2024-02,2.000,333.444.555-66 1598 1598 1598 1598,333,2021-03,3.000,666.777.888-99 6655 6655 6655 6655,444,2025-04,4.000,666.777.888-99 -3939 3939 3939 3939,555,2026-05,5.000,999.123.456-78 \ No newline at end of file +3939 3939 3939 3939,555,2026-05,5.000,999.123.456-78 diff --git a/src/yes_she_codes/dados/clientes.csv b/dados/clientes.csv similarity index 100% rename from src/yes_she_codes/dados/clientes.csv rename to dados/clientes.csv diff --git a/src/yes_she_codes/dados/compras.csv b/dados/compras.csv similarity index 100% rename from src/yes_she_codes/dados/compras.csv rename to dados/compras.csv diff --git a/project.clj b/project.clj index ae30710..dca7eef 100644 --- a/project.clj +++ b/project.clj @@ -3,9 +3,18 @@ :url "http://example.com/FIXME" :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0" :url "https://www.eclipse.org/legal/epl-2.0/"} + :repositories {"my.datomic.com" {:url "https://my.datomic.com/repo"} + :username "emillyn.silva@nubank.com.br" + :password "1f29f970-ad2b-4c1e-b4ba-2e3dc6616849"} :dependencies [[org.clojure/clojure "1.10.1"] [clojure.java-time "0.3.2"] [org.clojure/data.csv "1.0.1"] - [prismatic/schema "1.1.12"]] + [com.datomic/datomic-pro "1.0.6397"] + [prismatic/schema "1.1.12"] + [org.slf4j/slf4j-nop "1.7.36"]] - :repl-options {:init-ns yes-she-codes.core}) \ No newline at end of file + + + + + :repl-options {:init-ns yes-she-codes.core}) diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..1faa59d9172479b446ecc4f28f028f6a659c6c8d GIT binary patch literal 6148 zcmeHK!A=4(5N!dq8$#q@g2%mh;{ceT7n2R*!JEk%J*a_Qm&hi#CF~+WT(h5{f8-zd zJI=I)NKjA47&4Pg-*h_DrmvgMmNCX#ns|PI05>}^Nnayfdd;g%8R_$56aR{AeqnW0bsO;5`PP_Nh$JtZ*{Gw^*@I@-w zHaLe@Fn-wagFA`_GP;AP`3&<+LSldzAO_Zj0dv2J;<{{z_CX8~1HZ-q-VZh?qGK@A zsICrZ@b?kN3y3IS<68pJHs~14G{Oi7*QtOym76C9*XiK5O`Kye)2P!KS1ZFjW@T<( zC|s=$ep`hz?r5Zz7$63g8OZBy8SDSa_wWDZBCliente nil - "Mulher Maravilha" - "321.321.321-32" - "maisfortedaliga@ligadajustica.dc.br")) +(pprint "-----------------------------------------------------------") +(pprint (cliente/agregate-clients)) -(println "\n========= PESQUISA CLIENTE DE ID 5 ==========") -(pprint (y.bd/pesquisa-cliente-por-id! 5)) +(pprint "-----------------------------------------------------------") -(println "\n========= EXCLUIR CLIENTE DE ID 5 ==========") -(y.bd/exclui-cliente! 5) +;SEMANA 2 - ATOM +(pprint (compra/limpa-atom)) +(pprint (compra/lista-compra!)) +(pprint (compra/carrega-compra)) +(pprint "LISTA COMPRA") +(pprint (compra/lista-compra!)) -; TESTE DAS LÓGICAS DE CARTÕES -(println "\n========= LISTAGEM DE CARTÕES ==========") -(pprint (y.bd/lista-cartoes!)) +(pprint "FILTRA POR ID") +(pprint (compra/pesquisa-compra-por-id! 3 (compra/lista-compra!))) -(println "\n========= INSERE CARTÃO DE ID 6 ==========") -(y.bd/insere-cartao! (y.cartao/->Cartao nil - 1111222233334444 - 999 - (time/year-month "2025-05") - 1500M - "000.111.222-33")) +(pprint "UPDATE POR ID") +(pprint (compra/update-compra 6 :valor 200.0)) -(println "\n========= PESQUISA CARTÃO DE ID 6 ==========") -(pprint (y.bd/pesquisa-cartao-por-id! 6)) +(pprint "EXCLUI POR ID") +(pprint (compra/exclui-compra! 3)) -(println "\n========= EXCLUIR CLIENTE DE ID 5 ==========") -(y.bd/exclui-cartao! 6) - - -; TESTE DAS LÓGICAS DE COMPRAS -(println "\n========= LISTAGEM DE COMPRAS ==========") -(pprint (y.bd/lista-compras!)) - -(println "\n========= INSERE COMPRA DE ID 20 ==========") -(y.bd/insere-compra! (y.compra/->Compra nil - (time/local-date "2022-05-20") - 123.99M - "Carrefour" - "Alimentação" - 3939393939393939)) - -(println "\n========= PESQUISA COMPRA DE ID 20 ==========") -(pprint (y.bd/pesquisa-compra-por-id! 20)) - -(println "\n========= EXCLUIR COMPRA DE ID 20 ==========") -(y.bd/exclui-compra! 20) - -(println "\n========= INSPECIONANDO COMPRAS DA Viúva Negra (cartão 4321432143214321)") -(def compras-da-viuva (y.compra/filtra-compras-por-cartao 4321432143214321 - (y.bd/lista-compras!))) - -(println "\n========= TOTAL GASTO EM TODAS AS COMPRAS =========") -(pprint (y.compra/total-gasto compras-da-viuva)) - -(println "\n========= TOTAL GASTO EM FEVEREIRO =========") -(pprint (y.compra/total-gasto-no-mes 2 compras-da-viuva)) -(pprint (y.compra/filtra-compras-no-mes 2 compras-da-viuva)) - -(println "\n========= COMPRAS AGRUPADAS POR CATEGORIA =========") -(pprint (y.compra/agrupa-gastos-por-categoria compras-da-viuva)) - - -(println "\n========= TENTA INSERIR COMPRA INVÁLIDA ==========") -(try - (y.bd/insere-compra! (y.compra/->Compra nil - (time/local-date "2022-05-20") - -100M - "Carrefour" - "Alimentação" - 3939393939393939)) - (catch Exception e - (println "Validação deu certo"))) \ No newline at end of file +;SEMANA 4 - DATOMIC +(datomic/carrega-banco-de-dados!) diff --git a/src/yes_she_codes/csv/leitor_csv.clj b/src/yes_she_codes/csv/leitor_csv.clj index 8693ee6..afaa798 100644 --- a/src/yes_she_codes/csv/leitor_csv.clj +++ b/src/yes_she_codes/csv/leitor_csv.clj @@ -1,9 +1,9 @@ (ns yes-she-codes.csv.leitor-csv (:require [java-time :as time] - [yes-she-codes.util :as y.util] - [yes-she-codes.dominio.compra :as y.compra] - [yes-she-codes.dominio.cliente :as y.cliente] - [yes-she-codes.dominio.cartao :as y.cartao])) + [yes-she-codes.logic :as logic] + [yes-she-codes.dominio.compra :as dominio.compra] + [yes-she-codes.dominio.cliente :as dominio.cliente] + [yes-she-codes.dominio.cartao :as dominio.cartao])) (defn- converte-valores-na-linha [funcoes-de-conversao linha] (map #(%1 %2) funcoes-de-conversao linha)) @@ -15,22 +15,22 @@ rest (map #(clojure.string/split % #",")))) -(def csv->cartao [y.util/str->long - y.util/str->long - time/year-month +(def csv->cartao [logic/format-numero + logic/format-numero + logic/format-data-cartao bigdec identity]) -(def csv->compra [time/local-date +(def csv->compra [logic/format-data-compra bigdec identity identity - y.util/str->long]) + logic/format-numero]) -(def cria-cliente-sem-id (partial y.cliente/->Cliente nil)) -(def cria-cartao-sem-id (partial y.cartao/->Cartao nil)) -(def cria-compra-sem-id (partial y.compra/->Compra nil)) +(def cria-cliente-sem-id (partial dominio.cliente/->Cliente nil)) +(def cria-cartao-sem-id (partial dominio.cartao/->Cartao nil)) +(def cria-compra-sem-id (partial dominio.compra/->Compra nil)) (defn processa-arquivo-de-clientes! [] @@ -49,4 +49,3 @@ (map #(converte-valores-na-linha csv->compra %)) (map #(apply cria-compra-sem-id %)) vec)) - diff --git a/src/yes_she_codes/db.clj b/src/yes_she_codes/db.clj deleted file mode 100644 index cb766a8..0000000 --- a/src/yes_she_codes/db.clj +++ /dev/null @@ -1,30 +0,0 @@ -(ns yes-she-codes.db) - - -(def clientes [["feiticeira escarlate" "000.111.222-33" "feiticeira.poderosa@vingadoras.com.br"] - ["Viúva Negra" "333.444.555-66" "viuva.casca.grossa@vingadoras.com.br"] - ["Hermione Granger" "666.777.888-99" "hermione.salvadora@hogwarts.com"] - ["Daenerys Targaryen" "999.123.456-78" "mae.dos.dragoes@got.com"]]) - -(def cartoes [["1234 1234 1234 1234" 111 1000 "2012-12" "000.111.222-33"] - ["4321 4321 4321 4321" 222 2000 "2012-12" "333.444.555-66"] - ["1598 1598 1598 1598" 333 3000 "2012-12" "666.777.888-99"] - ["6655 6655 6655 6655" 444 4000 "2012-12" "666.777.888-99"] - ["3939 3939 3939 3939" 555 5000 "2012-12" "999.123.456-78"]]) - -(def compras [["2022-01-01" 129.90 "Outback" "Alimentacao" "1234 1234 1234 1234"] - ["2022-01-01" 80.90 "Dentista" "Saude" "1234 1234 1234 1234"] - ["2022-02-01" 40.90 "Cinema" "Lazer" "1234 1234 1234 1234"] - ["2022-04-01" 60.90 "Show" "Lazer" "3939 3939 3939 3939"] - ["2022-04-01" 10.90 "Ifood" "Alimentacao" "3939 3939 3939 3939"] - ["2022-04-01" 90.90 "Alura" "Educacao" "1234 1234 1234 1234"] - ["2022-04-01" 400.00 "Tok&Stock" "Casa" "6655 6655 6655 6655"] - ["2022-04-01" 129.90 "Leroy Merlin" "Casa" "6655 6655 6655 6655"] - ["2022-04-01" 150.90 "Madero" "Alimentacao" "3939 3939 3939 3939"]]) - - -(defn param-clientes [] clientes) - -(defn param-cartoes [] cartoes) - -(defn param-compras [] compras) \ No newline at end of file diff --git a/src/yes_she_codes/db/atom.clj b/src/yes_she_codes/db/atom.clj new file mode 100644 index 0000000..778612f --- /dev/null +++ b/src/yes_she_codes/db/atom.clj @@ -0,0 +1,7 @@ +(ns yes-she-codes.db.atom) + +(def repositorio-clientes (atom [])) + +(def repositorio-de-compras (atom [])) + +(def repositorio-cartoes (atom [])) diff --git a/src/yes_she_codes/db/cartao.clj b/src/yes_she_codes/db/cartao.clj new file mode 100644 index 0000000..cd10686 --- /dev/null +++ b/src/yes_she_codes/db/cartao.clj @@ -0,0 +1,9 @@ +(ns yes-she-codes.db.cartao) + +(def cartoes [["1234 1234 1234 1234" 111 "2012-12" 1000 "000.111.222-33"] + ["4321 4321 4321 4321" 222 "2012-12" 2000 "333.444.555-66"] + ["1598 1598 1598 1598" 333 "2012-12" 3000 "666.777.888-99"] + ["6655 6655 6655 6655" 444 "2012-12" 4000 "666.777.888-99"] + ["3939 3939 3939 3939" 555 "2012-12" 5000 "999.123.456-78"]]) + +(defn param-cartoes [] cartoes) diff --git a/src/yes_she_codes/db/cliente.clj b/src/yes_she_codes/db/cliente.clj new file mode 100644 index 0000000..aa6837c --- /dev/null +++ b/src/yes_she_codes/db/cliente.clj @@ -0,0 +1,10 @@ +(ns yes-she-codes.db.cliente) + + +(def clientes [["feiticeira escarlate" "000.111.222-33" "feiticeira.poderosa@vingadoras.com.br"] + ["Viúva Negra" "333.444.555-66" "viuva.casca.grossa@vingadoras.com.br"] + ["Hermione Granger" "666.777.888-99" "hermione.salvadora@hogwarts.com"] + ["Daenerys Targaryen" "999.123.456-78" "mae.dos.dragoes@got.com"]]) + + +(defn param-clientes [] clientes) \ No newline at end of file diff --git a/src/yes_she_codes/db/compra.clj b/src/yes_she_codes/db/compra.clj new file mode 100644 index 0000000..22a8387 --- /dev/null +++ b/src/yes_she_codes/db/compra.clj @@ -0,0 +1,13 @@ +(ns yes-she-codes.db.compra) + +(def compras [["2022-01-01" 129.90 "Outback" "Alimentação" "1234 1234 1234 1234"] + ["2022-01-01" 80.90 "Dentista" "Saúde" "1234 1234 1234 1234"] + ["2022-02-01" 40.90 "Cinema" "Lazer" "1234 1234 1234 1234"] + ["2022-04-01" 60.90 "Show" "Lazer" "3939 3939 3939 3939"] + ["2022-04-01" 10.90 "Ifood" "Alimentação" "3939 3939 3939 3939"] + ["2022-04-01" 90.90 "Alura" "Educação" "1234 1234 1234 1234"] + ["2022-04-01" 400.00 "Tok&Stock" "Casa" "6655 6655 6655 6655"] + ["2022-04-01" 129.90 "Leroy Merlin" "Casa" "6655 6655 6655 6655"] + ["2022-04-01" 150.90 "Madero" "Alimentação" "3939 3939 3939 3939"]]) + +(defn param-compras [] compras) diff --git a/src/yes_she_codes/db/datomic.clj b/src/yes_she_codes/db/datomic.clj new file mode 100644 index 0000000..cd6724f --- /dev/null +++ b/src/yes_she_codes/db/datomic.clj @@ -0,0 +1,46 @@ +(ns yes-she-codes.db.datomic) + +(def schema-datomic + [ + {:db/ident :compra/data + :db/valueType :db.type/string + :db/cardinality :db.cardinality/one + :db/doc "Data em que a compra foi realizada."} + {:db/ident :compra/valor + :db/valueType :db.type/bigdec + :db/cardinality :db.cardinality/one + :db/doc "Valor total da compra."} + {:db/ident :compra/estabelecimento + :db/valueType :db.type/string + :db/cardinality :db.cardinality/one + :db/doc "Local onde a compra foi realizada."} + {:db/ident :compra/categoria + :db/valueType :db.type/string + :db/cardinality :db.cardinality/one + :db/doc "Grupo de gastos ao qual a compra pertence."} + {:db/ident :compra/cartao + :db/valueType :db.type/ref + :db/cardinality :db.cardinality/one + :db/doc "Cartão utilizado na compra."} + + {:db/ident :cartao/numero + :db/valueType :db.type/long + :db/cardinality :db.cardinality/one + :db/unique :db.unique/identity + :db/doc "Número identificador do cartão."} + {:db/ident :cartao/cvv + :db/valueType :db.type/long + :db/cardinality :db.cardinality/one + :db/doc "Número de segurança do cartão."} + {:db/ident :cartao/validate + :db/valueType :db.type/string + :db/cardinality :db.cardinality/one + :db/doc "Número de segurança do cartão."} + {:db/ident :cartao/limite + :db/valueType :db.type/bigdec + :db/cardinality :db.cardinality/one + :db/doc "Valor máximo de compras para um cartão."} + {:db/ident :cartao/cliente + :db/valueType :db.type/string + :db/cardinality :db.cardinality/one + :db/doc "Dono do cartão."}]) diff --git a/src/yes_she_codes/dominio/cartao.clj b/src/yes_she_codes/dominio/cartao.clj index 7fe44c6..6cc5770 100644 --- a/src/yes_she_codes/dominio/cartao.clj +++ b/src/yes_she_codes/dominio/cartao.clj @@ -1,3 +1,27 @@ -(ns yes-she-codes.dominio.cartao) +(ns yes-she-codes.dominio.cartao + (:require [schema.core :as s] + [yes-she-codes.logic :as logic])) -(defrecord Cartao [id numero cvv validade limite cliente]) \ No newline at end of file +(def CartaoSchema + {(s/optional-key :id) logic/IdOpcional + :numero (s/constrained s/Num logic/validate-numero) , + :cvv (s/constrained s/Num logic/validate-cvv), + :validate (s/constrained s/Str logic/validate-data-cartao), + :limite (s/constrained s/Num pos?), + :cliente s/Str}) + + +(s/defn ->Cartao :- CartaoSchema + [id :- (s/maybe s/Num) + numero :- s/Num + cvv :- s/Num + validade :- s/Str + limite :- s/Num + cliente :- s/Str] + + {:id id + :numero numero + :cvv cvv + :validate validade + :limite limite + :cliente cliente}) diff --git a/src/yes_she_codes/dominio/cliente.clj b/src/yes_she_codes/dominio/cliente.clj index e1b2bb6..39206b9 100644 --- a/src/yes_she_codes/dominio/cliente.clj +++ b/src/yes_she_codes/dominio/cliente.clj @@ -1,5 +1,20 @@ -(ns yes-she-codes.dominio.cliente) +(ns yes-she-codes.dominio.cliente + (:require [schema.core :as s] + [yes-she-codes.logic :as v])) -(defrecord Cliente [id nome cpf email]) +(def ClienteSchema + {(s/optional-key :id) v/IdOpcional + :nome (s/constrained s/Str v/validate-string) , + :cpf (s/constrained s/Str v/validate-cpf), + :email (s/constrained s/Str v/validate-email)}) +(s/defn ->Cliente :- ClienteSchema + [id :- (s/maybe s/Num) + nome :- s/Str + cpf :- s/Str + email :- s/Str] + {:id id + :nome nome + :cpf cpf + :email email}) diff --git a/src/yes_she_codes/dominio/compra.clj b/src/yes_she_codes/dominio/compra.clj index e0d0fda..05d5259 100644 --- a/src/yes_she_codes/dominio/compra.clj +++ b/src/yes_she_codes/dominio/compra.clj @@ -1,61 +1,28 @@ (ns yes-she-codes.dominio.compra - (:require [yes-she-codes.util :as util] - [java-time :as time])) - - -(defrecord Compra [id - data - valor - estabelecimento - categoria - cartao]) - - -(defn compra-valida? [compra] - (let [valida-data (time/after? (time/local-date) (get compra :data)) - valida-valor (and (number? (get compra :valor)) (>= (get compra :valor) 0)) - valida-estabelecimento (>= (count (get compra :estabelecimento)) 2) - categoria (get compra :categoria) - valida-categoria (or (= "Alimentação" categoria) - (= "Automóvel" categoria) - (= "Casa" categoria) - (= "Educação" categoria) - (= "Lazer" categoria) - (= "Saúde" categoria))] - - (and valida-data valida-valor valida-estabelecimento valida-categoria))) - - -(defn filtra-compras [predicado compras] - (vec (filter predicado compras))) - - -(defn filtra-compras-no-mes [mes compras] - (filtra-compras #(= mes (util/mes-da-data (:data %))) compras)) - - -(defn filtra-compras-no-estabelecimento [estabelecimento compras] - (filtra-compras #(= estabelecimento (:estabelecimento %)) compras)) - - -(defn filtra-compras-por-valor [minimo maximo compras] - (filtra-compras #(and (>= (:valor %) minimo) - (<= (:valor %) maximo)) - compras)) - -(defn filtra-compras-por-cartao [cartao compras] - (filtra-compras #(= cartao (:cartao %)) compras)) - - -(defn total-gasto [compras] - (reduce + (map :valor compras))) - - -(def total-gasto-no-mes (comp total-gasto filtra-compras-no-mes)) - - -(defn agrupa-gastos-por-categoria [compras] - (vec (map (fn [[categoria compras-da-categoria]] - {:categoria categoria - :total-gasto (total-gasto compras-da-categoria)}) - (group-by :categoria compras)))) \ No newline at end of file + (:require [schema.core :as s] + [yes-she-codes.logic :as v])) + + +(def CompraSchema + {(s/optional-key :id) v/IdOpcional + :data (s/constrained s/Str v/validate-data) , + :valor (s/constrained s/Num pos?) , + :estabelecimento (s/constrained s/Str v/validate-string), + :categoria (s/constrained s/Str v/validate-categoria), + :cartao (s/constrained s/Num v/validate-numero)}) + + +(s/defn ->Compra :- CompraSchema + [id :- (s/maybe s/Num) + data :- s/Str + valor :- s/Num + estabelecimento :- s/Str + categoria :- s/Str + cartao :- s/Num] + + {:id id + :data data + :valor valor + :estabelecimento estabelecimento + :categoria categoria + :cartao cartao}) diff --git a/src/yes_she_codes/leituracsv.clj b/src/yes_she_codes/leituracsv.clj deleted file mode 100644 index 593c0a1..0000000 --- a/src/yes_she_codes/leituracsv.clj +++ /dev/null @@ -1,40 +0,0 @@ -(ns yes-she-codes.leituracsv) - -(defn novo-cliente - [nome cpf email] - (let [client (conj {:nome nome :cpf cpf :email email})] - client)) - -(defn novo-cartao - [numero cvv limite validate cpf] - (let [formatValidate (t/format "MM/yyyy" (t/year-month validate))] - (let [client (conj {:numero numero, :cvv cvv, :limite limite :validate formatValidate :cpf cpf})] - client))) - -(defn nova-compra - [data valor estabelecimento categoria cartao] - (let [formatData (t/format "dd/MM/yyyy" (t/local-date data))] - (conj {:data formatData, :valor valor, :estabelecimento estabelecimento :categoria categoria :cartao cartao}))) - -(defn processa-csv [caminho-arquivo funcao-mapeamento] - (->> (slurp caminho-arquivo) - clojure.string/split-lines - rest - (map #(clojure.string/split % #",")) - (mapv funcao-mapeamento))) - - -(defn lista-clientes [] - (processa-csv "dados/clientes.csv" (fn [[nome cpf email]] - (novo-cliente nome cpf email)))) - -(defn lista-cartoes [] - (processa-csv "dados/cartoes.csv" (fn [[numero cvv validade limite cliente]] - (novo-cartao numero cvv validade limite cliente)))) - - -(defn lista-compras [] - (processa-csv "dados/compras.csv" (fn [[data valor estabelecimento categoria cartao]] - (nova-compra data valor estabelecimento categoria cartao)))) - - diff --git a/src/yes_she_codes/logic.clj b/src/yes_she_codes/logic.clj new file mode 100644 index 0000000..3e99e0d --- /dev/null +++ b/src/yes_she_codes/logic.clj @@ -0,0 +1,48 @@ +(ns yes-she-codes.logic + (:require [schema.core :as s] + [java-time :as t] + [clojure.string :as str])) + +(s/set-fn-validation! true) + +(defn format-numero [numero] + (Long/parseLong (str/replace numero #" " ""))) + +(defn format-data-compra [data] + (t/format "dd/MM/yyyy" (t/local-date data))) + +(defn format-data-cartao [validate] + (t/format "MM/yyyy" (t/year-month validate))) + +(defn validate-numero [numero] + (and (pos? numero) (< numero 1e+16))) + +(defn validate-cvv [cvv] + (and (pos? cvv) (< cvv 999))) + +(defn validate-data-cartao [validate] + (re-matches #"\d{2}[/]?\d{4}" validate)) + + +(defn validate-email [email] + (re-matches #".+\@.+\..+" email)) + +(defn validate-cpf [cpf] + (re-matches #"\d{3}[\.]?\d{3}[\.]?\d{3}[-]?\d{2}" cpf)) + +(defn validate-string [string] + (>= (count string) 2)) + +(def IdOpcional (s/maybe s/Num)) + +(defn validate-data [data] + (re-matches #"[0-9]{2}[/]?[0-9]{2}[/]?[0-9]{4}" data)) + + +(defn validate-categoria [categoria] + (let [lista-categoria ["Alimentação", "Automóvel", "Casa", "Educação", "Lazer", "Saúde"]] + (str/includes? lista-categoria categoria))) + +(defn filtra-data + [field value data] + (filter #(= value (field %)) data)) diff --git a/src/yes_she_codes/persistencia/banco_de_dados_atomos.clj b/src/yes_she_codes/persistencia/banco_de_dados_atomos.clj deleted file mode 100644 index 973025e..0000000 --- a/src/yes_she_codes/persistencia/banco_de_dados_atomos.clj +++ /dev/null @@ -1,118 +0,0 @@ -(ns yes-she-codes.persistencia.banco-de-dados-atomos - (:require [yes-she-codes.dominio.compra :as y.compra] - [yes-she-codes.util :as y.util] - [yes-she-codes.csv.leitor-csv :as y.csv])) - -;;;;; Átomos que persistem cada uma das entidades -(def repositorio-de-clientes (atom [])) -(def repositorio-de-cartoes (atom [])) -(def repositorio-de-compras (atom [])) - - - -;;;;; Lógicas genéricas de persistência de entidades -(defn- insere-entidade [entidades entidade] - (let [id-da-entidade (y.util/proximo-id entidades) - entidade-com-id (assoc entidade :id id-da-entidade)] - (conj entidades entidade-com-id))) - - -(defn- pesquisa-por-id [entidades id] - (first (filter #(= id (:id %)) entidades))) - - -(defn- deve-remover? [id entidade] - (->> entidade - :id - (= id))) - - -(defn- exclui-entidade [entidades id] - (remove #(deve-remover? id %) entidades)) - - -(defn- carrega-registros! [insere registros] - (doseq [registro registros] - (insere registro))) - - - -;;;;; Lógicas de persistência de COMPRAS -(defn insere-compra! [compra] - (if (y.compra/compra-valida? compra) - (swap! repositorio-de-compras insere-entidade compra) - (throw (ex-info "Compra inválida!" {:compra compra})))) - - -(defn- carrega-compras-no-atomo! [] - (carrega-registros! insere-compra! (y.csv/processa-arquivo-de-compras!))) - - -(defn exclui-compra! [id] - (swap! repositorio-de-compras exclui-entidade id)) - - -(defn pesquisa-compra-por-id! [id] - (pesquisa-por-id @repositorio-de-compras id)) - - -(defn lista-compras! [] - @repositorio-de-compras) - - - -;;;;; Lógicas de persistência de CLIENTES -(defn insere-cliente! [cliente] - (swap! repositorio-de-clientes insere-entidade cliente)) - - -(defn- carrega-clientes-no-atomo! [] - (carrega-registros! insere-cliente! (y.csv/processa-arquivo-de-clientes!))) - - -(defn pesquisa-cliente-por-id! [id] - (pesquisa-por-id @repositorio-de-clientes id)) - - -(defn lista-clientes! [] - @repositorio-de-clientes) - - -(defn exclui-cliente! [id] - (swap! repositorio-de-clientes exclui-entidade id)) - - - -;;;;; Lógicas de persistência de CARTÕES -(defn insere-cartao! [cartao] - (swap! repositorio-de-cartoes insere-entidade cartao)) - - -(defn- carrega-cartoes-no-atomo! [] - (carrega-registros! insere-cartao! (y.csv/processa-arquivo-de-cartoes!))) - - -(defn pesquisa-cartao-por-id! [id] - (pesquisa-por-id @repositorio-de-cartoes id)) - - -(defn lista-cartoes! [] - @repositorio-de-cartoes) - - -(defn exclui-cartao! [id] - (swap! repositorio-de-cartoes exclui-entidade id)) - - - -;;;;; Limpa e carrega registros nos átomos -(defn limpa-banco-de-dados! [] - (doseq [atomo [repositorio-de-clientes repositorio-de-cartoes repositorio-de-compras]] - (swap! atomo (fn [entidades] - [])))) - -(defn carrega-banco-de-dados! [] - (limpa-banco-de-dados!) - (carrega-clientes-no-atomo!) - (carrega-cartoes-no-atomo!) - (carrega-compras-no-atomo!)) \ No newline at end of file diff --git a/src/yes_she_codes/persistencia/cartao.clj b/src/yes_she_codes/persistencia/cartao.clj new file mode 100644 index 0000000..d52faba --- /dev/null +++ b/src/yes_she_codes/persistencia/cartao.clj @@ -0,0 +1,21 @@ +(ns yes-she-codes.persistencia.cartao + (:require [yes-she-codes.logic :as logic] + [yes-she-codes.dominio.cartao :as dominio.cartao] + [yes-she-codes.db.cartao :as db.cartao] + [schema.core :as s])) + +(s/defn novo-cartao :- dominio.cartao/CartaoSchema + [[numero cvv validate limite cliente]] + (let [format-numero (logic/format-numero numero) + format-data (logic/format-data-cartao validate)] + (dominio.cartao/->Cartao nil format-numero cvv format-data limite cliente))) + +(defn parametros-cartoes [] + (let [cartoes (db.cartao/param-cartoes)] + (vec (map novo-cartao cartoes)))) + +(defn lista-cartoes [] + (parametros-cartoes)) + +(defn agregate-cartao [cpf] + {:cartao (into {} (logic/filtra-data :cliente cpf (lista-cartoes)))}) diff --git a/src/yes_she_codes/persistencia/cliente.clj b/src/yes_she_codes/persistencia/cliente.clj new file mode 100644 index 0000000..3abff35 --- /dev/null +++ b/src/yes_she_codes/persistencia/cliente.clj @@ -0,0 +1,37 @@ +(ns yes-she-codes.persistencia.cliente + (:use clojure.pprint) + (:require [yes-she-codes.db.cliente :as db.cliente] + [yes-she-codes.dominio.cliente :as dominio.cliente] + [yes-she-codes.logic :as logic] + [yes-she-codes.persistencia.compra :as compras] + [yes-she-codes.persistencia.cartao :as cartao] + [schema.core :as s])) + +(s/defn novo-cliente :- dominio.cliente/ClienteSchema + [[nome cpf email]] + (dominio.cliente/->Cliente nil nome cpf email)) + + +(defn parametros-clients [] + (let [clients (db.cliente/param-clientes)] + (vec (map novo-cliente clients)))) + + +(defn lista-clientes [] + (parametros-clients)) + + +(defn agregate-client-total-gasto [compras] + (let [total (compras/total-compras-por-cartao compras)] + (if (empty? total) 0 (:R$ (first total))))) + +(defn agregate-client-cpf [cpf] + (let [client (into {} (logic/filtra-data :cpf cpf (lista-clientes))) + cartao (cartao/agregate-cartao cpf) + compras (compras/agregate-compras (:numero (:cartao cartao))) + total-gasto (assoc-in cartao [:cartao :total-gasto] (agregate-client-total-gasto compras)) + conta (assoc-in total-gasto [:cartao :compras] compras)] + (conj client conta))) + +(defn agregate-clients [] + (map (fn [client] (agregate-client-cpf (:cpf client))) (lista-clientes))) diff --git a/src/yes_she_codes/persistencia/compra.clj b/src/yes_she_codes/persistencia/compra.clj new file mode 100644 index 0000000..7d9a8fe --- /dev/null +++ b/src/yes_she_codes/persistencia/compra.clj @@ -0,0 +1,114 @@ +(ns yes-she-codes.persistencia.compra + (:use [clojure.pprint]) + (:require [yes-she-codes.logic :as logic] + [schema.core :as s] + [yes-she-codes.dominio.compra :as dominio.compra] + [yes-she-codes.db.compra :as db.compra] + [yes-she-codes.db.atom :as db.atom] + [java-time :as t])) + + +;SEMANA 1/SEMANA 3 +(s/defn nova-compra :- dominio.compra/CompraSchema + [[data, valor, estabelecimento, categoria, cartao]] + (let [format-data (logic/format-data-compra data) + format-long (logic/format-numero cartao)] + (dominio.compra/->Compra nil format-data valor estabelecimento categoria format-long))) + +(defn parametros-compras [] + (let [compras (db.compra/param-compras)] + (vec (map nova-compra compras)))) + +(defn lista-compras [] + (parametros-compras)) + +(defn total-gasto [compras] + (reduce + (map #(:valor %) compras))) + +(defn total-por-cartao [[cartao values]] + {:cartao cartao + :R$ (total-gasto values)}) + +(defn total-compras-por-cartao [compras] + (map total-por-cartao (group-by :cartao compras))) + + +(defn filtra-compras-data + [field value data] + (let [pattern (re-pattern (str "[0-9]{2}/" value "/[0-9]{4}")) + match-month #(re-matches pattern (field %))] + (filter #(= (match-month %) (field %)) data))) + +(defn buscar-por-mes + [mes compras] + (filtra-compras-data :data mes compras)) + + +(defn total-gasto-mes + [mes cartao compras] + (let [client-cartao (logic/filtra-data :cartao cartao compras) + data (buscar-por-mes mes client-cartao)] + (total-gasto data))) + + +(defn total-compras-por-categoria + [[categoria values]] + {:categoria categoria + :R$ (format "%.2f" (total-gasto values))}) + +(defn agrupar-por-categoria [compras] + (map total-compras-por-categoria + (group-by :categoria compras))) + +(defn filtrar-intervalo-compras + [minimo maximo compras] + (filter #(and (> (compare (% :valor) minimo) 0) (< (compare (% :valor) maximo) 0)) compras)) + +(defn agregate-compras [cartao] + (vec (logic/filtra-data :cartao cartao (lista-compras)))) + + +;SEMANA 2 +(defrecord Compra [^Long id ^String data ^BigDecimal valor ^String estabelecimento ^String categoria ^Long cartao]) + +(defn proximo-id [entidades] + (if-not (empty? entidades) + (+ 1 (apply max (map :id entidades))) 1)) + +(defn insere-compra [compras record] + (let [id (proximo-id compras)] + (conj compras (assoc record :id id)))) + +(defn insere-compra! [compra] + (swap! db.atom/repositorio-de-compras insere-compra compra)) + +(defn carrega-compra [] + (->> (db.compra/param-compras) + (map (fn [[data valor estabelecimento categoria cartao]] + (let [formatData (t/format "dd/MM/yyyy" (t/local-date data))] + (Compra. nil formatData valor estabelecimento categoria cartao)))) + (map insere-compra!))) + +(defn lista-compra! [] + (deref db.atom/repositorio-de-compras)) + +(defn pesquisa-compra-por-id! [id compras] + (logic/filtra-data :id id compras)) + +(defn exclui-entidade [entidades id] + (remove #(= id (:id %)) entidades)) + +(defn exclui-compra! [id] + (swap! db.atom/repositorio-de-compras exclui-entidade id)) + +(defn id->index [id] + (->> @db.atom/repositorio-de-compras + (map-indexed (fn [i v] [i v])) + (filter (fn [[_ v]] (= (:id v) id))) + ffirst)) + +(defn update-compra [id field value] + (swap! db.atom/repositorio-de-compras assoc-in [(id->index id) field] value)) + +(defn limpa-atom [] + (reset! db.atom/repositorio-de-compras [])) diff --git a/src/yes_she_codes/persistencia/datomic.clj b/src/yes_she_codes/persistencia/datomic.clj new file mode 100644 index 0000000..9e66656 --- /dev/null +++ b/src/yes_she_codes/persistencia/datomic.clj @@ -0,0 +1,142 @@ +(ns yes-she-codes.persistencia.datomic + (:require [datomic.api :as d] + [yes-she-codes.db.datomic :as datomic] + [yes-she-codes.csv.leitor-csv :as csv.leitor-csv])) + + +(defn- db-url [] + "datomic:dev://localhost:4334/she-codes") + +(defn cria-conexao [] + (d/create-database (db-url)) + (d/connect (db-url))) + +(defn apaga-banco! [] + (d/delete-database (db-url))) + +(defn cria-schema! [conn] + @(d/transact conn datomic/schema-datomic)) + + +(defn cartao->registro-datomic [cartao] + (-> cartao + (clojure.set/rename-keys {:id :db/id + :numero :cartao/numero + :cvv :cartao/cvv + :validate :cartao/validate + :limite :cartao/limite + :cliente :cartao/cliente}))) + +(defn registro-datomic->cartao [registro] + (-> registro + (clojure.set/rename-keys {:db/id :id + :cartao/numero :numero + :cartao/cvv :cvv + :cartao/validate :validate + :cartao/limite :limite + :cartao/cliente :cliente}))) + + +(defn pesquisa-cartao [snapshot numero] + (-> (d/q '[:find (pull ?id-cartao [*]) + :in $ ?numero + :where [?id-cartao :cartao/numero ?numero]] + snapshot numero) + ffirst + registro-datomic->cartao)) + + +(defn salva-cartao! [conn cartao] + (let [registro (cartao->registro-datomic cartao)] + @(d/transact conn [registro]))) + + +(defn lista-cartoes! [snapshot] + (mapv registro-datomic->cartao + (flatten (d/q '[:find (pull ?id-cartao [*]) + :where [?id-cartao :cartao/numero]] + snapshot)))) + + +(defn compra->registro-datomic [compra] + (-> compra + (clojure.set/rename-keys {:id :db/id + :data :compra/data + :valor :compra/valor + :cartao :compra/cartao + :categoria :compra/categoria + :estabelecimento :compra/estabelecimento}))) + + +(defn registro-datomic->compra [registro] + (-> registro + (clojure.set/rename-keys {:db/id :id + :compra/data :data + :compra/valor :valor + :compra/cartao :cartao + :compra/categoria :categoria + :compra/estabelecimento :estabelecimento}))) + + + +(defn salva-compra! [conn compra] + (some->> (:cartao compra) + (pesquisa-cartao (d/db conn)) + :id + (assoc compra :cartao) ; relaciona compra com ID do cartão + (compra->registro-datomic) + (conj []) + (d/transact conn) + deref)) + + +(defn lista-compras! [snapshot] + (vec (map registro-datomic->compra + (flatten (d/q '[:find (pull ?compra [*]) + :where [?compra :compra/valor]] + snapshot))))) + + + +(defn carrega-compras-no-banco! [conn] + (mapv + (partial salva-compra! conn) + (csv.leitor-csv/processa-arquivo-de-compras!))) + +(defn carrega-cartoes-no-banco! [conn] + (mapv + (partial salva-cartao! conn) + (csv.leitor-csv/processa-arquivo-de-cartoes!))) + + +(defn lista-compras-por-cartao! [snapshot cartao] + (->> (d/q '[:find (pull ?id-compra [*]) + :in $ ?cartao + :where [?id-compra :compra/cartao ?cartao]] + snapshot cartao) + flatten + (map registro-datomic->compra) + vec)) + + +(defn lista-compras-por-cartao-e-mes [snapshot cartao mes] + (-> '[:find (pull ?id-compra [*]) + :in $ ?cartao ?mes + :where [?id-compra :compra/cartao ?cartao] + [?id-compra :compra/data ?data] + [(yes-she-codes.persistencia.banco-de-dados-datomic/filtro-mes ?data ?mes)]] + (d/q snapshot [:cartao/numero cartao] mes) ; usa lookup ref para pesquisar pelo número do cartão + flatten + (#(mapv registro-datomic->compra %)))) + + +(def lista-gastos-por-categoria! + (comp (partial group-by :categoria) lista-compras!)) + + +(defn carrega-banco-de-dados! [] + (apaga-banco!) + (let [conexao (cria-conexao)] + (cria-schema! conexao) + (carrega-cartoes-no-banco! conexao) + (carrega-compras-no-banco! conexao))) diff --git a/src/yes_she_codes/semana1.clj b/src/yes_she_codes/semana1.clj deleted file mode 100644 index 9e143be..0000000 --- a/src/yes_she_codes/semana1.clj +++ /dev/null @@ -1,102 +0,0 @@ -(ns yes-she-codes.semana1 - (:require [yes-she-codes.db :as l.db] - [java-time :as t])) - -(defn novo-cliente - [[nome cpf email]] - (let [client (conj {:nome nome :cpf cpf :email email})] - client)) - -(defn novo-cartao - [[numero cvv limite validate cpf]] - (let [formatValidate (t/format "MM/yyyy" (t/year-month validate)) - client (conj {:numero numero, :cvv cvv, :limite limite :validate formatValidate :cpf cpf})] - client)) - -(defn nova-compra - [[data valor estabelecimento categoria cartao]] - (let [formatData (t/format "dd/MM/yyyy" (t/local-date data))] - (conj {:data formatData, :valor valor, :estabelecimento estabelecimento :categoria categoria :cartao cartao}))) - - -(defn parametros-clients [] - (let [clients (l.db/param-clientes)] - (vec (map novo-cliente clients)))) - - -(defn parametros-cartoes [] - (let [cartoes (l.db/param-cartoes)] - (vec (map novo-cartao cartoes)))) - -(defn parametros-compras [] - (let [compras (l.db/param-compras)] - (vec (map nova-compra compras)))) - -(defn lista-clientes [] - (parametros-clients)) - -(defn lista-cartoes [] - (parametros-cartoes)) - -(defn lista-compras [] - (parametros-compras)) - -(defn total-gasto [compras] - (reduce + (map #(:valor %) compras))) - -(defn filtra-data - [field value data] - (filter #(= value (field %)) data)) - -(defn filtra-compras-data - [field value data] - (let [pattern (re-pattern (str "[0-9]{2}/" value "/[0-9]{4}")) - match-month #(re-matches pattern (field %))] - (filter #(= (match-month %) (field %)) data))) - -(defn buscar-por-mes - [mes compras] - (filtra-compras-data :data mes compras)) - - -(defn total-gasto-mes - [mes cartao compras] - (let [client-cartao (filtra-data :cartao cartao compras) - data (filtra-compras-data :data mes client-cartao)] - (total-gasto data))) - - -(defn buscar-por-estabelecimento - [estabelecimento compras] - (filtra-data :estabelecimento estabelecimento compras)) - -(defn filtrar-intervalo-compras - [minimo maximo compras] - (filter #(and (> (compare (% :valor) minimo) 0) (< (compare (% :valor) maximo) 0)) compras)) - - -(defn total-compras-por-categoria - [[categoria values]] - {:categoria categoria - :R$ (format "%.2f" (total-gasto values))}) - - -(defn agrupar-por-categoria [compras] - (map total-compras-por-categoria - (group-by :categoria compras))) - -(defn agregate-cartao [cpf] - {:cartao (into {} (filtra-data :cpf cpf (lista-cartoes)))}) - -(defn agregate-compras [cartao] - (vec (filtra-data :cartao cartao (lista-compras)))) - -(defn agregate-client-cpf [cpf] - (let [client (into {} (filtra-data :cpf cpf (lista-clientes))) - cartao (agregate-cartao cpf) - compras (agregate-compras (:numero (:cartao cartao))) - conta (assoc-in cartao [:cartao :compras] compras)] - (conj client conta))) - -(defn agregate-clients [clients] - (map (fn [client] (agregate-client-cpf (:cpf client))) clients)) \ No newline at end of file diff --git a/src/yes_she_codes/semana2.clj b/src/yes_she_codes/semana2.clj deleted file mode 100644 index 2731a71..0000000 --- a/src/yes_she_codes/semana2.clj +++ /dev/null @@ -1,61 +0,0 @@ -(ns yes-she-codes.semana2 - (:require [yes-she-codes.semana1 :as y.s1] - [yes-she-codes.db :as y.db] - [java-time :as t])) - -(def repositorio-de-compras (atom [])) - -(defrecord Compra [^Long id ^String data ^BigDecimal valor ^String estabelecimento ^String categoria ^Long cartao]) - -(defn proximo-id [entidades] - (if-not (empty? entidades) - (+ 1 (apply max (map :id entidades))) 1)) - -(defn insere-compra [compras record] - (let [id (proximo-id compras)] - (conj compras (assoc record :id id)))) - - -(defn insere-compra! [compra] - (swap! repositorio-de-compras insere-compra compra)) - -(defn carrega-compra - [compras] - (->> compras - (map (fn [[data valor estabelecimento categoria cartao]] - (let [formatData (t/format "dd/MM/yyyy" (t/local-date data))] - (Compra. nil formatData valor estabelecimento categoria cartao)))) - (map insere-compra!))) - -(defn parametros-compras [] - (let [compras (y.db/param-compras)] - (carrega-compra compras))) - -(defn lista-compra [] - (parametros-compras)) - -(defn lista-compra! [repositorio-de-compras] - (deref repositorio-de-compras)) - -(defn pesquisa-compra-por-id [id compras] - (y.s1/filtra-data :id id (first compras))) - -(defn pesquisa-compra-por-id! [id compras] - (y.s1/filtra-data :id id compras)) - -(defn exclui-compra [id compras] - (remove #(= (:id %) id) compras)) - -(defn exclui-compra! [id compras] - (swap! compras (remove #(= (:id %) id) compras) compras)) - -;(println "Parametros Compras" (parametros-compras)) -;(println (Compra. nil "12/12/1221" 121.0 "Extra" "Alimentacao" "218928192819")) - -(println "Repositorio de compras" @repositorio-de-compras) -(println "lista compra" (lista-compra)) -(println "lista compra!" (lista-compra! repositorio-de-compras)) -(println "lista id" (pesquisa-compra-por-id 1 (parametros-compras))) -(println "Atom" (pesquisa-compra-por-id! 1 (deref repositorio-de-compras))) -;(println "Exclui" (exclui-compra 1 (parametros-compras))) -;(println "Exclui!" (exclui-compra! 1 repositorio-de-compras)) diff --git a/src/yes_she_codes/semana3.clj b/src/yes_she_codes/semana3.clj deleted file mode 100644 index 9d306f5..0000000 --- a/src/yes_she_codes/semana3.clj +++ /dev/null @@ -1,67 +0,0 @@ -(ns yes-she-codes.semana3 - (:use clojure.pprint) - (:require [schema.core :as s] - [clojure.string :as str])) - -(s/set-fn-validation! true) - -(defn validate-email [email] - (re-matches #".+\@.+\..+" email)) - -(defn validate-cpf [cpf] - (re-matches #"\d{3}[\.]?\d{3}[\.]?\d{3}[-]?\d{2}" cpf)) - -(defn validate-string [string] - (>= (count string) 2)) - -(def ClienteSchema - "Schema de um paciente" - {:nome (s/constrained s/Str validate-string) , - :cpf (s/constrained s/Str validate-cpf), - :email (s/constrained s/Str validate-email)}) - -(defn validate-numero [numero] - (and (pos? numero) (< numero 1e+16))) - -(defn validate-cvv [cvv] - (and (pos? cvv) (< cvv 999))) - -(defn validate-data-cartao [validate] - (re-matches #"\d{2}[/]?\d{4}" validate)) - -(def CartaoSchema - {:numero (s/constrained s/Num validate-numero) , - :cvv (s/constrained s/Num validate-cvv) , - :validate (s/constrained s/Str validate-data-cartao), - :limite (s/constrained s/Num pos?), - :cliente (s/constrained s/Str validate-cpf)}) - -(defn validate-data [data] - (re-matches #"[0-9]{2}[/]?[0-9]{2}[/]?[0-9]{4}" data)) - -(defn validate-categoria [categoria] - (let [lista-categoria ["Alimentação", "Automovel", "Casa", "Educação", "Lazer", "Saúde"]] - (str/includes? lista-categoria categoria))) - -(def CompraSchema - {:data (s/constrained s/Str validate-data) , - :valor (s/constrained s/Num pos?) , - :estabelecimento (s/constrained s/Str validate-string), - :categoria (s/constrained s/Str validate-categoria), - :cartao (s/constrained s/Num validate-numero)}) - - -(s/defn novo-cliente :- ClienteSchema - [nome :- s/Str, cpf :- s/Str, email :- s/Str] - {:nome nome, :cpf cpf, :email email}) - -(s/defn novo-cartao :- CartaoSchema - [numero :- s/Num, cvv :- s/Num, validate :- s/Str, limite :- s/Num, cliente :- s/Str] - {:numero numero, :cvv cvv, :validate validate, :limite limite, :cliente cliente}) - -(s/defn nova-compra :- CompraSchema - [data :- s/Str, valor :- s/Num, estabelecimento :- s/Str, categoria :- s/Str, cartao :- s/Num] - {:data data, :valor valor, :estabelecimento estabelecimento, :categoria categoria :cartao cartao}) - - - diff --git a/src/yes_she_codes/util.clj b/src/yes_she_codes/util.clj deleted file mode 100644 index bb888a7..0000000 --- a/src/yes_she_codes/util.clj +++ /dev/null @@ -1,33 +0,0 @@ -(ns yes-she-codes.util - (:require [java-time :as time])) - -(defn str->long [valor] - (Long/parseLong (clojure.string/replace valor #" " ""))) - - -(defprotocol ExtratorDeMes - (mes-da-data [data] - "Dada uma data, extrai o valor do mês como um inteiro de 1 a 12")) - -(extend-type java.lang.String ExtratorDeMes - (mes-da-data [data] - (->> data - (re-matches #"\d{4}-(\d{2})-\d{2}") - second - Long/valueOf))) - -(extend-type java.time.LocalDate ExtratorDeMes - (mes-da-data [data] - (.getValue (time/month data)))) - -(defn data-valida? [data] - (re-matches #"\d{4}-(\d{2})-\d{2}" data)) - - -(defn proximo-id [entidades] - (if-not (empty? entidades) - (+ 1 (apply max (map :id entidades))) - 1)) - - - diff --git a/test/yes_she_codes/core_test.clj b/test/yes_she_codes/core_test.clj index e959f21..4fe86e1 100644 --- a/test/yes_she_codes/core_test.clj +++ b/test/yes_she_codes/core_test.clj @@ -13,6 +13,6 @@ (is (= [{:nome "Emillyn", :cpf "1829182918", :email "testando"}]) (cria-map "Emillyn" "1829182918" "testando"))) (testing "verifica se cria um novo cartao corretamente" - (is (= {:numero 12819289182918, :cvv 233, :validate "13/2222" :limite 1000.0 :cpf "238923928"} ) + (is (= {:numero 12819289182918, :cvv 233, :validate "13/2222" :limite 1000.0 :cpf "238923928"}) (cria-cartao 12819289182918 233 "2022/12" 1000.0 "238923928"))))