Skip to content

Commit

Permalink
web: support adding new transactions via JSON PUT (#316)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
simonmichael committed Feb 21, 2019
1 parent b46212a commit 2dc716c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
23 changes: 22 additions & 1 deletion hledger-web/Hledger/Web/Handler/AddR.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@
module Hledger.Web.Handler.AddR
( getAddR
, postAddR
, putAddR
) where

import Data.Aeson.Types (Result(..))
import qualified Data.Text as T
import Network.HTTP.Types.Status (status400)
import Text.Blaze.Html (preEscapedToHtml)
import Yesod

import Hledger
import Hledger.Cli.Commands.Add (appendToJournalFileOrStdout)
import Hledger.Cli.Commands.Add (appendToJournalFileOrStdout, journalAddTransaction)
import Hledger.Web.Import
import Hledger.Web.Json ()
import Hledger.Web.WebOptions (WebOpts(..))
import Hledger.Web.Widget.AddForm (addForm)

getAddR :: Handler ()
Expand All @@ -31,6 +37,7 @@ postAddR = do
let t = txnTieKnot res'
-- XXX(?) move into balanceTransaction
liftIO $ ensureJournalFileExists (journalFilePath j)
-- XXX why not journalAddTransaction ?
liftIO $ appendToJournalFileOrStdout (journalFilePath j) (showTransaction t)
setMessage "Transaction added."
redirect JournalR
Expand All @@ -46,3 +53,17 @@ postAddR = do
<form#addform.form.col-xs-12.col-md-8 method=post enctype=#{enctype}>
^{view}
|]

-- Add a single new transaction, send as JSON via PUT, to the journal.
-- The web form handler above should probably use PUT as well.
putAddR :: Handler RepJson
putAddR = do
VD{caps, j, opts} <- getViewData
when (CapAdd `notElem` caps) (permissionDenied "Missing the 'add' capability")

(r :: Result Transaction) <- parseCheckJsonBody
case r of
Error err -> sendStatusJSON status400 ("could not parse json: " ++ err ::String)
Success t -> do
liftIO $ journalAddTransaction j (cliopts_ opts) t
sendResponseCreated TransactionsR
2 changes: 1 addition & 1 deletion hledger-web/config/routes
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/ RootR GET
/journal JournalR GET
/register RegisterR GET
/add AddR GET POST
/add AddR GET POST PUT

/manage ManageR GET
/edit/#FilePath EditR GET POST
Expand Down
3 changes: 2 additions & 1 deletion hledger-web/hledger-web.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
-- hash: 561c0c98e7883244294c66058dba4862cbb498a2f5211e2abdd034ff7156be9a
-- hash: db2d8932eb2a684f9139d117f6901d746b8795d70264d00fe8d45625cf7a7cc3

name: hledger-web
version: 1.13.99
Expand Down Expand Up @@ -173,6 +173,7 @@ library
, hledger-lib >=1.13.99 && <1.14
, http-client
, http-conduit
, http-types
, json
, megaparsec >=7.0.0 && <8
, mtl
Expand Down
1 change: 1 addition & 0 deletions hledger-web/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ library:
- hjsmin
- http-conduit
- http-client
- http-types
- json
- megaparsec >=7.0.0 && <8
- mtl
Expand Down

0 comments on commit 2dc716c

Please sign in to comment.