(in-package :cl-user)

(defpackage :my-blog
  (:use :common-lisp)
)


(in-package :my-blog)
(require :hunchentoot)
(require :html-template)
(require :elephant)
(use-package :elephant)

(defpclass blog-post ()
  ((title :initarg :title
          :accessor title
)

   (body :initarg :body
         :accessor body
)

   (timestamp :initarg :timestamp
              :accessor timestamp
              :initform (get-universal-time)
)
)
)


; Open the store where our data is stored
(defvar *elephant-store* (open-store '(:clsql (:sqlite3 "/tmp/blog.db"))))

; Container for all our blog posts
(defvar *blog-posts* (or (get-from-root "blog-posts")
                         (let ((blog-posts (make-pset)))
                           (add-to-root "blog-posts" blog-posts)
                           blog-posts
)
)
)


; Make sure html-template looks for files in the right directory
(setq html-template:*default-template-pathname* #P"/Users/vetler/Documents/devel/blog/source/")
(defun generate-index-page ()
  "Generate the index page showing all the blog posts."
  (with-output-to-string (stream)
    (html-template:fill-and-print-template
     #P"index.tmpl"
     (list :blog-posts
           (loop for blog-post in (pset-list *blog-posts*)
              collect (list :title (title blog-post) :body (body blog-post))
)
)

     :stream stream
)
)
)


(setq hunchentoot:*dispatch-table*
      (list (hunchentoot:create-prefix-dispatcher "/" 'generate-index-page))
)

(defvar *ht-server* (hunchentoot:start-server :port 8080))