From 6eb21540a594cfb4df7a931c8ac945728a6fd976 Mon Sep 17 00:00:00 2001 From: dan Date: Tue, 28 Feb 2023 11:31:48 -0500 Subject: implement delete button --- main.scm | 47 ++++++++++++++++++++++++++++++++++------------- style.css | 10 ++++++++-- 2 files changed, 42 insertions(+), 15 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 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 diff --git a/style.css b/style.css index c6f0172..503f976 100644 --- a/style.css +++ b/style.css @@ -98,8 +98,8 @@ tr:nth-child(even) { margin-top: 2px; } -.author { - font-size: 12pt; +.post-info { + font-size: 0.8em; } .new-post-form { @@ -152,3 +152,9 @@ textarea { font-size: 1em; font-family: Lato, FreeSans, Roboto, Helvetica, Sans-Serif; } + +.delete-button { + margin-left:2px; + float:right; + font-size:0.8em +} -- cgit v1.2.3