diff --git a/.gitignore b/.gitignore index f776c39..68895eb 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ node_modules/ .cljs_node_repl/ out/ .cpcache -.lsp/ \ No newline at end of file +.lsp/ +*.log \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index d54d0c9..a8745df 100644 --- a/package-lock.json +++ b/package-lock.json @@ -161,6 +161,11 @@ "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz", "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==" }, + "buffer-writer": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/buffer-writer/-/buffer-writer-2.0.0.tgz", + "integrity": "sha512-a7ZpuTZU1TRtnwyCNW3I5dc0wWNC3VR9S++Ewyk2HHZdrO3CQJqSpd+95Us590V6AL7JqUAH2IwZ/398PmNFgw==" + }, "bytes": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz", @@ -998,6 +1003,11 @@ } } }, + "packet-reader": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/packet-reader/-/packet-reader-1.0.0.tgz", + "integrity": "sha512-HAKu/fG3HpHFO0AA8WE8q2g+gBJaZ9MG7fcKk+IJPLTGAD6Psw4443l+9DGRbOIh3/aXr7Phy0TjilYivJo5XQ==" + }, "parseurl": { "version": "1.3.3", "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", @@ -1013,12 +1023,89 @@ "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" }, + "pg": { + "version": "8.5.1", + "resolved": "https://registry.npmjs.org/pg/-/pg-8.5.1.tgz", + "integrity": "sha512-9wm3yX9lCfjvA98ybCyw2pADUivyNWT/yIP4ZcDVpMN0og70BUWYEGXPCTAQdGTAqnytfRADb7NERrY1qxhIqw==", + "requires": { + "buffer-writer": "2.0.0", + "packet-reader": "1.0.0", + "pg-connection-string": "^2.4.0", + "pg-pool": "^3.2.2", + "pg-protocol": "^1.4.0", + "pg-types": "^2.1.0", + "pgpass": "1.x" + } + }, + "pg-connection-string": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.4.0.tgz", + "integrity": "sha512-3iBXuv7XKvxeMrIgym7njT+HlZkwZqqGX4Bu9cci8xHZNT+Um1gWKqCsAzcC0d95rcKMU5WBg6YRUcHyV0HZKQ==" + }, + "pg-int8": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz", + "integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==" + }, + "pg-pool": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.2.2.tgz", + "integrity": "sha512-ORJoFxAlmmros8igi608iVEbQNNZlp89diFVx6yV5v+ehmpMY9sK6QgpmgoXbmkNaBAx8cOOZh9g80kJv1ooyA==" + }, + "pg-protocol": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.4.0.tgz", + "integrity": "sha512-El+aXWcwG/8wuFICMQjM5ZSAm6OWiJicFdNYo+VY3QP+8vI4SvLIWVe51PppTzMhikUJR+PsyIFKqfdXPz/yxA==" + }, + "pg-types": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz", + "integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==", + "requires": { + "pg-int8": "1.0.1", + "postgres-array": "~2.0.0", + "postgres-bytea": "~1.0.0", + "postgres-date": "~1.0.4", + "postgres-interval": "^1.1.0" + } + }, + "pgpass": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.4.tgz", + "integrity": "sha512-YmuA56alyBq7M59vxVBfPJrGSozru8QAdoNlWuW3cz8l+UX3cWge0vTvjKhsSHSJpo3Bom8/Mm6hf0TR5GY0+w==", + "requires": { + "split2": "^3.1.1" + } + }, "picomatch": { "version": "2.2.2", "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.2.2.tgz", "integrity": "sha512-q0M/9eZHzmr0AulXyPwNfZjtwZ/RBZlbN3K3CErVrk50T2ASYI7Bye0EvekFY3IP1Nt2DHu0re+V2ZHIpMkuWg==", "dev": true }, + "postgres-array": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz", + "integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==" + }, + "postgres-bytea": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.0.tgz", + "integrity": "sha1-AntTPAqokOJtFy1Hz5zOzFIazTU=" + }, + "postgres-date": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.7.tgz", + "integrity": "sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==" + }, + "postgres-interval": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz", + "integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==", + "requires": { + "xtend": "^4.0.0" + } + }, "prepend-http": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/prepend-http/-/prepend-http-2.0.0.tgz", @@ -1092,6 +1179,16 @@ "strip-json-comments": "~2.0.1" } }, + "readable-stream": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.0.tgz", + "integrity": "sha512-BViHy7LKeTz4oNnkcLJ+lVSL6vpiFeX6/d3oSH8zCW7UxP2onchk+vTGB143xuFjHS3deTgkKoXXymXqymiIdA==", + "requires": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + } + }, "readdirp": { "version": "3.5.0", "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.5.0.tgz", @@ -1224,6 +1321,14 @@ "source-map": "^0.6.0" } }, + "split2": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/split2/-/split2-3.2.2.tgz", + "integrity": "sha512-9NThjpgZnifTkJpzTZ7Eue85S49QwpNhZTq6GRJwObb6jnLFNGB7Qm73V5HewTROPyxD0C29xqmaI68bQtV+hg==", + "requires": { + "readable-stream": "^3.0.0" + } + }, "statuses": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", @@ -1269,6 +1374,21 @@ } } }, + "string_decoder": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", + "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "requires": { + "safe-buffer": "~5.2.0" + }, + "dependencies": { + "safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==" + } + } + }, "strip-ansi": { "version": "5.2.0", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-5.2.0.tgz", @@ -1411,6 +1531,11 @@ "prepend-http": "^2.0.0" } }, + "util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" + }, "utils-merge": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", @@ -1457,6 +1582,11 @@ "resolved": "https://registry.npmjs.org/xdg-basedir/-/xdg-basedir-4.0.0.tgz", "integrity": "sha512-PSNhEJDejZYV7h50BohL09Er9VaIefr2LMAf3OEmpCkjOi34eYyQYAXUTjEQtZJTKcF0E2UKTh+osDLsgNim9Q==", "dev": true + }, + "xtend": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", + "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==" } } } diff --git a/package.json b/package.json index f07afb8..838b536 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "express": "^4.17.1", "express-handlebars": "^5.2.1", "morgan": "^1.10.0", + "pg": "^8.5.1", "source-map-support": "^0.5.19" }, "devDependencies": { diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css index 7dc003e..55695f6 100644 --- a/public/stylesheets/style.css +++ b/public/stylesheets/style.css @@ -49,6 +49,7 @@ h1, h2, h3, p, ol, ul, blockquote { list-style-type: none; padding: 0; margin: 0; + text-transform: uppercase; } .header .links li{ @@ -715,4 +716,4 @@ input[type=text]:focus ~ .search-suggestions, .search-suggestions:focus, .search .tab-pill .selected:hover, .tab-pill .selected:focus { background-color: #235174; -} \ No newline at end of file +} diff --git a/src/sindicato_ufm/core.cljs b/src/sindicato_ufm/core.cljs index 44ff72d..d46b018 100644 --- a/src/sindicato_ufm/core.cljs +++ b/src/sindicato_ufm/core.cljs @@ -1,31 +1,52 @@ (ns sindicato-ufm.core (:require - [sindicato-ufm.routes.users :as users] [sindicato-ufm.routes.index :as index] - [clojure.string :as string])) + [clojure.string :as string] path)) (def express (js/require "express")) (def logger (js/require "morgan")) +(def path (js/require "path")) +(def pg (js/require "pg")) + +(def client (new (. pg -Client) #js{:connectionString "postgres://yccfbeasuxbqjy:89298dfa903a83f89fb9e4ba35c25584f0363302379a75b7f028711033828ab2@ec2-184-73-153-64.compute-1.amazonaws.com:5432/dfp3idvcven38h" + :ssl #js{:rejectUnauthorized false}})) + + (def exphbs (js/require "express-handlebars")) (def app (express)) (defn port [] - (if process.env.PORT process.env.PORT 3000)) + (if (.. js/process -env -PORT) (.. js/process -env -PORT) 3000)) + +(defn str-to-link [str] + (string/lower-case (string/replace str #" " "-"))) -(defn static[root] (.static express root)) +(def hbs-config {:extname ".hbs" + :defaultLayout "layout" + :helpers {:toLink str-to-link}}) -(defn hbs [] - (.create exphbs #js{"extname" ".hbs" - "defaultLayout" "layout" - "helpers" #js{"toLink" (fn [context] (string/replace context #" " "-"))}})) +(def hbs + (.create exphbs (clj->js hbs-config))) + +() (defn start [] (-> - (.set app "view engine" "hbs") + (.connect client) + (.then (fn [_] + (.log js/console "connected to pg"))) + (.catch (fn [e] (.log js/console e)))) + + (-> + ; Engine setup + (.engine app ".hbs" (. hbs -engine)) + (.set "view engine" ".hbs") + ; Middlewares (.use (logger "dev")) - (.use (static "public")) - (.use "/" (index/index-routes)) - (.use "/users" (users/users-routes)) + (.use (.static express "public")) + ; Routes + (.use "/" (index/index-routes client)) + ; Starting the server (.listen (port) [] (println (str "Running at http://localhost:" (port)))))) diff --git a/src/sindicato_ufm/routes/article.cljs b/src/sindicato_ufm/routes/article.cljs deleted file mode 100644 index 79a3d66..0000000 --- a/src/sindicato_ufm/routes/article.cljs +++ /dev/null @@ -1,2 +0,0 @@ -(ns sindicato-ufm.routes.index) - diff --git a/src/sindicato_ufm/routes/index.cljs b/src/sindicato_ufm/routes/index.cljs index e0f02b7..9dc8206 100644 --- a/src/sindicato_ufm/routes/index.cljs +++ b/src/sindicato_ufm/routes/index.cljs @@ -1,11 +1,28 @@ -(ns sindicato-ufm.routes.index) +(ns sindicato-ufm.routes.index + (:require + [sindicato-ufm.services.index :as index])) + (def express (js/require "express")) (def router (.Router express)) -(defn index [] #js{"title" "Clojurexpress"}) +(defn index-routes [pgclient] + (.get router + "/" + (fn [_ res] + (index/category pgclient "opinion" 0 + (fn [data] + (.render res "index" data))))) + + (.get router + "/articulo/:article" + (fn [req res] + )) -(defn index-routes [] - (.get router "/" (fn [_ res] - (.render res "index" (index)))) + (.get router + "/:category" + (fn [req res] + (index/category pgclient (.. req -params -category) 0 + (fn [data] + (.render res "index" data))))) router) diff --git a/src/sindicato_ufm/routes/users.cljs b/src/sindicato_ufm/routes/users.cljs deleted file mode 100644 index dd13e36..0000000 --- a/src/sindicato_ufm/routes/users.cljs +++ /dev/null @@ -1,11 +0,0 @@ -(ns sindicato-ufm.routes.users) -(def express (js/require "express")) -(def router (.Router express)) - -(defn users [] #js{"name" "Juan" "lastname" "Ramirez"}) - -(defn users-routes [] - (.get router "/" (fn [_ res] - (.send res (users)))) - - router) \ No newline at end of file diff --git a/src/sindicato_ufm/services/articles.cljs b/src/sindicato_ufm/services/articles.cljs new file mode 100644 index 0000000..ee29425 --- /dev/null +++ b/src/sindicato_ufm/services/articles.cljs @@ -0,0 +1,19 @@ +(ns sindicato-ufm.services.articles) + +(defn get-all [pgclient limit offset callback] + (-> + (.query pgclient "select * from article limit $1 offset $2" #js[limit offset]) + (.then (fn [a] (callback (js->clj (. a -rows)) nil))) + (.catch (fn [e] (callback nil e))))) + +(defn get-row [pgclient id callback] + (-> + (.query pgclient "select * from article where id = $1" #js[id]) + (.then (fn [a] (callback (js->clj (. a -rows)) nil))) + (.catch (fn [e] (callback nil e))))) + +(defn get-by-category [pgclient category limit offset callback] + (-> + (.query pgclient "select * from article where category = $1 limit $2 offset $3" #js[category limit offset]) + (.then (fn [a] (callback (js->clj (. a -rows)) nil))) + (.catch (fn [e] (callback nil e))))) \ No newline at end of file diff --git a/src/sindicato_ufm/services/categories.cljs b/src/sindicato_ufm/services/categories.cljs new file mode 100644 index 0000000..45e98a4 --- /dev/null +++ b/src/sindicato_ufm/services/categories.cljs @@ -0,0 +1,7 @@ +(ns sindicato-ufm.services.categories) + +(defn get-all [pgclient limit offset callback] + (-> + (.query pgclient "select * from category order by id desc limit $1 offset $2" #js[limit offset]) + (.then (fn [a] (callback (js->clj (. a -rows)) nil))) + (.catch (fn [e] (callback nil e))))) \ No newline at end of file diff --git a/src/sindicato_ufm/services/index.cljs b/src/sindicato_ufm/services/index.cljs new file mode 100644 index 0000000..f13a2ea --- /dev/null +++ b/src/sindicato_ufm/services/index.cljs @@ -0,0 +1,29 @@ +(ns sindicato-ufm.services.index + (:require + [sindicato-ufm.services.categories :as cats] + [sindicato-ufm.services.articles :as arts])) + +(defn index-view [title links articles] + (clj->js { + :title title + :links links + :articles articles + })) + +(defn category [pgclient category page callback] + (arts/get-by-category + pgclient + category + 6 + (* page 6) + (fn [articles err] + (if err + (throw err) + (cats/get-all + pgclient + 100 + 0 + (fn [categories err] + (if err + (throw err) + (callback (index-view "Opinión" categories articles))))))))) \ No newline at end of file diff --git a/src/sindicato_ufm/utils/clex.cljs b/src/sindicato_ufm/utils/clex.cljs new file mode 100644 index 0000000..c2eb135 --- /dev/null +++ b/src/sindicato_ufm/utils/clex.cljs @@ -0,0 +1,4 @@ +(ns sindicato-ufm.utils.clex) + +(defn params [req param] + ((js->clj (. req -params)) param)) \ No newline at end of file diff --git a/views/article.hbs b/views/article.hbs deleted file mode 100644 index 7a0887f..0000000 --- a/views/article.hbs +++ /dev/null @@ -1,21 +0,0 @@ -
-

{{{headline}}}

-

{{author}} - {{date}}

-

{{{subhead}}}

-
{{{body}}}
-
- ¿Te gustó el artículo? - - Comparte en Facebook - , - - Twitter - o síguenos en las - redes -
-
-
\ No newline at end of file diff --git a/views/index.hbs b/views/index.hbs index e64a04d..a39806b 100644 --- a/views/index.hbs +++ b/views/index.hbs @@ -1,2 +1 @@ -

{{title}}

-

Welcome to {{title}}

\ No newline at end of file +{{>articles articles=articles}} \ No newline at end of file diff --git a/views/layout.hbs b/views/layouts/layout.hbs similarity index 93% rename from views/layout.hbs rename to views/layouts/layout.hbs index 24a0811..78be62a 100644 --- a/views/layout.hbs +++ b/views/layouts/layout.hbs @@ -8,7 +8,7 @@ - {{{header}}} + {{>header links=links}}
{{{body}}}
diff --git a/views/partials/articles.hbs b/views/partials/articles.hbs new file mode 100644 index 0000000..bdeb2be --- /dev/null +++ b/views/partials/articles.hbs @@ -0,0 +1,21 @@ +{{#each articles}} +
+

{{{this.headline}}}

+

{{this.author}} - {{this.date}}

+

{{{this.subhead}}}

+
{{{this.body}}}
+
+ ¿Te gustó el artículo? + Compartílo + + Twitter + o síguenos en las + redes +
+
+
+{{/each}} \ No newline at end of file diff --git a/views/partials/header.hbs b/views/partials/header.hbs new file mode 100644 index 0000000..8d26ed9 --- /dev/null +++ b/views/partials/header.hbs @@ -0,0 +1,13 @@ +
+

EL SINDICATO

+ +
\ No newline at end of file