Skip to content

Commit 2dc716c

Browse files
committed
web: support adding new transactions via JSON PUT (#316)
A single transaction can be added by PUT to /add. (I read that PUT, not POST, should be used to create; perhaps the web add form should also use PUT ?) As with the web form, the `add` capability is required (and enabled by default). Here's how to test with curl: $ curl -s http://127.0.0.1:5000/add -X PUT -H 'Content-Type: application/json' --data-binary @in.json; echo New readJsonFile/writeJsonFile helpers in Hledger.Web.Json are handy for generating test data. Eg: >>> writeJsonFile "in.json" (head $ jtxns samplejournal)
1 parent b46212a commit 2dc716c

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

hledger-web/Hledger/Web/Handler/AddR.hs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@
77
module Hledger.Web.Handler.AddR
88
( getAddR
99
, postAddR
10+
, putAddR
1011
) where
1112

13+
import Data.Aeson.Types (Result(..))
1214
import qualified Data.Text as T
15+
import Network.HTTP.Types.Status (status400)
1316
import Text.Blaze.Html (preEscapedToHtml)
17+
import Yesod
1418

1519
import Hledger
16-
import Hledger.Cli.Commands.Add (appendToJournalFileOrStdout)
20+
import Hledger.Cli.Commands.Add (appendToJournalFileOrStdout, journalAddTransaction)
1721
import Hledger.Web.Import
22+
import Hledger.Web.Json ()
23+
import Hledger.Web.WebOptions (WebOpts(..))
1824
import Hledger.Web.Widget.AddForm (addForm)
1925

2026
getAddR :: Handler ()
@@ -31,6 +37,7 @@ postAddR = do
3137
let t = txnTieKnot res'
3238
-- XXX(?) move into balanceTransaction
3339
liftIO $ ensureJournalFileExists (journalFilePath j)
40+
-- XXX why not journalAddTransaction ?
3441
liftIO $ appendToJournalFileOrStdout (journalFilePath j) (showTransaction t)
3542
setMessage "Transaction added."
3643
redirect JournalR
@@ -46,3 +53,17 @@ postAddR = do
4653
<form#addform.form.col-xs-12.col-md-8 method=post enctype=#{enctype}>
4754
^{view}
4855
|]
56+
57+
-- Add a single new transaction, send as JSON via PUT, to the journal.
58+
-- The web form handler above should probably use PUT as well.
59+
putAddR :: Handler RepJson
60+
putAddR = do
61+
VD{caps, j, opts} <- getViewData
62+
when (CapAdd `notElem` caps) (permissionDenied "Missing the 'add' capability")
63+
64+
(r :: Result Transaction) <- parseCheckJsonBody
65+
case r of
66+
Error err -> sendStatusJSON status400 ("could not parse json: " ++ err ::String)
67+
Success t -> do
68+
liftIO $ journalAddTransaction j (cliopts_ opts) t
69+
sendResponseCreated TransactionsR

hledger-web/config/routes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/ RootR GET
66
/journal JournalR GET
77
/register RegisterR GET
8-
/add AddR GET POST
8+
/add AddR GET POST PUT
99

1010
/manage ManageR GET
1111
/edit/#FilePath EditR GET POST

hledger-web/hledger-web.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cabal-version: 1.12
44
--
55
-- see: https://github.com/sol/hpack
66
--
7-
-- hash: 561c0c98e7883244294c66058dba4862cbb498a2f5211e2abdd034ff7156be9a
7+
-- hash: db2d8932eb2a684f9139d117f6901d746b8795d70264d00fe8d45625cf7a7cc3
88

99
name: hledger-web
1010
version: 1.13.99
@@ -173,6 +173,7 @@ library
173173
, hledger-lib >=1.13.99 && <1.14
174174
, http-client
175175
, http-conduit
176+
, http-types
176177
, json
177178
, megaparsec >=7.0.0 && <8
178179
, mtl

hledger-web/package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ library:
119119
- hjsmin
120120
- http-conduit
121121
- http-client
122+
- http-types
122123
- json
123124
- megaparsec >=7.0.0 && <8
124125
- mtl

0 commit comments

Comments
 (0)