Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(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
You can’t perform that action at this time.