Permalink
Browse files

Revise routes and add a deletion API method.

  • Loading branch information...
1 parent 373ba14 commit a82c61158ca70e455a4983d4b7a5633b773aeb9e @Raynes Raynes committed Jan 5, 2012
Showing with 18 additions and 4 deletions.
  1. +18 −4 src/refheap/views/api.clj
View
@@ -35,8 +35,8 @@
(when-let [id (:id (session/get :user))]
(api/new-token id)))
-(defpage [:post "/api/paste/create"] {:keys [private contents language username token]
- :or {private "false"}}
+(defpage [:post "/api/paste"] {:keys [private contents language username token]
+ :or {private "false"}}
(response/json
(let [user (api/validate-user username token)]
(if (string? user)
@@ -46,15 +46,15 @@
{:error paste}
(api/process-paste paste)))))))
-(defpage [:post "/api/paste/:id/edit"] {:keys [id private contents language username token]}
+(defpage [:post "/api/paste/:id"] {:keys [id private contents language username token]}
(response/json
(if-let [paste (paste/get-paste id)]
(let [user (api/validate-user username token)]
(cond
(string? user) {:error "Username or token is incorrect."}
(nil? (:user paste)) {:error "You can't edit anonymous pastes."}
(nil? user) {:error "You must be authenticated to edit pastes."}
- (not= (:id user) (:user paste)) {:error "You can only edit pastes created by you."}
+ (not= (:id user) (:user paste)) {:error "You can only edit pastes that you own."}
:else (let [paste (paste/update-paste paste
(or language (:language paste))
(or contents (:raw-contents paste))
@@ -67,6 +67,20 @@
(api/process-paste paste)))))
{:error "Paste does not exist."})))
+(defpage [:delete "/api/paste/:id"] {:keys [id username token]}
+ (let [resp (if-let [paste (paste/get-paste id)]
+ (let [user (api/validate-user username token)]
+ (cond
+ (string? user) {:error user}
+ (nil? (:user paste)) {:error "You can't delete anonymous pastes."}
+ (nil? user) {:error "You must be authenticated to delete pastes."}
+ (not= (:id user) (:user paste)) {:error "You can only delete pastes that you own."}
+ :else (paste/delete-paste id)))
+ {:error "Paste doesn't exist."})]
+ (if (map? resp)
+ (response/json resp)
+ (response/empty))))
+
(defpage "/api/paste/:id" {:keys [id username token]}
(response/json
(if-let [paste (paste/get-paste id)]

0 comments on commit a82c611

Please sign in to comment.