Skip to content

Instantly share code, notes, and snippets.

@aconanlai
Created November 3, 2017 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aconanlai/53284043ea8aaf9b76b334685a378dfd to your computer and use it in GitHub Desktop.
Save aconanlai/53284043ea8aaf9b76b334685a378dfd to your computer and use it in GitHub Desktop.
(ns clj-youtube-server.core
(:require [clojure.java.jdbc :as sql]
[compojure.core :refer :all]
[compojure.handler :as handler]
[ring.middleware.json :as middleware]
[ring.adapter.jetty :as ring]
[compojure.route :as route]))
(def spec (or (System/getenv "DATABASE_URL")
"postgresql://localhost:5432/youtuber"))
(defn db-exists?
[name]
(-> (sql/query spec
[(str "select count(*) from information_schema.tables where table_name='" name "'")])
first :count pos?))
(defn create-table
[name]
(when-not (db-exists? name)
(sql/db-do-commands spec
(sql/create-table-ddl name [[:comment "varchar(120)"] [:time :int]]))))
(defroutes app-routes
; serve root
(GET "/" [] {:status 200
:body {}})
; serve a specific video id
(GET "/video/:id" [id]
{:status 200
:body {:id id
:comments (get-comments id)}})
(POST "/video"
request
(let [id (get-in request [:params :id]) comment (get-in request [:params :comment]) time (get-in request [:params :time])]
(do
(create-table id)
(create-comment id comment time)
{:status 200
:body {:id id
:comment comment}}))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment