summaryrefslogtreecommitdiffstats
path: root/main.scm
diff options
context:
space:
mode:
Diffstat (limited to 'main.scm')
-rw-r--r--main.scm47
1 files changed, 34 insertions, 13 deletions
diff --git a/main.scm b/main.scm
index 8f05d97..10213a0 100644
--- a/main.scm
+++ b/main.scm
@@ -4,11 +4,12 @@
(import sqlite3)
(import srfi-69)
(import (chicken random))
+(import (chicken process-context))
;; db open and create
-(define db (open-database "/tmp/54itter.db"))
+(define db (open-database (or (get-environment-variable "DB_FILE") "/tmp/54itter.db")))
(when (zero? (first-result db "SELECT count(*) FROM sqlite_master WHERE type='table' AND name='posts'"))
(execute db "CREATE TABLE posts (
@@ -82,6 +83,9 @@
GROUP BY p.id, p.user_id, p.content, p.created_at, p.parent_id
ORDER BY count(c.id) DESC, p.created_at DESC LIMIT 25" post-id))
+(define (delete-post post-id current-user)
+ (execute db "DELETE FROM posts WHERE id = ? AND user_id = ?" post-id current-user))
+
;(define (top-posts) (map-row list db
; "SELECT id, user_id, content, created_at
; FROM posts
@@ -113,17 +117,23 @@
[apikey (if cookie (cdr cookie) #f)])
(if apikey (lookup-user apikey) #f)))
-(define (post p)
- `(div (@ [class "post"] [id ,(id p)])
- (p ,(content p))
- (div [@ (class "author")]
- (em "- " ,(user p))
- (span (@ [style "float:right;"])
- (a (@ [href ,(string-append "/posts/" (id p))] [style "margin-right:2px;"])
- ,(children-count p) " comments")
- ,(created-at p)
- ))
- ))
+(define (post current-user)
+ (lambda (p)
+ (let ([uri (string-append "/posts/" (id p))])
+ `(div (@ [class "post"] [id ,(id p)])
+ ,(if (equal? current-user (user p))
+ `(a (@ [href ,(string-append uri "/delete")]
+ [class "delete-button"])
+ "[delete]")
+ '())
+ (p ,(content p))
+ (div [@ (class "post-info")]
+ (em "- " ,(user p))
+ (span (@ [style "float:right;"])
+ (a (@ [href ,uri] [style "margin-right:2px;"])
+ "[" ,(children-count p) " comments]")
+ ,(created-at p)
+ ))))))
(define new-post-form
`(form (@ [class "new-post-form"] [action "/"] [method "POST"])
@@ -185,7 +195,7 @@
,(if current-user new-post-form '())
,(if (null? displayed-posts)
"No posts yet!"
- (map post displayed-posts))
+ (map (post current-user) displayed-posts))
)
)))
@@ -260,6 +270,17 @@
headers: '((content-type text/html))
status: 'ok
body: (posts-page current-user displayed-posts)))))
+ ; Has method GET so that it can be used from <a> tag links
+ (GET (/ "posts" ":id" "delete") ,(lambda (rt)
+ (let* ([current-user (get-current-user)]
+ [post-id (get-route-param rt "id")]
+ [succeeded? (delete-post post-id current-user)])
+ (if succeeded?
+ (send-response
+ headers: `((location "/"))
+ status: 'see-other
+ )
+ (send-status 'internal-server-error "Deletion Failed")))))
(GET (/ "posts") ,(lambda (rt)
(send-response