;; MAIN.LISP
;; ========================

(in-package :doormat)

(defun increment-visitors (url)
  "Increment the visitor count of a set URL by one"

  (simple-sql:init "doormat")

  (let ((visitor-count
          (nth 1
               (nth 0
                    (simple-sql:fetch-where "entries"
                                            "url" (stra url))))))
    (if visitor-count
      (simple-sql:update-where "entries" "visitors" nil
                               (format nil "~A" (+ visitor-count 1))
                               "url" (stra url)))))


(defun create-entry (url)
  "Create a new entry for a set URL (with one initial visit)!"

  (simple-sql:init "doormat")
  
  (simple-sql:insert-row "entries" (stra url ) 1))


(defun fetch-visitors (url)
  "Fetches the visitor-count of a URL."

  (simple-sql:init "doormat")

  (nth 1 (nth 0 (simple-sql:fetch-where "entries" "url" (stra url)))))


(defun stra (string)
  "Encases a string in apostrophes."

  (concatenate `string "'" string "'"))