about summary refs log tree commit diff
path: root/src/App.hs
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-07-31T10·25+0100
committerWilliam Carroll <wpcarro@gmail.com>2020-07-31T10·25+0100
commited557fb6be749b0b06666674e35db4a75655af08 (patch)
tree14e5d5feeba801a782e68c8c18c17adbed4ca6b9 /src/App.hs
parent7d64011cbd6b0d6ce2237de2a3dfcc1f9f81a4c9 (diff)
Support PATCH /trips
Support a top-level PATCH request to trips that permits any admin to update any
trip, and any user to update any of their trips.

I'm using Aeson's (:?) combinator to support missing fields from the incoming
JSON requests, and then M.fromMaybe to apply these values to any record that
matches the primary key.

See the TODOs that I introduced for some shortcomings.
Diffstat (limited to 'src/App.hs')
-rw-r--r--src/App.hs14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/App.hs b/src/App.hs
index 4f02cb4447db..df70910510e0 100644
--- a/src/App.hs
+++ b/src/App.hs
@@ -62,6 +62,7 @@ server T.Config{..} = createAccount
                  :<|> deleteAccount
                  :<|> listAccounts
                  :<|> createTrip
+                 :<|> updateTrip
                  :<|> deleteTrip
                  :<|> listTrips
                  :<|> login
@@ -120,6 +121,19 @@ server T.Config{..} = createAccount
         liftIO $ Trips.create dbFile trip
         pure NoContent
 
+    updateTrip :: T.SessionCookie -> T.UpdateTripRequest -> Handler NoContent
+    updateTrip cookie updates@T.UpdateTripRequest{..} =
+      adminsAnd cookie (\T.Account{..} -> accountUsername == T.tripPKUsername updateTripRequestTripPK) $ do
+        mTrip <- liftIO $ Trips.get dbFile updateTripRequestTripPK
+        case mTrip of
+          Nothing -> throwError err400 { errBody = "tripKey is invalid" }
+          Just trip@T.Trip{..} -> do
+            -- TODO(wpcarro): Prefer function in Trips module that does this in a
+            -- DB transaction.
+            liftIO $ Trips.delete dbFile updateTripRequestTripPK
+            liftIO $ Trips.create dbFile (T.updateTrip updates trip)
+            pure NoContent
+
     deleteTrip :: T.SessionCookie -> T.TripPK -> Handler NoContent
     deleteTrip cookie tripPK@T.TripPK{..} =
       adminsAnd cookie (\T.Account{..} -> accountUsername == tripPKUsername) $ do