Created
January 26, 2014 01:36
-
-
Save mntmn/8626797 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ql:quickload "ningle") | |
(ql:quickload "cl-mongo") | |
(use-package 'cl-mongo) | |
(db.use "test") | |
(defvar *collections* | |
(list | |
(list "pages" "title" "content") | |
(list "products" "title" "content" "price") | |
(list "news_items" "date" "author" "content"))) | |
(defvar *app* (make-instance 'ningle:<app>)) | |
(defun get-doc-by-key (collection key) | |
(first (docs (db.find collection (kv "key" key))))) | |
(defun get-all-docs (collection) | |
(docs (db.find collection :all))) | |
(defun get-all-keys (collection) | |
(map 'list #'(lambda (x) (get-element "key" x)) (get-all-docs collection))) | |
(defun get-collection-fields (collection) | |
(cdr (find collection *collections* :test #'(lambda (x y) (string= x (car y)))))) | |
(defun get-title-field (collection) | |
(car (get-collection-fields collection))) | |
(defun get-title-value (collection doc) | |
(get-element (get-title-field collection) doc)) | |
(defun get-form-fields (collection doc) | |
(map 'list #'(lambda (x) (format nil "<label>~A: <input name='~A' value='~A'></label><br/>" x x (get-element x doc))) (get-collection-fields collection))) | |
(defun get-all-items-as-html (collection) | |
(map 'list #'(lambda (x) (format nil "<li><a href='/~A/~A'>~A</a></li>" | |
collection (get-element "key" x) (get-title-value collection x))) (get-all-docs collection))) | |
(defun get-collections-as-html () | |
(map 'list #'(lambda (x) (format nil "<li><a href='/~A'>~A</a></li>" (car x) (car x))) *collections*)) | |
(setf (ningle:route *app* "/") | |
#'(lambda (params) | |
(format nil "Welcome. All collections: <ul>~{~A~^~}</ul>" (get-collections-as-html)))) | |
(setf (ningle:route *app* "/:collection") | |
#'(lambda (params) | |
(let* ((collection (getf params :collection))) | |
(format nil "All items in ~A: <ul>~{~A~^~}</ul> | |
<form action='/~A/new' method='POST'> | |
<input name='key' placeholder='key'> | |
<input type='submit' value='new item'> | |
</form>" collection (get-all-items-as-html collection) collection)))) | |
(setf (ningle:route *app* "/:collection/new" :method :POST) | |
#'(lambda (params) | |
(let* ((collection (getf params :collection)) | |
(key (getf params :|key|))) | |
(format nil "<h1>New: ~A</h1> | |
<form method='POST' action='/~A/~A'> | |
~A | |
<input type='submit' value='ok'></form>" | |
key collection key (get-form-fields collection (make-document))) | |
))) | |
(setf (ningle:route *app* "/:collection/:id" :method :GET) | |
#'(lambda (params) | |
(let* ((collection (getf params :collection)) | |
(the-doc (get-doc-by-key collection (getf params :id))) | |
(key (get-element "key" the-doc)) | |
(content (get-element "content" the-doc))) | |
(if (eq nil the-doc) | |
"Document not found." | |
(format nil "<h1>~A</h1><p>~A</p> | |
<a href='/~A/~A/edit'>Edit</a> <a href='/'>List</a>" key content collection key)) | |
))) | |
(setf (ningle:route *app* "/:collection/:id/edit" :method :GET) | |
#'(lambda (params) | |
(let* ((collection (getf params :collection)) | |
(the-doc (get-doc-by-key collection (getf params :id))) | |
(key (get-element "key" the-doc))) | |
(format nil "<h1>Edit: ~A</h1> | |
<form method='POST' action='/~A/~A'> | |
~A | |
<input type='submit' value='ok'></form>" | |
key collection key (get-form-fields collection the-doc)) | |
))) | |
(setf (ningle:route *app* "/:collection/:id" :method :POST) | |
#'(lambda (params) | |
(let* ((collection (getf params :collection)) | |
(the-doc (get-doc-by-key collection (getf params :id))) | |
(the-doc (if (eq nil the-doc) (make-document) the-doc)) | |
(the-id (getf params :id)) | |
(content (getf params :|content|))) | |
(progn | |
(add-element "key" the-id the-doc) | |
(loop | |
for field in (get-collection-fields collection) | |
do (progn | |
(print (format nil "add-element: ~A ~A ~A" (intern field) params (getf params (intern field :keyword)))) | |
(add-element field (getf params (intern field :keyword)) the-doc))) | |
(db.save collection the-doc) | |
(format nil "Saved. <a href='/~A/~A'>back</a>" collection the-id)) ))) | |
(clack:clackup *app*) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment