Skip to content

Commit

Permalink
retrieving categories and articles from db
Browse files Browse the repository at this point in the history
  • Loading branch information
fabrv committed Feb 22, 2021
1 parent 8f5144e commit ac6d960
Show file tree
Hide file tree
Showing 17 changed files with 285 additions and 56 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ node_modules/
.cljs_node_repl/
out/
.cpcache
.lsp/
.lsp/
*.log
130 changes: 130 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
3 changes: 2 additions & 1 deletion public/stylesheets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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;
}
}
45 changes: 33 additions & 12 deletions src/sindicato_ufm/core.cljs
Original file line number Diff line number Diff line change
@@ -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))))))

Expand Down
2 changes: 0 additions & 2 deletions src/sindicato_ufm/routes/article.cljs

This file was deleted.

27 changes: 22 additions & 5 deletions src/sindicato_ufm/routes/index.cljs
Original file line number Diff line number Diff line change
@@ -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)
11 changes: 0 additions & 11 deletions src/sindicato_ufm/routes/users.cljs

This file was deleted.

19 changes: 19 additions & 0 deletions src/sindicato_ufm/services/articles.cljs
Original file line number Diff line number Diff line change
@@ -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)))))
7 changes: 7 additions & 0 deletions src/sindicato_ufm/services/categories.cljs
Original file line number Diff line number Diff line change
@@ -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)))))
29 changes: 29 additions & 0 deletions src/sindicato_ufm/services/index.cljs
Original file line number Diff line number Diff line change
@@ -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)))))))))
Loading

0 comments on commit ac6d960

Please sign in to comment.